TanqoryDocs

Storefront API

อ่านสินค้าของร้านและสร้างตะกร้าจากเว็บหรือแอปของคุณเอง ด้วย storefront access token ที่แอปของคุณสร้าง

Storefront API คือ API สำหรับสิ่งที่ผู้ซื้อเห็น ได้แก่ สินค้า คอลเลกชัน หน้าเว็บ การค้นหา ตะกร้า และลิงก์ชำระเงิน ตอบบนโดเมนของร้านเอง และใช้ storefront access token ที่แอปของคุณสร้างด้วย Admin API token นี้อ่านได้เฉพาะสิ่งที่ร้านแสดงให้ผู้เข้าชมทุกคนเห็นและสร้างตะกร้าได้ จึงไม่เป็นความลับแบบเดียวกับ token ของ Admin API

ก่อนเริ่ม

คุณต้องมีแอปที่ติดตั้งบนร้านแล้วพร้อม access token ของ Admin API ตามเรียก API ครั้งแรก และโดเมนของร้าน อ่านโดเมนด้วย Admin API

GraphQL
{ shop { primaryDomain { host } } }
JSON
{ "data": { "shop": { "primaryDomain": { "host": "docs-writer-store.mytanqory.com" } } } }

1. สร้าง storefront access token

เรียก storefrontAccessTokenCreate ด้วย token ของ Admin API ของแอป mutation นี้ไม่ต้องใช้ access scope ของตัวเอง

