Webhooks

Send an HTTP POST to any URL when documents change, with a body built from a Handlebars template, and inspect every delivery.

A webhook posts to a URL whenever a change happens in a collection, for example when a document is inserted into a bucket. Use webhooks to notify Slack, start a CI build or call another service, without writing a function.

A webhook in the panel, showing its trigger, active state, body template and delivery logs

Create a webhook

Terminal
curl -X POST https://api.example.com/api/webhook \
  -H "Authorization: IDENTITY <token>" -H "Content-Type: application/json" \
  -d '{
    "title": "Notify Slack on new post",
    "url": "https://hooks.slack.com/services/T000/B000/XXXX",
    "body": "{\"text\": \"New post: {{document.slug}}\"}",
    "trigger": {
      "name": "database",
      "active": true,
      "options": {"collection": "bucket_6b0b1968a5611972c29c25f7", "type": "INSERT"}
    }
  }'
FieldDescription
titleName shown in the panel.
urlWhere to send the request.
bodyA Handlebars template for the request body.
trigger.nameAlways database.
trigger.activePause the webhook with false.
trigger.options.collectionThe collection to watch. Bucket data is stored in bucket_<bucketId>.
trigger.options.typeINSERT, UPDATE, REPLACE or DELETE.

GET /webhook/collections lists the collections you can watch, with readable names for buckets:

JSON
[{"id": "bucket_6b0b1968a5611972c29c25f7", "slug": "Posts"}, {"id": "identity", "slug": "identity"}]

The request

Spica sends a POST with Content-Type: application/json and User-Agent: Spica/Webhooks; (https://spicaengine.com/docs/guide/webhook). The body template is rendered with:

VariableContents
typeThe operation: insert, update, replace or delete.
documentKeyThe id of the document.
documentThe document after the change, or before it for deletes.

Use the toJSON helper with triple braces to embed objects:

Text
{"event": "{{type}}", "id": "{{documentKey}}", "post": {{{ toJSON document }}}}

Templates are rendered in strict mode: referencing a missing property fails the delivery and records the error.

Delivery logs

Every delivery is logged with the request, the response status and body, and whether it succeeded (status below 400).

EndpointDescription
GET /webhook/logs?webhook=<id>Logs of a webhook. Filter with succeed=true or false, begin, end, skip, limit.
DELETE /webhook/logs/:idDelete one log entry.
DELETE /webhook/logsClear logs.

Manage webhooks

EndpointAction
GET /webhookwebhook:index
GET /webhook/:idwebhook:show
POST /webhookwebhook:create
PUT /webhook/:idwebhook:update
DELETE /webhook/:idwebhook:delete

Webhooks or functions?

Use a webhook whenUse a function when
You only forward the change to a URL.You need logic, conditions or several calls.
The receiver accepts a simple JSON body.You need authentication beyond a URL token, retries or transformations.

Webhooks are fire-and-forget: a failed delivery is logged but not retried.