Basic webhook
Define a webhook capability on your worker like this:The event object
Theexecute function receives an array of WebhookEvent objects. The array currently contains one event, but may contain multiple events in the future.
Webhook URLs
Webhook URLs include a unique ID that acts as a shared secret:Verify requests
Most webhook providers can sign requests with a shared secret. Store the signing secret as a worker secret, verify each request usingevent.rawBody and event.headers, and throw WebhookVerificationError when verification fails:
.env file:
Execution and retries
When a webhook request reaches Notion, Notion validates the URL, enqueues the event, and responds with202 Accepted. Your worker runs asynchronously after the HTTP response is sent.
If your handler throws WebhookVerificationError, Notion records a verification failure and does not retry that event. If your handler throws another error, Notion retries the worker run up to 3 times.
Successful runs reset the consecutive verification failure counter.
Use Notion from a webhook
Webhook handlers receive the same context object as other capabilities, includingcontext.notion, the Notion API SDK client:
context.notion is not automatically authenticated. To call the Notion API, create an internal integration, give it access to the relevant pages or databases, and store the integration token in NOTION_API_TOKEN:
context.notion reads process.env.NOTION_API_TOKEN and uses it as the Notion API client token.
For more information about creating an integration token for a worker, see Using Notion API from a worker.
Inspect runs
Use worker run logs to debug webhook executions:ntn workers flags and options.
Next steps
Secrets
Store webhook signing secrets and API keys.
Notion API
Read and write Notion data from a webhook handler.
OAuth
Authenticate with third-party APIs from your webhook.
SDK reference
Detailed API docs for worker.webhook() and WebhookVerificationError.