เรียก API ครั้งแรก
สร้างแอป ติดตั้งบนร้านทดสอบ รับ access token แล้วอ่านข้อมูลร้านด้วย Admin API
คู่มือนี้ใช้เวลาประมาณสิบนาที คุณต้องมี partner organization และ ร้านทดสอบ โค้ดทั้งหมดรันบนเครื่องของคุณ ยังไม่ต้อง deploy อะไร
1. สร้างแอป
- ใน partner dashboard เปิด Apps แล้วเลือก Create app
- กรอก App name ส่วน Handle ไม่บังคับ ระบบสร้างจากชื่อให้ และใช้เป็นที่อยู่ของแอปในหน้าจัดการร้าน
- เลือก Create app หน้าต่าง Save your client secret จะแสดง Client ID และ Client secret โดย secret แสดงเพียงครั้งนี้ครั้งเดียว ให้เก็บไว้ในที่เก็บความลับของคุณทันที แล้วเลือก I saved the secret




2. ตั้งค่าแอป
เปิด Configuration
- App URL หน้าที่หน้าจัดการร้านจะเปิด เช่น
https://example.com/app - Redirect URLs ที่อยู่ที่ Tanqory ส่งร้านค้ากลับมาหลังติดตั้ง เช่น
https://example.com/auth/callbackแล้วเลือก Add redirect URL เพิ่มได้สูงสุด 10 รายการ - Access scopes หัวข้อ Required เลือก Add scope ค้นหา
read_productsแล้วเลือก เพิ่มwrite_productsด้วยวิธีเดียวกัน - เลือก Save version




หน้า Home ของแอปจะแสดง OAuth endpoints ได้แก่ ที่อยู่ authorize ของร้านทดสอบแต่ละร้าน ที่อยู่ token และ session keys
3. ติดตั้งบนร้านทดสอบ
การติดตั้งคือ OAuth 2.0 authorization code grant แบบ PKCE (RFC 6749, RFC 7636) ให้สร้างค่า state และ code verifier เก็บไว้ทั้งสองค่า แล้วส่งร้านค้า (ในที่นี้คือตัวคุณเอง) ไปที่ที่อยู่ authorize
ตั้งค่าเหล่านี้ใน shell ก่อน
export STORE_ID=<the store id from the store dashboard address>
export TANQORY_CLIENT_ID=<your client ID>
export TANQORY_CLIENT_SECRET=<your client secret>// Node.js 18 or later: the values for one install. Keep state and verifier until the callback.
import { createHash, randomBytes } from 'node:crypto';
const state = randomBytes(16).toString('hex');
const verifier = randomBytes(32).toString('base64url');
const challenge = createHash('sha256').update(verifier).digest('base64url');
const url = new URL(`https://admin.tanqory.com/admin/store/${process.env.STORE_ID}/oauth/authorize`);
url.search = new URLSearchParams({
client_id: process.env.TANQORY_CLIENT_ID,
scope: 'read_products,write_products',
redirect_uri: 'https://example.com/auth/callback',
state,
code_challenge: challenge,
code_challenge_method: 'S256',
});
console.log(JSON.stringify({ state, verifier, url: url.href }));# Python 3: the values for one install. Keep state and verifier until the callback.
import base64, hashlib, json, os, secrets
from urllib.parse import urlencode
state = secrets.token_hex(16)
verifier = secrets.token_urlsafe(32)
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
url = f"https://admin.tanqory.com/admin/store/{os.environ['STORE_ID']}/oauth/authorize?" + urlencode({
"client_id": os.environ["TANQORY_CLIENT_ID"],
"scope": "read_products,write_products",
"redirect_uri": "https://example.com/auth/callback",
"state": state,
"code_challenge": challenge,
"code_challenge_method": "S256",
})
print(json.dumps({"state": state, "verifier": verifier, "url": url}))เปิด url ที่พิมพ์ออกมาในเบราว์เซอร์ที่เข้าสู่ระบบหน้าจัดการร้านไว้ หน้า Authorize app จะแสดงชื่อแอป องค์กรของคุณ และสิ่งที่แอปจะทำได้ เลือก Install app


