Partner API · โค้ดตัวอย่าง
โค้ดตัวอย่าง พร้อมคัดลอกไปใช้
เลือกบริการที่ต้องการ แล้วคัดลอกหรือดาวน์โหลดโค้ดได้ทันที — คีย์อ่านจากตัวแปรสภาพแวดล้อม ไม่ฝังในโค้ด · ทดลองได้ฟรีด้วยคีย์เทสและดวงตัวอย่าง
bazi-chart.mjs
// Bazi API v2 — BaZi chart · elements · stars · 5 Structures · 10 Profiles
// Node.js 18+ · run: BAZI_API_KEY=bz_live_... node bazi-chart.mjs
// Optional HMAC signing: also set BAZI_SIGNING_SECRET=bzs_...
import crypto from 'node:crypto';
const URL_BASE = 'https://bazi.fengshuix.com';
const PATH = '/api/v2/chart';
const body = JSON.stringify({
"birth": {
"date": "1985-03-15",
"time": "09:30",
"gender": "male"
},
"lang": "th",
"sections": [
"pillars",
"stars",
"elements",
"strength",
"structures"
]
});
const headers = {
'Content-Type': 'application/json',
'x-api-key': process.env.BAZI_API_KEY,
'Idempotency-Key': crypto.randomUUID(),
'X-End-User': 'user-123',
};
// Only needed when request signing is enabled for this key
if (process.env.BAZI_SIGNING_SECRET) {
const ts = String(Math.floor(Date.now() / 1000));
const nonce = crypto.randomBytes(16).toString('hex');
const base = [ts, nonce, 'POST', PATH, '0', body].join('.');
headers['X-Bazi-Timestamp'] = ts;
headers['X-Bazi-Nonce'] = nonce;
headers['X-Bazi-Signature'] = crypto.createHmac('sha256', process.env.BAZI_SIGNING_SECRET).update(base).digest('hex');
}
const res = await fetch(URL_BASE + PATH, { method: 'POST', headers, body });
const json = await res.json();
if (!res.ok || !json.success) {
console.error(res.status, json.error); // 4xx = not charged
process.exit(1);
}
console.log('cost', json.cost, 'balance', json.balance_after);
console.log(JSON.stringify(json.data, null, 2));
ข้อควรรู้
- เรียกจากเซิร์ฟเวอร์ของคุณเท่านั้น ห้ามวางคีย์ในหน้าเว็บหรือแอปมือถือ (ดูตัวอย่าง "ตัวกลางฝั่งเซิร์ฟเวอร์")
- ส่ง Idempotency-Key ใหม่ทุกคำขอ — ถ้าเครือข่ายหลุดแล้วส่งซ้ำด้วยค่าเดิม ระบบไม่ตัดเงินซ้ำ
- X-End-User (ไม่บังคับ) = รหัสผู้ใช้ฝั่งคุณ ห้ามใส่อีเมลหรือชื่อ ใช้แยกค่าใช้จ่ายรายผู้ใช้
- ถ้าเปิดลายเซ็น HMAC ให้คีย์ไว้ ต้องส่ง X-Bazi-Timestamp · X-Bazi-Nonce · X-Bazi-Signature ตามตัวอย่าง Node.js/Python/PHP