# Webhooks

> Running and debugging · https://spoletta.com/docs/webhooks

GitHub, Slack and other systems can't call the API, but they can send their events. A webhook gives a flow a URL of its own that a service sends its events to, signed with the webhook's secret, and starts a run on each event, given what the webhook takes from its payload.

## Adding a webhook

1.  **Open the flow's settings**
    
    **Views** → **Flow settings**, then **Webhooks** → **Add a webhook**. Whoever may change the flow adds them.
    
2.  **Say what sends it**
    
    Give it a name, such as *Issues labeled*, and pick **Sent by**: **GitHub**, **Slack** or **Generic**. See the table below for each.
    
3.  **Say what the flow is given**
    
    Map each object the flow starts from onto the payload. See below.
    
4.  **Give the sender its URL and secret**
    
    The webhook's URL, `https://spoletta.example.com/hooks/…`, has **Copy** beside it. A secret Spoletta makes, starting `spwh_`, is shown once, as it is made: *Copy it now: it isn't shown again.*
    

| Sent by | Set up |
| --- | --- |
| GitHub | In the repository's or organization's settings, under Webhooks, add one with the URL, the content type `application/json` and the secret. **Events it takes** names each event alone, such as `pull_request`, or with its action, such as `issues.labeled`; it answers the others and starts nothing. GitHub signs each delivery's body. |
| Slack | In the Slack app's settings, under Event Subscriptions, give the URL as the Request URL, which Slack checks at once, and subscribe to the events. Paste the app's signing secret, from its Basic Information page. **Events it takes** names them by type, such as `app_mention`; none takes every event. |
| Generic | Any other system. **Each delivery carries** either **An HMAC of its body**: an HMAC-SHA256 keyed with the secret, in hex or base64, in the header you name, covering the timestamp and the delivery's id when you name headers for those; or **The secret** itself in a header, such as `Authorization: Bearer …`. |

## What the flow is given

Each object the flow starts from, those a process on its top board takes and none generates, is taken from the event's payload:

-   **A path** into it: `issue.title`, `pull_request.number`, `labels.0.name`. For a list, the path leads to an array of its items.
-   **The whole payload**.
-   **A path for each attribute**, for an object with attributes: `number` from `issue.number`, `title` from `issue.title`.

Once a delivery comes, its payload shows beside the mapping, to write the paths from. What the webhook takes is checked as an API request's [inputs](https://spoletta.com/docs/api.md#what-a-flow-takes) are. So a flow meant to run when an issue is labeled has its first process take an *Issue* object with the attributes it needs, nothing generating it, and its webhook maps them from the payload. To try such a flow by hand, use [Start with inputs](https://spoletta.com/docs/running.md#starting-with-inputs).

## How its runs go

-   A webhook's run takes the version the flow [published](https://spoletta.com/docs/versions.md#publishing), or its board while it publishes none. The run list marks it *A webhook*.
-   Each event starts one run, however often the service sends it: a GitHub delivery is known by its body, a Slack one by its event id, a generic one by the id in the header you name. A generic webhook signing no id, or checking the secret itself, takes a delivery sent again as a new one. A flow that must never handle an event twice gives what it starts from a [unique](https://spoletta.com/docs/objects.md#stored-objects) attribute.
-   Its runs are for whoever added it, so their limits and access apply, as they are in the project now: once they are taken out of it, or only see it, the webhook refuses its deliveries, and an archived project's webhooks answer as if they weren't there. See [What runs for you](https://spoletta.com/docs/project-access.md#on-your-behalf). Whoever changes what starts its runs (its kind, secret, headers, events or mapping), or turns it back on, makes them theirs: *Its runs are Ada's. Changing anything but its name makes them yours.*
-   A delivery the flow can't take, such as one whose payload lacks a path, is refused and listed with why, and can be sent again once that is put right.

## Following its deliveries

Each webhook in the list shows its last delivery and what came of it, and how many requests were refused as not signed with its secret. **Deliveries** lists the latest: when, which event, and what came of it, *Started* with its run, *Sent again*, *Ignored* or *Refused*, with why. Turn a webhook off and on, edit it, or remove it; its runs stay.

The sender is answered at once, as soon as the run is recorded:

| Status | Means |
| --- | --- |
| 202 | A run started: `{"run": 418, "url": "…"}`. A delivery sent again gets 200 with the first's run. |
| 200 | Ignored, with why, such as an event it doesn't take or the webhook turned off: `{"ignored": "…"}` |
| 400 | Something wrong with the payload, such as one that isn't JSON or a timestamp more than 5 minutes off |
| 401 | Not signed with the webhook's secret |
| 403 | Whoever added it was removed, or may no longer run the flow |
| 404 | No such webhook, or its flow is in the trash |
| 413, 429 | A delivery larger than 1 MB; too many requests, sent too quickly |

## Keeping it safe

-   The URL alone starts nothing: every delivery must be signed with the secret, which is sealed with the instance's [secret key](https://spoletta.com/docs/sign-in.md#secrets) and never shown again, nor is the payload it kept.
-   A Slack delivery, and a generic one signing a timestamp, is refused when its timestamp is more than five minutes off, so it can't be sent again later. A generic one carrying the secret itself sends it with every delivery, and nothing tells one sent again from a new one: prefer an HMAC with an id and a timestamp.
-   Every delivery goes in the [audit log](https://spoletta.com/docs/security.md#audit), never what it carried or the secret.
-   Webhooks aren't in [setup files](https://spoletta.com/docs/setup-files.md): they hold a secret and run as whoever added them, so they are added on each instance. On an instance without sign-in they are nobody's, and stop working once people sign in.