curl
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": "mutation { storefrontAccessTokenCreate(input: { title: \"My storefront\" }) { storefrontAccessToken { accessToken accessScopes { handle } } userErrors { field message } } }"}'

คำตอบ (ย่อ token ไว้)

JSON
{
  "data": {
    "storefrontAccessTokenCreate": {
      "storefrontAccessToken": {
        "accessToken": "e1c3…",
        "accessScopes": [
          { "handle": "unauthenticated_read_product_listings" },
          { "handle": "unauthenticated_write_checkouts" },
          { "handle": "unauthenticated_read_checkouts" },
          { "handle": "unauthenticated_read_selling_plans" }
        ]
      },
      "userErrors": []
    }
  }
}

2. เรียก Storefront API

POST https://<store domain>/api/v1/stores/<store id>/2026-10/graphql โดยส่ง token ใน X-Tanqory-Storefront-Access-Token บนโดเมนของร้านเอง ที่อยู่สั้น https://<store domain>/api/2026-10/graphql.json (หรือ /graphql) คือ API เดียวกันของร้านนั้น โดยไม่ต้องใส่ store id

Shell
export TANQORY_STOREFRONT_TOKEN=<accessToken from step 1>
export STORE_HOST=<host จาก "ก่อนเริ่ม">
curl -X POST "https://$STORE_HOST/api/v1/stores/$STORE_ID/2026-10/graphql" \
  -H "X-Tanqory-Storefront-Access-Token: $TANQORY_STOREFRONT_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"query": "{ shop { name } products(first: 2) { nodes { title handle variants(first: 1) { nodes { id price { amount currencyCode } } } } } }"}'

คำตอบ

JSON
{
  "data": {
    "shop": { "name": "Docs Writer Store" },
    "products": {
      "nodes": [
        { "title": "Linen Throw", "handle": "linen-throw", "variants": { "nodes": [{ "id": "gid://tanqory/ProductVariant/01a1dcb0-711d-72b3-b72f-032093b8ec7a", "price": { "amount": "2490.0", "currencyCode": "THB" } }] } },
        { "title": "Tall Stoneware Vase", "handle": "stoneware-vase", "variants": { "nodes": [{ "id": "gid://tanqory/ProductVariant/01a1dcb0-7616-7203-9e69-2c2c34fd9f67", "price": { "amount": "1290.0", "currencyCode": "THB" } }] } }
      ]
    }
  }
}

3. สร้างตะกร้า

ส่ง id ของ variant จากขั้นที่ 2 เป็น merchandiseId

GraphQL
mutation CreateCart($lines: [CartLineInput!]) {
  cartCreate(input: { lines: $lines }) {
    cart { id checkoutUrl totalQuantity cost { totalAmount { amount currencyCode } } }
    userErrors { field message }
  }
}
JSON
{ "lines": [{ "merchandiseId": "gid://tanqory/ProductVariant/01a1dcb0-711d-72b3-b72f-032093b8ec7a", "quantity": 1 }] }

คำตอบมีตะกร้าและ checkoutUrl บนโดเมนของร้านเอง

JSON
{
  "data": {
    "cartCreate": {
      "cart": {
        "id": "gid://tanqory/Cart/c1-5c39da09b860e971a6d65b8ddedb36cf?key=4a71176a3c676c9200d6a848e2609166",
        "checkoutUrl": "https://docs-writer-store.mytanqory.com/cart/c/c1-5c39da09b860e971a6d65b8ddedb36cf?key=4a71176a3c676c9200d6a848e2609166",
        "totalQuantity": 1,
        "cost": { "totalAmount": { "amount": "2490.0", "currencyCode": "THB" } }
      },
      "userErrors": []
    }
  }
}

ส่งผู้ซื้อไปที่ checkoutUrl เพื่อชำระเงิน เก็บ id ของตะกร้าพร้อม key ไว้ใช้เพิ่มรายการภายหลังด้วย cartLinesAdd

ข้อผิดพลาด

ถ้าไม่มี token token ไม่ถูกต้อง หรือเป็นของร้านอื่น จะได้ HTTP 401 ก่อน GraphQL ทำงาน

JSON
{"error":{"code":"invalid_access_token","message":"Send a valid access token in the X-Tanqory-Storefront-Access-Token header. The token sent is missing, not valid, or belongs to another store.","status":401}}

กรณีอื่นเป็นคำตอบของ GraphQL ตามที่อธิบายในข้อผิดพลาด

ควรรู้

  • ร้านที่ตั้งรหัสผ่าน รหัสผ่านของร้านปิดเฉพาะหน้าเว็บของร้านออนไลน์ ไม่ได้ปิด Storefront API ร้านใหม่รวมถึงร้านทดสอบเริ่มต้นแบบมีรหัสผ่าน และ API ยังตอบเมื่อใช้ token ที่ถูกต้อง
  • เรียกจากที่ใด จากเซิร์ฟเวอร์ของคุณ หรือจากเบราว์เซอร์บนโดเมนใดก็ได้: API ตอบคำขอ preflight ข้ามโดเมนของเบราว์เซอร์ และให้ทุกหน้าอ่านคำตอบได้ (Access-Control-Allow-Origin: * ไม่ใช้ cookie) เบราว์เซอร์ส่งได้เฉพาะ X-Tanqory-Storefront-Access-Token แบบสาธารณะ ห้ามส่ง private token ซึ่งใช้กับเซิร์ฟเวอร์เท่านั้น
  • เวอร์ชัน ระบุเวอร์ชันใน URL แบบด้านบน /api/v1/stores/<store id>/graphql ที่ไม่ระบุเวอร์ชันจะใช้เวอร์ชันล่าสุดและเปลี่ยนตามทุกการออกเวอร์ชัน ดู Versioning
  • token เปิดอะไรได้บ้าง scope สาธารณะตามขั้นที่ 1 ได้แก่ สินค้าและคอลเลกชันที่เผยแพร่ ตะกร้าและลิงก์ชำระเงิน และ selling plan ไม่มีข้อมูลลูกค้าหรือคำสั่งซื้อ และเปลี่ยนแปลงร้านไม่ได้
  • ข้อมูลอ้างอิง Storefront API แสดงทุก query, mutation และ type

สร้างจาก commit c0b3fb3fdef7 · เวอร์ชัน API 2026-10