Developer Docs

เริ่มต้น

Partner API — Auth

Partner API — Products

Partner API — Inventory

Partner API — Customers

Partner API — Reports

Get Access Token

ใช้ Endpoint นี้เพื่อแลก client_id และ client_secret เป็น Access Token สำหรับเรียกใช้งาน Partner API

Partner API ใช้รูปแบบ OAuth 2.0 Client Credentials Flow โดย Application จะยืนยันตัวตนด้วย Client Credentials ของตนเอง และได้รับ Bearer Token สำหรับใช้ในการเรียก API

Token Reuse

Access Token สามารถนำกลับมาใช้ซ้ำได้จนกว่าจะหมดอายุ

ไม่ควรเรียก /oauth/token ทุกครั้งก่อนเรียก API เนื่องจากจะเพิ่มภาระให้ระบบโดยไม่จำเป็น

แนวทางที่แนะนำ:

  1. ขอ Access Token ครั้งแรกเมื่อเริ่มต้นใช้งาน
  2. เก็บ Token ไว้ใน Memory หรือ Cache
  3. ใช้ Token เดิมกับทุก API Request
  4. ขอ Token ใหม่เมื่อ:
    • Token หมดอายุ (expires_in)
    • API ตอบกลับ 401 invalid_token

Endpoint

POST /api/partner/v1/oauth/token

Request Headers

Content-Type: application/x-www-form-urlencoded

Authorization Header ไม่จำเป็นสำหรับ Endpoint นี้


Request Body

FieldTypeRequiredDescription
grant_typestringYesต้องเป็น client_credentials
client_idstringYesClient ID ที่ได้รับจาก Open API Settings
client_secretstringYesClient Secret ที่ได้รับตอนสร้าง Application

Example Request

curl -X POST https://<host>/api/partner/v1/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "client_id=meed_0cb08343fffe4134028921bf07882ea8" \
  -d "client_secret=b47f8cb732a93dd355565a185a6a04efc03ca62fd707fbf51261744228f13d76"

Successful Response

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 3600
}

Response Fields

FieldTypeDescription
access_tokenstringJWT Access Token สำหรับเรียก Partner API
token_typestringค่าเป็น Bearer เสมอ
expires_innumberอายุของ Token (วินาที)

Token Lifecycle

Access Token มีอายุจำกัดตามค่า expires_in

เมื่อ Token หมดอายุ:

  1. Partner API จะปฏิเสธ Request
  2. ระบบควรเรียก /oauth/token
  3. รับ Token ใหม่
  4. Retry Request เดิมด้วย Token ใหม่

ระบบไม่รองรับ Refresh Token

ดังนั้นเมื่อ Access Token หมดอายุ ให้เรียก Endpoint นี้อีกครั้งด้วย client_id และ client_secret เดิม


Using Access Token

แนบ Access Token ใน Authorization Header ของทุก Partner API Request

Authorization: Bearer <access_token>

ตัวอย่าง:

curl https://<host>/api/partner/v1/sales \
  -H "Authorization: Bearer <access_token>"

Security Recommendations

Keep Client Secret Secure

client_secret เปรียบเสมือนรหัสผ่านของ Application

ควรเก็บไว้เฉพาะฝั่ง Server เท่านั้น

ไม่ควร:

  • ฝังไว้ใน Mobile Application
  • ฝังไว้ใน Frontend JavaScript
  • เปิดเผยใน Repository หรือ Source Code

Rotate Credentials

หากสงสัยว่า Client Secret รั่วไหล ควรสร้าง Application ใหม่และยกเลิกการใช้งาน Credentials เดิมทันที

Use HTTPS Only

Partner API รองรับเฉพาะ HTTPS เพื่อป้องกันการดักจับข้อมูลระหว่างทาง


Error Responses

Invalid Request

400 Bad Request
{
  "error": "invalid_request",
  "message": "grant_type, client_id, and client_secret are required and grant_type must be \"client_credentials\""
}

สาเหตุ:

  • ไม่ส่ง Required Field
  • grant_type ไม่ใช่ client_credentials

Invalid Client

401 Unauthorized
{
  "error": "invalid_client",
  "message": "Invalid client_id or client_secret"
}

สาเหตุ:

  • client_id ไม่ถูกต้อง
  • client_secret ไม่ถูกต้อง
  • Credentials ไม่ตรงกัน

Suspended or Revoked Application

401 Unauthorized
{
  "error": "invalid_client",
  "message": "API application is suspended or revoked"
}

สาเหตุ:

  • API Application ถูกระงับ
  • API Application ถูกยกเลิกการใช้งาน
  • สถานะ Application ไม่ใช่ ACTIVE