Summary
To start receiving webhook events in your integration, create and register a webhook endpoint by following the steps below:- Create a webhook endpoint handler to receive event data POST requests.
- Register your endpoint with Brandfetch via an API request.
- Secure your webhook endpoint.
Create a handler
See the events reference to identify the event types your webhook handler needs to process. Set an HTTPS endpoint function that can accept webhook requests with a POST method. Set up your endpoint function so that it:- Handles POST requests with a JSON payload consisting of an event object.
- Quickly returns a successful status code (2xx) prior to any complex logic that could cause a timeout.
Example endpoint
This code snippet is a webhook function configured to check that the event type was received, to handle the event, and return a 200 response. Example code forhasVerifiedPayload() is available here.
Register your endpoint
Once your handler is deployed on the web and ready to go, register your endpoint with Brandfetch by creating a webhook using the GraphQL APIscreateWebhook mutation.
Registered webhook endpoint URLs must be publicly accessible HTTPS URLs.
Subscribe to brands by URN or domain
The final step is to subscribe to the objects (like brands) for which you want to receive events. You can subscribe to a few objects, or many thousands, one at a time or in batches. For example, perhaps you want to receive events for the Brandfetch brand. The URN for this brand isurn:brandfetch:brand:idL0iThUh6which means we would subscribe to that URN.
To add a subscription we need two things: The URN for the webhook we created ($webhookUrn: URN!) and the URN for the object to which we want to subscribe to ($subscriptions: [URN!]!).
Subscribe by domain
If all you have is a website, you can skip the brand lookup:addWebhookSubscriptions also accepts a domains argument of type [FQDN!], and resolves each domain to the brand registered for it before creating the subscription.
subscriptions is optional now, so one call can name domains, subscriptions, or both. A few things worth knowing:
- Subdomains resolve to the registrable domain, so subscribing to
blog.nike.comsubscribes you to the same brand asnike.com. - One call can name at most 1,000 entries, counted across
subscriptionsanddomainstogether. The cap counts the entries you send, so a brand named twice, once by URN and once by domain, counts twice towards it. - A brand named both ways is still subscribed once and charged once.
- A domain Brandfetch has not indexed yet is refused, and the error names the domains at fault (“No brand is registered for these domains yet: example.com”), so you can tell which entry of a batch was the problem. Nothing in the call is subscribed when that happens: correct the list and send it again.
- A call that names neither argument, or names only empty lists, is refused with “Provide at least one of
subscriptionsordomains.”
Edit a webhook
To change a webhook you already registered, send just the fields you want to change to theupdateWebhook mutation. A field you leave out, or set to null, keeps its current value.
events replaces the stored set rather than adding to it, so send the complete list of event types you want, not just the ones you are adding. An empty list is refused with “A webhook must subscribe to at least one event.”, because a webhook with no event types keeps its subscriptions but can never deliver anything.
Pause a webhook
Setenabled to false to stop deliveries without taking anything apart. The endpoint, its event types, and every one of its subscriptions stay exactly as they are, and nothing is delivered while the webhook is off. Set enabled back to true to resume.
An event that happens while the webhook is off is skipped rather than held back, so switching the webhook on again does not replay what it missed.
Re-enabling also clears the run of failures behind an automatic disable, so a webhook we switched off after 14 days without a single successful delivery starts again with a clean slate rather than resuming an outage you have already fixed.
When a request is declined
createWebhook and addWebhookSubscriptions check your plan and your billing status before they change anything. When a check fails, the mutation returns success: false with a code you can branch on and a message you can show, and nothing is created or subscribed. A call that goes through returns code: "success".
These codes come back on the mutation result. Problems with the request itself, such as an unknown domain, more than 1,000 entries in one call, or an empty
events list, come back as a GraphQL error instead, with a message naming what to fix.Unsubscribe from objects
If you no longer want to receive events for an object, remove it with theremoveWebhookSubscriptions mutation. As with subscribing, you can remove one or many subscriptions in a single call by passing multiple URNs.
Debugging delivery issues
To help debug your endpoint, or to later retrieve failed event deliveries when your endpoint has a long duration outage, you can review all of the events Brandfetch attempted to deliver to your webhook endpoint using the GraphQL API. Performing the following GraphQL query on the Webhooks API will return a list of all attempted webhook deliveries, responses from your endpoint, and the respective HTTP status codes we received. Delivery history is kept for 90 days after which time it is irreversibly deleted.Filter and page the history
deliveries takes three optional arguments:
filter narrows the history to a time window on createdAt. Both bounds are inclusive, and you can pass either on its own: omit from to start at the oldest delivery still retained, and omit to to end at the newest.
first sets the page size. It defaults to 100 and cannot exceed it. To read further back, pass the endCursor from pageInfo as after and repeat the query until hasNextPage is false. Every edge also carries its own cursor, so you can resume from a specific delivery rather than from the end of a page.
totalCount is the number of deliveries the webhook has recorded since it was created. It is a lifetime total rather than the size of the result: it counts deliveries that have aged out of the retention window, and a filter does not narrow it. To count the deliveries in a window, page through the window and count the edges.pageInfo.currentPage and pageInfo.totalPages are null here, because neither can be computed while paging by cursor. Use hasNextPage and endCursor to walk the history instead.