App proxy
ให้หน้าเว็บหรือ API จาก server ของคุณอยู่บนที่อยู่ของร้านเอง และตรวจว่าแต่ละคำขอมาจาก Tanqory
app proxy วาง path หนึ่งบนที่อยู่ของร้าน เช่น https://example-store.mytanqory.com/apps/book ไว้หน้า server ของคุณ เบราว์เซอร์ของผู้ซื้อเรียกร้าน Tanqory จะเติมข้อมูลว่าเป็นร้านไหนและผู้ซื้อคือใคร ลงนามคำขอ ส่งไปที่ server ของคุณ แล้วส่งคำตอบของคุณกลับ หน้าของคุณเรียกได้ด้วย fetch จาก app embed ของธีม โดยไม่ต้องตั้งค่า cross-origin
ตั้งค่า
ใน partner dashboard เปิด Configuration ของแอป เปิด Use an app proxy แล้วกรอก:
| ช่อง | ค่า |
|---|---|
| Prefix | a apps community หรือ tools |
| Subpath | ตัวอักษร ตัวเลข _ หรือ - 1 ถึง 30 ตัว ห้ามเป็น admin services password หรือ login |
| Proxy URL | ที่อยู่ https ของ server เช่น https://booking.example.com/proxy |
การเปิด proxy จะเพิ่ม scope write_app_proxy ในสิ่งที่แอปขอ จากนั้นเลือก Save version คำขอไปที่ /<prefix>/<subpath>/rest/of/path จะไปที่ <Proxy URL>/rest/of/path พร้อม query string ของผู้ซื้อ
สิ่งที่ server ของคุณได้รับ
Tanqory เติม query parameter ห้าตัวนี้ในทุกคำขอ และตัดพารามิเตอร์ชื่อเดียวกันที่ผู้ซื้อส่งมาทิ้ง:
| พารามิเตอร์ | ค่า |
|---|---|
shop | ที่อยู่ของร้านบน Tanqory เช่น example-store.mytanqory.com |
logged_in_customer_id | id ลูกค้าในร้านนี้ของผู้ซื้อที่เข้าสู่ระบบอยู่ (ส่วนท้ายของ id ใน Admin API gid://tanqory/Customer/<id>) หรือว่างเมื่อไม่มีใครเข้าสู่ระบบ |
path_prefix | /<prefix>/<subpath> เช่น /apps/book |
timestamp | เวลาที่ Tanqory ส่งคำขอ เป็นวินาที Unix |
signature | HMAC-SHA256 แบบ hex ของพารามิเตอร์อื่น โดยใช้ client secret เป็น key |
จาก header ของคำขอผู้ซื้อ จะส่งต่อเฉพาะ Accept Accept-Language Content-Type User-Agent และ X-Forwarded-For ไม่ส่ง cookie และ Tanqory ไม่เติม header ของตัวเอง: ที่นี่ไม่มี X-Tanqory-Shop-Domain ร้านคือพารามิเตอร์ shop (X-Tanqory-Shop-Domain เป็น header ของ การส่ง webhook มีค่าเดียวกับ shop)
ตรวจ signature
สร้างข้อความจากทุกพารามิเตอร์ยกเว้น signature: แต่ละชื่อเป็น name=value ถ้าชื่อซ้ำให้ต่อค่าด้วย , เรียงสตริงเหล่านี้แล้วต่อกันโดยไม่มีตัวคั่น HMAC-SHA256 แบบ hex ของข้อความนั้น โดยใช้ client secret เป็น key ต้องเท่ากับ signature เทียบแบบ constant time และปฏิเสธ timestamp ที่เก่าเกินไป
import { createHmac, timingSafeEqual } from 'node:crypto';
// query: the URLSearchParams of the request as your server received it.
export function proxyRequestIsFromTanqory(query, clientSecret, maxAgeSeconds = 300) {
const byName = new Map();
for (const [k, v] of query) if (k !== 'signature') byName.set(k, [...(byName.get(k) ?? []), v]);
const message = [...byName].map(([k, vs]) => `${k}=${vs.join(',')}`).sort().join('');
const expected = Buffer.from(createHmac('sha256', clientSecret).update(message).digest('hex'));
const given = Buffer.from(query.get('signature') ?? '');
const fresh = Math.abs(Date.now() / 1000 - Number(query.get('timestamp'))) <= maxAgeSeconds;
return fresh && given.length === expected.length && timingSafeEqual(given, expected);
}import hashlib, hmac, time
from urllib.parse import parse_qsl
# query_string: the raw query string of the request, as your server received it.
def proxy_request_is_from_tanqory(query_string: str, client_secret: str, max_age_seconds: int = 300) -> bool:
by_name: dict[str, list[str]] = {}
signature = ""
for k, v in parse_qsl(query_string, keep_blank_values=True):
if k == "signature":
signature = v
else:
by_name.setdefault(k, []).append(v)
message = "".join(sorted(f"{k}={','.join(vs)}" for k, vs in by_name.items()))
expected = hmac.new(client_secret.encode(), message.encode(), hashlib.sha256).hexdigest()
ts = next((vs[0] for k, vs in by_name.items() if k == "timestamp"), "0")
fresh = abs(time.time() - int(ts or 0)) <= max_age_seconds
return fresh and hmac.compare_digest(expected, signature)ตอบ 401 เมื่อตรวจไม่ผ่าน ระหว่างที่ client secret ที่ rotate แล้วยังไม่ถูกเพิกถอน Tanqory ลงนามด้วย secret เก่า (Secrets)
คำตอบของคุณ
- ไม่เกิน 5 MB ภายใน 10 วินาที redirect ไม่เกิน 5 ครั้ง server ต้องอยู่บนที่อยู่สาธารณะ
Set-CookieServerX-Powered-ByDatePragmaheader ของการเชื่อมต่อ และ headerX-Tq-*ของคุณเองจะถูกตัดออก ถ้าไม่มีCache-Controlของคุณเอง คำตอบจะได้private, no-storeContent-Typeเป็นapplication/liquidจะถูกปฏิเสธ ให้ส่ง HTML หรือ JSON
เมื่อไม่มีแอปที่ติดตั้งอยู่เป็นเจ้าของ path นั้น หรือการติดตั้งไม่มี write_app_proxy ผู้ซื้อจะได้ 404 พร้อม X-Tanqory-App-Proxy: none เมื่อติดต่อ server ของคุณไม่ได้หรือเกินข้อจำกัด จะได้ 502 พร้อม X-Tanqory-App-Proxy: failed:<why>