Webhooks
A webhook endpoint is an address of yours that Black calls with a POST for each event of an environment. An environment holds up to 16 endpoints, and an endpoint belongs to one environment: a test environment and a live environment each have their own.
Register an endpoint
Send POST /v1/webhook-endpoints with the url of your endpoint. Two more fields are optional: description, and enabled_events, a list of event types the endpoint receives. Without enabled_events, the endpoint receives every event. The Create a webhook endpoint page lists every field.
The response holds the endpoint, with an ID that starts with we_, and its signing secret, which starts with whsec_.
Black shows the secret once: in the response that creates the endpoint, and in the response that rotates it. Retrieving or listing the endpoint again does not show it. Sending the same registration again with the same Idempotency-Key answers without the secret, so the first answer is the only one that holds it. Store the secret when you receive it. When you lose it, rotate it.
Use a public https address
Black accepts a url only when all of these hold. Otherwise it answers 422 and names the url field.
- The address starts with
https, in a test environment too. - The address has no user name or password and is at most 2,048 characters long.
- The host name holds only letters, digits, dots and hyphens, and it resolves.
- The host is not, and does not resolve to, a loopback, private, link-local or otherwise reserved address.
Black checks the address again before every attempt, and an address that fails the check counts as a failed attempt. Black follows no redirect: an answer of 301 or 302 is a failed attempt, so give the final address. An address on your own machine, such as localhost, is refused: put a public address in front of it while you test.
Read a delivery
A delivery is a POST whose body is the event as JSON, the same bytes on every attempt. Black reads a 2xx answer as received. Any other answer, no answer within 10 seconds, or an answer larger than 64 KiB is a failed attempt. Answer quickly, then do your own work afterwards.
Each delivery carries these headers:
| Header | Holds |
|---|---|
Black-Signature | The signature. See Check the signature. |
Black-Event-Id | The ID of the event, the same on every attempt. |
Black-Event-Created | When the event was created, in Unix seconds. |
Black-Delivery-Id | The ID of the delivery. |
Black-Delivery-Attempt | The number of this attempt, starting at 1. |
Check the signature
The Black-Signature header has the form t=<timestamp>,v1=<signature>. t is the time of this attempt in Unix seconds. Each v1 is a signature, in hexadecimal.
To check a delivery, take the raw body exactly as it arrived, before any JSON parsing, and do these steps:
- Read
tand everyv1from the header. - Refuse the delivery when
tis more than 300 seconds from your own clock. - Compute the HMAC-SHA256 of the text
t, a dot and the raw body, in hexadecimal, with the whole secret as the key. The key includes thewhsec_prefix. - Accept the delivery when the result equals any
v1. Compare the two in constant time, with the function your language provides for it.
A signature never changes for an attempt, but each retry is signed again with its own t, so a retry that arrives late still passes step 2.
Rotate a secret
Send POST /v1/webhook-endpoints/{id}/roll-secret, as on the Roll the signing secret page. The response shows the new secret.
The optional expires_in_hours is how long the old secret stays valid, from 0 to 168. It is 0 by default, which ends the old secret at once. While the old secret is valid, each delivery carries two signatures, t=<timestamp>,v1=<new>,v1=<old>, and previous_secret_expires on the endpoint says when the old one ends. Store the new secret, then deploy it: a check that accepts any v1 passes with either secret during the overlap.
Expect retries
Black sends again a delivery that fails, on a schedule that depends on the environment.
| Environment | Retries |
|---|---|
| Test | After 1, 5 and 30 minutes: 4 attempts in all. |
| Live | After 1 minute, 5 minutes, 30 minutes, 2 hours, 5 hours and 10 hours, then every 12 hours until 3 days have passed since the first attempt: 11 attempts at most. |
After the last attempt the delivery is marked failed and Black does not send it again by itself. In a live environment, an endpoint whose every attempt has failed for 3 days is switched off, and Black records the event webhook_endpoint.disabled. A test endpoint is never switched off.
A switched-off endpoint receives nothing. Enable it again with PATCH /v1/webhook-endpoints/{id} and "disabled": false. It then receives the events recorded from that moment, not the events it missed. To fill the gap, list the events, as the next section says.
Handle repeats and events out of order
Black sends an event at least once and in no fixed order. The same event can arrive twice, such as after a retry whose first attempt you answered too late, and a later event can arrive before an earlier one.
For a customer's events, order and de-duplicate them by customer_sequence:
- Apply a customer's events in the order of their
customer_sequence, not in the order they arrive. - Ignore an event whose number you have already applied for that customer.
Put a customer's events in order explains the number and how to fill a gap. An event about the environment as a whole carries no customer_sequence: de-duplicate it by Black-Event-Id, or by the id in its body.
A runnable sample
The sample registers an endpoint for one event type that the sample never causes, so nothing is delivered to the address. It reads the secret from the response, signs a fixed body in the form t=<timestamp>,v1=<signature> and checks it as the steps above describe, then rotates the secret with the old one kept for 24 hours and checks the header with both signatures. It also confirms that a wrong key and a timestamp 301 seconds old are refused. It stops with an error when a check fails, and it deletes the endpoint at the end. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, and needs sed and openssl. A test environment takes five writes a second, so the sample waits a second between its writes.
TOKEN=PLACEHOLDER_ACCESS_TOKEN
BODY='{"object":"event","id":"evt_sample","type":"customer.created"}'
NOW=$(date +%s)
sign() { printf '%s' "$2.$BODY" | openssl dgst -sha256 -hmac "$1" | sed 's/^.*= //'; }
check() {
local header=$1 secret=$2 t age signed v1
t=$(printf '%s' "$header" | sed -n 's/^t=\([0-9]*\),.*/\1/p')
[ -n "$t" ] || return 1
age=$(( $(date +%s) - t ))
[ "${age#-}" -le 300 ] || return 1
signed=$(sign "$secret" "$t")
for v1 in $(printf '%s' "$header" | tr ',' '\n' | sed -n 's/^v1=//p'); do
[ "$v1" = "$signed" ] && return 0
done
return 1
}
CREATED=$(curl --silent --request POST https://api.black.example/v1/webhook-endpoints \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"url":"https://example.com/black","description":"Docs sample","enabled_events":["webhook_endpoint.disabled"]}')
ENDPOINT=$(printf '%s' "$CREATED" | sed -n 's/.*"id":"\(we_[0-9A-Za-z]*\)".*/\1/p')
SECRET=$(printf '%s' "$CREATED" | sed -n 's/.*"secret":"\(whsec_[0-9A-Za-z]*\)".*/\1/p')
[ -n "$SECRET" ] || { echo 'The response holds no secret.'; exit 1; }
HEADER="t=$NOW,v1=$(sign "$SECRET" "$NOW")"
check "$HEADER" "$SECRET" || { echo 'The signature of the first secret was refused.'; exit 1; }
if check "$HEADER" "whsec_wrongkey"; then echo 'A wrong key was accepted.'; exit 1; fi
STALE=$(( NOW - 301 ))
if check "t=$STALE,v1=$(sign "$SECRET" "$STALE")" "$SECRET"; then echo 'A stale timestamp was accepted.'; exit 1; fi
ROLLED=$(curl --silent --request POST https://api.black.example/v1/webhook-endpoints/$ENDPOINT/roll-secret \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"expires_in_hours":24}')
NEW_SECRET=$(printf '%s' "$ROLLED" | sed -n 's/.*"secret":"\(whsec_[0-9A-Za-z]*\)".*/\1/p')
[ -n "$NEW_SECRET" ] && [ "$NEW_SECRET" != "$SECRET" ] || { echo 'The rotation gave no new secret.'; exit 1; }
HEADER="t=$NOW,v1=$(sign "$NEW_SECRET" "$NOW"),v1=$(sign "$SECRET" "$NOW")"
check "$HEADER" "$NEW_SECRET" || { echo 'The new secret was refused.'; exit 1; }
check "$HEADER" "$SECRET" || { echo 'The old secret was refused.'; exit 1; }
curl --silent --request DELETE https://api.black.example/v1/webhook-endpoints/$ENDPOINT \
--header "Authorization: Bearer $TOKEN"
The create request answers 201, and the rotation and the delete answer 200. To check a real delivery, replace BODY with the raw body you received and HEADER with its Black-Signature header, and set SECRET to the secret you stored.