ข้ามไปยังเนื้อหา

REST API & Webhooks

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

  1. ใน GrowthBook ไปที่ Settings → API Keys
  2. คลิก Add API Key
  3. เลือก Secret Key (สิทธิ์อ่าน/เขียนเต็ม) หรือ Read-only Key (ปลอดภัยสำหรับ dashboard)
  4. ตั้งคำอธิบาย เช่น ci-pipeline
  5. คลิก Create และคัดลอก key — แสดงเพียงครั้งเดียว
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

# 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 ของคุณ

Webhooks คือ HTTP call ขาออกที่ GrowthBook ส่งออกไปทุกครั้งที่ feature หรือ experiment เปลี่ยนแปลง กรณีใช้งานหลักคือ SDK cache invalidation: CDN หรือ application server ของคุณรับ webhook และดึง GrowthBook feature payload ใหม่ทันที ทำให้ผู้ใช้ได้รับ flag state ล่าสุดโดยไม่ต้องรอ poll cycle ถัดไป

  1. ไปที่ Settings → SDK Connections และเปิด SDK connection ที่ต้องการ hook
  2. คลิก WebhooksAdd Webhook
  3. ใส่ Endpoint URL — URL HTTPS ที่ server หรือ CDN ของคุณรับฟัง
  4. (ไม่บังคับ) ตั้ง Signing Secret เพื่อให้ server ของคุณยืนยันว่า payload มาจาก GrowthBook จริง
  5. คลิก Save GrowthBook ส่ง test ping ทันที

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

นอกจาก 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"
}
ตัวเลือกBenefitCost
Webhookreal-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 ได้เร็วขึ้น

คุณสร้าง API key ได้ที่ไหนใน GrowthBook?
GrowthBook toggle endpoint ใช้ HTTP method อะไร?
กรณีใช้งานหลักของ SDK webhook คืออะไร?
GrowthBook ใช้ HTTP header อะไรส่ง HMAC-SHA256 webhook signature?