เบราว์เซอร์จะไปที่ redirect URL ของคุณพร้อมพารามิเตอร์หกตัว ได้แก่ code, hmac, shop, state, store_id และ timestamp ให้คัดลอกที่อยู่จากเบราว์เซอร์ไว้ หน้าของคุณยังไม่ต้องมีอยู่จริงก็ได้
4. ตรวจ callback แล้วแลก code
ก่อนใช้ code ให้ตรวจว่า callback มาจาก Tanqory จริง state ต้องเป็นค่าที่คุณสร้าง และ hmac ต้องเป็น HMAC-SHA256 แบบเลขฐานสิบหกของพารามิเตอร์อื่นทั้งหมด เรียงตามชื่อ ต่อกันในรูป name=value&name=value โดยใช้ client secret เป็นกุญแจ
import { createHmac, timingSafeEqual } from 'node:crypto';
// query: the callback's query string, for example new URL(req.url, 'https://example.com').search
export function callbackIsFromTanqory(query, clientSecret) {
const params = new URLSearchParams(query);
const message = [...params]
.filter(([key]) => key !== 'hmac')
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
.map(([key, value]) => `${key}=${value}`)
.join('&');
const expected = createHmac('sha256', clientSecret).update(message).digest('hex');
const given = params.get('hmac') ?? '';
return given.length === expected.length && timingSafeEqual(Buffer.from(given), Buffer.from(expected));
}import hashlib, hmac
from urllib.parse import parse_qsl
def callback_is_from_tanqory(query: str, client_secret: str) -> bool:
params = dict(parse_qsl(query.lstrip("?"), keep_blank_values=True))
given = params.pop("hmac", "")
message = "&".join(f"{key}={value}" for key, value in sorted(params.items()))
expected = hmac.new(client_secret.encode(), message.encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(given, expected)จากนั้นแลก code เป็น access token โดย code ใช้ได้ครั้งเดียว และใช้ได้กับ verifier ของการติดตั้งครั้งเดียวกันเท่านั้น
export CODE=<code from the callback>
export CODE_VERIFIER=<verifier from step 3>curl -X POST "https://api.tanqory.com/v1/stores/$STORE_ID/apps/oauth/token" \
--data-urlencode "client_id=$TANQORY_CLIENT_ID" \
--data-urlencode "client_secret=$TANQORY_CLIENT_SECRET" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=$CODE" \
--data-urlencode "code_verifier=$CODE_VERIFIER" \
--data-urlencode "redirect_uri=https://example.com/auth/callback"// Node.js 18 or later. Save as exchange.mjs and run: node exchange.mjs
const res = await fetch(`https://api.tanqory.com/v1/stores/${process.env.STORE_ID}/apps/oauth/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
client_id: process.env.TANQORY_CLIENT_ID,
client_secret: process.env.TANQORY_CLIENT_SECRET,
grant_type: 'authorization_code',
code: process.env.CODE,
code_verifier: process.env.CODE_VERIFIER,
redirect_uri: 'https://example.com/auth/callback',
}),
});
console.log(res.status, await res.json()); // { access_token, scope, store_id, token_type: 'Bearer' }# Python 3, standard library only. Save as exchange.py and run: python3 exchange.py
import json, os, urllib.parse, urllib.request
req = urllib.request.Request(
f"https://api.tanqory.com/v1/stores/{os.environ['STORE_ID']}/apps/oauth/token",
data=urllib.parse.urlencode({
"client_id": os.environ["TANQORY_CLIENT_ID"],
"client_secret": os.environ["TANQORY_CLIENT_SECRET"],
"grant_type": "authorization_code",
"code": os.environ["CODE"],
"code_verifier": os.environ["CODE_VERIFIER"],
"redirect_uri": "https://example.com/auth/callback",
}).encode(),
)
with urllib.request.urlopen(req) as res:
print(res.status, json.load(res))คำตอบมี access_token, token_type (Bearer), scope (scope ที่ได้รับ คั่นด้วยช่องว่าง) และ store_id โดยไม่มีเวลาหมดอายุ เก็บ token ไว้บนเซิร์ฟเวอร์ ร้านละหนึ่ง token
5. เรียก Admin API
- เซิร์ฟเวอร์ของแอปคุณ → Tanqory edge POST /admin/<version>/stores/<store id>/graphql พร้อม Authorization: Bearer ถ้าไม่ผ่าน: 404 สำหรับ path อื่น
- Tanqory edge → Admin API ส่งต่อไปยังเซิร์ฟเวอร์ที่เก็บร้านนั้น
- Admin API มีเวอร์ชันนั้นอยู่จริง ถ้าไม่ผ่าน: 404
- Admin API token ใช้ได้กับร้านนี้ ถ้าไม่ผ่าน: 401
- Admin API มีร้านนี้อยู่ และ token ใช้กับร้านนี้ได้ ถ้าไม่ผ่าน: 404
- Admin API query ถูกไวยากรณ์และถูกต้องตาม schema ถ้าไม่ผ่าน: 400 GRAPHQL_PARSE_FAILED หรือ GRAPHQL_VALIDATION_FAILED
- Admin API ทุก field อยู่ใน scope ที่ได้รับ ถ้าไม่ผ่าน: 200 พร้อม ACCESS_DENIED
- Admin API ค่า cost ของ query ไม่เกินกำหนด และ bucket มีแต้มพอ ถ้าไม่ผ่าน: 200 พร้อม MAX_COST_EXCEEDED หรือ THROTTLED และ Retry-After
- Admin API รัน query
- Admin API → เซิร์ฟเวอร์ของแอปคุณ data และ extensions.cost พร้อมแต้มที่เหลือใน bucket
export TANQORY_ACCESS_TOKEN=<access_token from step 4>curl -X POST "https://api.tanqory.com/admin/2026-10/stores/$STORE_ID/graphql" \
-H "Authorization: Bearer $TANQORY_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
--data '{"query": "{ shop { name currencyCode } }"}'// Node.js 18 or later. Save as first-call.mjs and run: node first-call.mjs
const res = await fetch(`https://api.tanqory.com/admin/2026-10/stores/${process.env.STORE_ID}/graphql`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.TANQORY_ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: '{ shop { name currencyCode } }' }),
});
console.log(res.status, await res.json());# Python 3, standard library only. Save as first_call.py and run: python3 first_call.py
import json, os, urllib.request
req = urllib.request.Request(
f"https://api.tanqory.com/admin/2026-10/stores/{os.environ['STORE_ID']}/graphql",
data=json.dumps({"query": "{ shop { name currencyCode } }"}).encode(),
headers={
"Authorization": f"Bearer {os.environ['TANQORY_ACCESS_TOKEN']}",
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(req) as res:
print(res.status, json.load(res))คำตอบ
{
"data": { "shop": { "name": "Docs Writer Store", "currencyCode": "THB" } },
"extensions": {
"cost": {
"requestedQueryCost": 1,
"actualQueryCost": 1,
"throttleStatus": { "maximumAvailable": 1000, "currentlyAvailable": 999, "restoreRate": 100 }
}
}
}extensions.cost คือส่วนที่คำขอนี้ใช้ไปจาก rate limit ของคุณ ดู Rate limits and query cost
ต่อไป
- การติดตั้งและ access token พารามิเตอร์ทุกตัว การขอสิทธิ์ซ้ำ และการติดตั้งจากในหน้าจัดการร้าน
- API reference ทุก query และ mutation พร้อม scope ที่แต่ละตัวต้องใช้