REST API & Webhooks
GrowthBook REST API
หัวข้อที่มีชื่อว่า “GrowthBook REST API”GrowthBook เปิดเผย REST API ที่ให้คุณอ่านและจัดการ features, experiments, metrics และอื่นๆ ด้วยโปรแกรม นี่คือพื้นฐานของการ automate CI/CD: deployment pipeline ของคุณสามารถ toggle flags, ตรวจสอบสถานะ experiment หรือสร้าง features ใหม่ โดยไม่ต้องให้ใครเปิด UI
base URL สำหรับ self-hosted คือ GrowthBook app URL ของคุณ เช่น http://localhost:3100/api/v1
Step 1 — สร้าง API key
หัวข้อที่มีชื่อว่า “Step 1 — สร้าง API key”- ใน GrowthBook ไปที่ Settings → API Keys
- คลิก Add API Key
- เลือก Secret Key (สิทธิ์อ่าน/เขียนเต็ม) หรือ Read-only Key (ปลอดภัยสำหรับ dashboard)
- ตั้งคำอธิบาย เช่น
ci-pipeline - คลิก Create และคัดลอก key — แสดงเพียงครั้งเดียว
Step 2 — ดึงรายการ features ผ่าน curl
หัวข้อที่มีชื่อว่า “Step 2 — ดึงรายการ features ผ่าน curl”curl -s \
-H "Authorization: Bearer YOUR_API_KEY" \
http://localhost:3100/api/v1/features | \
jq '.features[] | {id, defaultValue}'response คือ JSON object โดย array .features มี feature ทุกอันพร้อม key, environments และ rules
Step 3 — Toggle feature เปิด/ปิดผ่าน curl
หัวข้อที่มีชื่อว่า “Step 3 — Toggle feature เปิด/ปิดผ่าน curl”# Enable 'checkout-v2' in the production environment
curl -s -X POST \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"environments": {"production": true}}' \
http://localhost:3100/api/v1/features/checkout-v2/togglesแทนที่ YOUR_API_KEY ด้วย secret key ที่สร้างใน Step 1 และ checkout-v2 ด้วย feature key ของคุณ
SDK Webhooks
หัวข้อที่มีชื่อว่า “SDK Webhooks”Webhooks คือ HTTP call ขาออกที่ GrowthBook ส่งออกไปทุกครั้งที่ feature หรือ experiment เปลี่ยนแปลง กรณีใช้งานหลักคือ SDK cache invalidation: CDN หรือ application server ของคุณรับ webhook และดึง GrowthBook feature payload ใหม่ทันที ทำให้ผู้ใช้ได้รับ flag state ล่าสุดโดยไม่ต้องรอ poll cycle ถัดไป
การสร้าง SDK webhook
หัวข้อที่มีชื่อว่า “การสร้าง SDK webhook”- ไปที่ Settings → SDK Connections และเปิด SDK connection ที่ต้องการ hook
- คลิก Webhooks → Add Webhook
- ใส่ Endpoint URL — URL HTTPS ที่ server หรือ CDN ของคุณรับฟัง
- (ไม่บังคับ) ตั้ง Signing Secret เพื่อให้ server ของคุณยืนยันว่า payload มาจาก GrowthBook จริง
- คลิก Save GrowthBook ส่ง test ping ทันที
การยืนยัน webhook signature
หัวข้อที่มีชื่อว่า “การยืนยัน webhook signature”GrowthBook เซ็น webhook body ด้วย HMAC-SHA256 บน server ของคุณ:
import crypto from 'node:crypto';
function verifyWebhook(body, signature, secret) { const expected = crypto .createHmac('sha256', secret) .update(body) .digest('hex'); return crypto.timingSafeEqual( Buffer.from(expected), Buffer.from(signature) );}header X-GrowthBook-Signature มี hex digest
Event webhooks
หัวข้อที่มีชื่อว่า “Event webhooks”นอกจาก SDK cache invalidation แล้ว GrowthBook ยังรองรับ event webhooks — การแจ้งเตือนสำหรับ event ระดับสูงกว่า เช่น experiment started, experiment stopped หรือ feature saved ตั้งค่าได้ที่ Settings → Event Webhooks
// Example GrowthBook event webhook payload (feature.updated)
{
"event": "feature.updated",
"data": {
"id": "checkout-v2",
"environments": {
"production": { "enabled": true }
}
},
"timestamp": "2025-09-01T12:00:00Z"
}ข้อแลกเปลี่ยน
หัวข้อที่มีชื่อว่า “ข้อแลกเปลี่ยน”| ตัวเลือก | Benefit | Cost |
|---|---|---|
| Webhook | real-time notification เมื่อ flag เปลี่ยน ดีสำหรับ automation | ต้อง handle retry/signature verification เอง |
| Polling ผ่าน API | เรียบง่ายกว่า | มี delay และเปลือง request |
ข้อผิดพลาดที่พบบ่อย
หัวข้อที่มีชื่อว่า “ข้อผิดพลาดที่พบบ่อย”- ไม่ verify webhook signature ทำให้เสี่ยงถูกปลอม request
- ไม่ handle retry เมื่อ webhook endpoint down ทำให้พลาด event
- เก็บ API key ไว้ใน client-side code ที่เข้าถึงได้จากภายนอก
💡 ตัวอย่างจากของจริง
ทีม DevOps ใช้ webhook ของ GrowthBook เชื่อมกับ Slack เพื่อแจ้งเตือนทันทีเมื่อมีคนแก้ flag ใน production ช่วย audit และ debug ได้เร็วขึ้น