본문으로 건너뛰기

웹훅

모든 변경을 알리는 서명된 재시도 알림과, 이를 잘 받는 방법입니다.

이 페이지의 내용

엔드포인트를 추가하고 받을 이벤트를 고르세요. 각 이벤트는 작은 JSON 본문의 POST로 전송됩니다: 무엇이 어떤 레코드에 일어났는지. 자세한 내용은 키로 레코드를 조회하세요.

http
POST /webhooks HTTP/1.1
Content-Type: application/json
webhook-id: msg_01JD3XC4Q8F2N6R0T4V8X2Z6B0
webhook-timestamp: 1764576000
webhook-signature: v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=

서명 확인하기

모든 요청은 엔드포인트의 비밀 값으로 Standard Webhooks 방식에 따라 서명됩니다. 서명이 맞지 않거나 타임스탬프가 5분보다 오래된 요청은 거부하세요.

javascript
import crypto from "node:crypto";

export function verify(rawBody, headers, secret, toleranceSec = 300) {
  const id = headers["webhook-id"], ts = headers["webhook-timestamp"], sig = headers["webhook-signature"];
  if (!id || !ts || !sig || Math.abs(Date.now() / 1000 - Number(ts)) > toleranceSec) return false;
  const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
  const expected = crypto.createHmac("sha256", key).update(`${id}.${ts}.${rawBody}`).digest("base64");
  return sig.split(" ").some((p) => {
    const [v, s] = p.split(",");
    return v === "v1" && s && s.length === expected.length && crypto.timingSafeEqual(Buffer.from(s), Buffer.from(expected));
  });
}
php
<?php
function verify(string $rawBody, array $headers, string $secret, int $toleranceSec = 300): bool
{
    $id = $headers['webhook-id'] ?? null;
    $ts = $headers['webhook-timestamp'] ?? null;
    $sig = $headers['webhook-signature'] ?? null;
    if (!$id || !$ts || !$sig || abs(time() - (int) $ts) > $toleranceSec) {
        return false;
    }
    $key = base64_decode(preg_replace('/^whsec_/', '', $secret));
    $expected = base64_encode(hash_hmac('sha256', "{$id}.{$ts}.{$rawBody}", $key, true));
    foreach (explode(' ', $sig) as $part) {
        [$version, $signature] = array_pad(explode(',', $part, 2), 2, '');
        if ($version === 'v1' && hash_equals($expected, $signature)) {
            return true;
        }
    }
    return false;
}
python
import base64, hashlib, hmac, time

def verify(raw_body: bytes, headers: dict, secret: str, tolerance_sec: int = 300) -> bool:
    msg_id = headers.get("webhook-id")
    ts = headers.get("webhook-timestamp")
    sig = headers.get("webhook-signature")
    if not msg_id or not ts or not sig or abs(time.time() - int(ts)) > tolerance_sec:
        return False
    key = base64.b64decode(secret.removeprefix("whsec_"))
    signed = f"{msg_id}.{ts}.".encode() + raw_body
    expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()
    return any(
        part.split(",", 1)[0] == "v1" and hmac.compare_digest(part.split(",", 1)[1], expected)
        for part in sig.split(" ") if "," in part
    )

재시도와 순서

  • 5초 안에 2xx로 응답하고, 작업은 그 뒤에 하세요.
  • 실패한 전송은 약 32시간 동안 8번 재시도됩니다. 리디렉션은 따라가지 않습니다.
  • 같은 이벤트가 두 번 올 수 있습니다. 이미 처리한 webhook-id는 건너뛰세요.
  • 이벤트 순서가 뒤바뀔 수 있습니다. 현재 상태는 레코드를 조회해 확인하세요.
  • 3일 동안 실패한 엔드포인트는 비활성화됩니다. 고친 뒤 관리 화면에서 놓친 이벤트를 재전송하세요.

보안 체크리스트

  • 유효한 인증서의 https를 사용하세요.
  • 파싱하기 전에 원본 본문으로 서명을 확인하세요.
  • 서명용 비밀 값은 서버에 두고, 유출되면 교체하세요.
다음예약 흐름
요금무료로 시작하기