Replace polling with webhooks
Polling the team event feed every minute works, but your application learns about a finished registration up to a minute late and sends a request even when nothing happened. With webhooks, Domain Chief sends each event to your server within seconds of it happening.
A webhook carries the same event as the feed, under the same id. Keep reading the feed only to catch up on events your receiver missed. This guide builds a receiver, sets up the endpoint, processes the events most integrations need, and moves an existing polling integration over without losing events.
The Domain Chief webhooks page is the reference for the payload and the event types. Receive webhooks covers signatures, retries, and pacing, which work the same in every Chief Tools product.
Recommended flow
- Create a token that can manage webhooks and read the resources you refresh.
- Build a receiver that verifies each request, stores the event once, and answers right away.
- Create the endpoint, store its signing secret, and send a test event.
- Process stored events from a queue: act on domain activity and refresh changed resources.
- Read the event feed on a schedule to catch events the receiver missed.
Prepare a token
The integration needs these scopes:
domainchief:webhooks:writeto create and manage the endpoint. Skip it when you add the endpoint under Settings › Webhooks instead.domainchief:events:readfor the scheduled feed read.- The read scope of every resource you retrieve after an event, such as
domainchief:domains:readanddomainchief:contacts:read.
An endpoint receives every event of the team, including domain activity, whatever other scopes the token has. A token restricted to specific domains or contacts cannot manage endpoints at all.
Build the receiver
Verify and acknowledge each request
Domain Chief sends each event as a POST with a JSON body:
Code
For every request:
- Read the raw body before parsing it.
- Verify the signature and reject requests whose
webhook-timestampis more than five minutes from your clock. A Standard Webhooks library does both. - Store the event by its
id. When thatidis already stored, the event is a retry or a resend, so store nothing. - Answer
200within 15 seconds, then process the event from a queue.
Answer 200 for duplicates and for event types your application ignores, so Domain Chief does not retry them. Do the slow work, such as API requests and emails, after you respond. Respond to a delivery explains what counts as a failure.
Store the event once
Give the stored id a unique constraint. The same constraint deduplicates events that arrive both as a webhook and through the scheduled feed read, because both use the same ID.
Store the event's type, timestamp, and data next to the id. Your queue worker needs nothing else to process it.
Create the endpoint
Create the endpoint with Create a webhook endpoint. Subscribe to the types your application handles, or to * for every event including types added later:
Code
The 201 Created response contains the endpoint with its signing secret:
Code
Store secret with your other secrets right away. It is null in every later response, until you rotate it.
Then send a test event to POST /api/v1/webhooks/endpoints/$ENDPOINT_ID/test. Your receiver gets a webhook_endpoint.test event. Check that it verifies the signature and answers 200. The endpoint's delivery log in Domain Chief shows the request and your response.
Process events
Route each stored event by its type. Two kinds of events arrive:
- Domain activity, such as
domain.registration.succeeded, records something that happened to a domain. The activity is indata.activity, so act on it without another request. - Resource changes, such as
domain.changedordns_record.deleted, tell you that a resource changed.data.objectidentifies the resource, so retrieve its current state when you need it.
Complete an order after registration or transfer
Registrations and incoming transfers finish later than the request that started them. Act on their outcome:
| Type | What to do |
|---|---|
domain.registration.succeeded, domain.transfer.incoming.succeeded | Complete the order and start billing. |
domain.registration.failed, domain.transfer.incoming.failed | Mark the order as failed and show the customer what to do next. |
data.object.id is the domain ID. Retrieve the domain with its notices through List specific domains when you show the customer its status. A failed registration or transfer has a notice that explains the problem and the available actions. The domain notices guide shows how to present them.
activity.properties.cause describes a failure when Domain Chief knows the cause. Use its code to decide, and show its message to people. Webhooks always send that message in English.
Notify customers about renewals, expiry, and deletion
Code
domain.renewal.succeededhas the previous and new renewal date inactivity.properties.old.renews_atandnew.renews_at.domain.expiredanddomain.restorablemean the domain can still be restored. Offer the customer a restore before it is deleted for good.domain.deletedmeans the domain is gone.reasonisexpiredwhen it lapsed, ordeletedwhen someone asked for the deletion. Whenwas_restorableistrue, you probably told the customer already when it expired or became restorable, so send a short closing message or none at all.
reason is extensible. Treat an unknown value like deleted.
Keep a local copy current
Handle resource changes the way the sync guide handles feed events:
- Remove the local copy on
contact.deleted,dns_record.deleted,web_redirect.deleted, andmail_forward.deleted. - Retrieve the current state for the other resource events. Use
data.object.parent.idas the domain of a DNS record, web redirect, or mail forward. - Several changes to one resource in quick succession arrive as one
*.changedevent. When many events arrive at once, retrieve each resource once.
Webhooks can arrive out of order. Retrieving the current state, instead of applying each event as a change, keeps the local copy correct regardless of the order.
Handle what you do not recognize
Event types, activity events, and values such as reason are extensible. Store an unknown event, answer 200, and log it for review instead of failing.
Catch up from the event feed
Deliveries are retried for about three days. Events can still be missed when your receiver is down for longer, when the endpoint was disabled after failing for five days, or when the endpoint did not exist yet. The event feed has them all.
Read the feed from your saved cursor on a schedule, such as every hour:
Code
Skip every event whose id you already stored, process the rest, and save meta.cursor once the page is done. Continue while meta.has_more is true. A feed event has the same content as a webhook: its resource is the webhook's data.object, its expanded activity is data.activity, and its occurred_at is the timestamp.
The sync guide describes the cursor in detail, including what to do with an invalid cursor.
Move an existing polling integration
An integration that already polls the feed keeps its snapshot and cursor:
- Deploy the receiver and create the endpoint while polling continues.
- Let both run side by side. The shared event
idmakes your application process each event once. - Check the endpoint's delivery log in Domain Chief until deliveries succeed.
- Lower the polling interval to the catch-up schedule, such as once an hour.
Keep the polling code. It becomes the catch-up read.
Test with .example domains
.example domains exist only inside Domain Chief and cost nothing. Register one, change its DNS records, and delete it to receive real registration, resource, and deletion events before you rely on your receiver.
Monitor the endpoint
The endpoint's page in Domain Chief shows how its deliveries went, with each attempt's request and response, and resends any of them. When every delivery has failed for an hour, the team gets an email. After five days of failures, Domain Chief disables the endpoint. Fix the receiver, enable the endpoint again, and resend its pending deliveries from the delivery log. The scheduled feed read covers anything else.