integration-routes
What it does
integration-routes (.framework/functions/integration-routes.ts) is the inbound webhook surface. An external caller POSTs a provider payload to a named integration; the function authenticates the caller's API key, resolves the integration's registered handler through the webhook_handlers registry, lets the adapter normalize the payload into an item shape, creates the record through admin-data (full permission/type/audit pipeline), and records the whole attempt as a webhook_deliveries row (direction: 'inbound').
The execute sequence:
- Read
slugfrom?slug=orbody.slug; require a machine principal —ctx.principal.idmust match anapi_keysrow. - Load the
integrationsrow matchingslugandapiKey.account_id(inbound integrations are always account-scoped here),is_active. - Enforce key pinning:
apiKey.integration_idmust equal the integration id when set;apiKey.app_idmust equalintegration.app_idwhen both set. - Extract the event name —
x-event-typeheader first, elseintegration.config.event_path(a dot-path into the payload). - Resolve the handler name
config.handler_name || provider || 'generic-webhook'throughresolveHandler(.framework/functions/_shared/ webhook-registry.ts): awebhook_handlersrow mapsname → function_name, scoped by(app_id, account_id)with a globalNULL/NULLfallback; the module is dynamically imported (custom/…paths resolve into the custom tree) and must exportadapt,handler, or a default function. - Insert the inbound
webhook_deliveriesrow (payload,api_key_id,handler_name,external_id: body.id), emitintegration.inbound.started, invoke the adapter, create theitemsrecord via admin-datacreate, and mark the deliverydelivered.
Route and methods
/api/integration-routes?slug=<integration-slug>—public/_redirectsmaps/api/*to/.netlify/functions/:splat; the default export iscreatePublicHandler(execute).executeperforms no method check — any HTTP method with a JSON body works; in practice providers POST.
Authentication
createPublicHandler resolves a principal but does not reject anonymous requests — execute itself enforces:
ctx.principal.idmust exist —401 Machine principal requiredotherwise (JWT-only human callers have a person id, not an api-key id, so they fail at the next check).- The id must match an
api_keysrow —401 Invalid API keyotherwise. - The key's
account_idscopes the integration lookup; a key pinned tointegration_idorapp_idgets403for anything else.
There is no HMAC/signature verification — the API key sent as x-api-key is the entire credential. Prefer keys pinned to integration_id/app_id and rotate them like any secret. See auth and API keys.
Request contract
| Input | Where | Notes |
|---|---|---|
slug | query ?slug= or body.slug | Integration slug, required. |
x-api-key | header | Machine credential; resolved by resolvePrincipal. |
x-event-type | header | Optional event name; overrides config.event_path extraction. |
| payload | JSON body | Passed verbatim to the adapter; body.id is stored as the delivery's external_id. |
The adapter returns:
{ type_slug: string, data: Record<string, any>, title?: string,
status?: string, thread?: any, links?: any[], external_id?: string,
error?: string, retryable?: boolean }type_slug is required for record creation. thread and links are declared in the contract but currently no-ops (TODO in source).
Response contract
Success returns the admin-data create result — the created items record — inside the standard { data, error, meta } envelope, and the delivery is marked delivered with response_status: 200.
Adapter-reported errors (adapterResult.error) throw with the adapter's message; retryable: true marks the delivery pending (with a backoff next_attempt_at) before the throw — though see Agent notes: the outer catch re-marks every post-insert error pending, and nothing re-delivers inbound rows anyway.
Side effects
webhook_deliveriesinsert (direction: 'inbound',api_key_id,handler_name,payload,external_id) then status updates.- One
itemsrow created viaadmin-datacreateunder a synthesized context (adminDb,accountId: integration.account_id,principal.id: null) — type resolution, validation, triggers, and audit all run. - Audit events:
integration.inbound.started,.delivered,.adapter_retryable,.adapter_failed,.error.
Errors
| Status | Condition |
|---|---|
| 400 | Integration slug is required |
| 401 | Machine principal required · Invalid API key |
| 403 | API key is not authorized for this integration · … for this app |
| 404 | Integration not found: <slug> · Handler not found: <name> |
| 500 | Failed to create inbound delivery record · Adapter result is missing type_slug · adapter/admin-data errors propagate |
Agent notes
- Inbound retry is not implemented.
retryPendingDeliveries(.framework/functions/_shared/retry-deliveries.ts) counts inbound rows asinbound_failedanddeliveries?action=retryrejects non-outbound deliveries outright. Worse, thecatchinexecutere-marks any error after the delivery insert aspendingwithnext_attempt_at— including non-retryable adapter errors and admin-data create failures — so failed inbound deliveries sitpendingforever. When debugging "stuck" inbound rows, checklast_error, not juststatus. registerWebhookHandler/deregisterWebhookHandler(.framework/functions/_shared/webhook-registration.ts) manage thewebhook_handlerstable — upsert on(app_id, account_id, name); deregister soft-deletes. Migration004seeds thegeneric-webhookhandler (adapters/generic-webhook-inbound), which stores the raw payload as awebhook_eventitem.- The lookup is cached per
(appId, accountId, handlerName)in-process — newly registered handlers may not resolve until a cold start. - Adapters run inside the function with a console
loggerand receive the raw headers — they can do their own signature checks if the provider requires it, but nothing enforces that. - See integrations and webhooks for the full mental model.