> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spott.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage webhooks with the API

> Create, change, rotate and delete your workspace's webhook endpoints from code, with an API key, instead of in Settings.

An integration that connects to many workspaces, or a team that keeps its setup in code,
can manage webhook endpoints through the [API](https://docs.spott.io/docs/developers/api-overview.md) instead of
in **Settings → Webhooks**. They are the same endpoints: one created through the API shows
up in Settings, and the other way round.

Each request acts on the endpoints of the workspace the API key belongs to, so an
integration that connects to several workspaces needs a key for each.

## Look up the event IDs

An endpoint subscribes to events by ID, not by name. The event pages in the sidebar show
each event's name and payload but not its ID, so
[List webhook events](https://docs.spott.io/api-reference/webhook-events/list-webhook-events.md)
(`GET /webhooks/events`) is where the IDs come from:

```bash theme={null}
curl 'https://api.gospott.com/webhooks/events?pageSize=100' \
  -H 'x-api-key: your_api_key'
```

Each item pairs an event's ID with its name. Two of them are shown here:

```json theme={null}
{
  "items": [
    {
      "id": "0a61bd1f-179a-5447-aea6-52333f7c64a0",
      "name": "placement.created",
      "description": "A new placement has been created"
    },
    {
      "id": "fea0c029-09b1-581f-a286-f3fa37fb46e7",
      "name": "placement.updated",
      "description": "A placement has been updated"
    }
  ]
}
```

With `pageSize=100` the whole catalog fits in one page today; if the response's
`pagination.hasNextPage` is `true`, ask for the next `page`. An event's ID is the same in
every workspace, so you can look the IDs up once and keep them in your code.

## Create an endpoint

Send the `https` URL that receives the deliveries, and the IDs of the events, to
[Create a webhook endpoint](https://docs.spott.io/api-reference/webhook-endpoints/create-a-webhook-endpoint.md)
(`POST /webhooks/endpoints`). This one subscribes to `placement.created` for an
invoicing sync:

```bash theme={null}
curl -X POST 'https://api.gospott.com/webhooks/endpoints' \
  -H 'x-api-key: your_api_key' \
  -H 'Content-Type: application/json' \
  -d '{
    "url": "https://example.com/spott/webhooks",
    "description": "Invoicing sync",
    "events": [{ "id": "0a61bd1f-179a-5447-aea6-52333f7c64a0" }]
  }'
```

```json theme={null}
{ "id": "ep_2zQh8KfR3tWmLxV9bNcY4pJsA1e", "secret": "whsec_k3H0r7c4..." }
```

The `secret` is how your receiver checks that a delivery comes from Spott. The API returns
it only here and when you rotate it, so put it in your secret store right away.
[Verify webhook deliveries](https://docs.spott.io/docs/developers/verify-webhooks.md) explains how your receiver
uses it.

## List and read endpoints

[List webhook endpoints](https://docs.spott.io/api-reference/webhook-endpoints/list-webhook-endpoints.md)
(`GET /webhooks/endpoints`) returns the workspace's endpoints a page at a time: pass the
`pagination.cursor` of a response as `cursor` to get the next page, until it is `null`.
[Retrieve a webhook endpoint](https://docs.spott.io/api-reference/webhook-endpoints/retrieve-a-webhook-endpoint.md)
(`GET /webhooks/endpoints/{id}`) returns one. Neither includes the secret.

An endpoint whose `events` is `null` receives every event. Only **Settings → Webhooks** can
set that; to narrow it down through the API, send a list of events.

## Change an endpoint

[Update a webhook endpoint](https://docs.spott.io/api-reference/webhook-endpoints/update-a-webhook-endpoint.md)
(`PATCH /webhooks/endpoints/{id}`) changes only the fields you send. The `events` you send
replace all the endpoint's events, so to add or remove one, read the endpoint first, edit
its list, and send the whole list back. Leave out any event that List webhook events no
longer returns, or the request fails.

The endpoint above subscribes to `placement.created`. To also get `placement.updated`,
send both:

```bash theme={null}
curl -X PATCH 'https://api.gospott.com/webhooks/endpoints/ep_2zQh8KfR3tWmLxV9bNcY4pJsA1e' \
  -H 'x-api-key: your_api_key' \
  -H 'Content-Type: application/json' \
  -d '{
    "events": [
      { "id": "0a61bd1f-179a-5447-aea6-52333f7c64a0" },
      { "id": "fea0c029-09b1-581f-a286-f3fa37fb46e7" }
    ]
  }'
```

To stop deliveries for a while, send `"disabled": true`, and `false` to start them again.
The endpoint keeps its ID and its secret.

Whichever change comes last wins, made through the API or in Settings.

## Rotate the secret

Rotate a secret when it may have leaked, when someone who knew it leaves, or on a fixed
schedule, with
[Rotate the secret of a webhook endpoint](https://docs.spott.io/api-reference/webhook-endpoints/rotate-the-secret-of-a-webhook-endpoint.md)
(`POST /webhooks/endpoints/{id}/secret/_rotate`).

A rotation does not have to cost a single delivery. For a while after you rotate, the
**overlap**, Spott signs every delivery with both the old and the new secret. Your receiver
needs only one of them: one that still has the old secret and one that already has the new
secret both pass. So you can switch your receivers over at any moment during the overlap,
one server at a time if you deploy that way.

**1. Rotate**

`overlapMs` sets how long the old secret keeps working; this example keeps it for one
hour:

```bash theme={null}
curl -X POST 'https://api.gospott.com/webhooks/endpoints/ep_2zQh8KfR3tWmLxV9bNcY4pJsA1e/secret/_rotate' \
  -H 'x-api-key: your_api_key' \
  -H 'Content-Type: application/json' \
  -d '{ "overlapMs": 3600000 }'
```

```json theme={null}
{ "secret": "whsec_Qm9yZ2x1..." }
```

**2. Store the new secret**

Put it in your secret store straight away. As on create, the API cannot return it
again.

**3. Deploy it to your receivers**

Do this before the overlap ends. Until then, deliveries pass with either secret.

**4. Let the overlap end**

After that, deliveries are signed only with the new secret, and the old one no longer
works. There is nothing to clean up.

Make the overlap longer than your slowest deploy, approvals included. Leave `overlapMs` out
and the old secret keeps working for 24 hours.

### When the secret has leaked

Send `"overlapMs": 0` to stop the old secret at once. From then on, deliveries fail your
signature check until your receivers have the new secret: your receiver turns them away,
they count as failed deliveries, and Spott tries them again later. So deploy the new
secret as fast as you can.

That stops only the secret you are replacing. If you rotated shortly before, the secret
from before that rotation keeps working until its own overlap ends.

## Delete an endpoint

[Delete a webhook endpoint](https://docs.spott.io/api-reference/webhook-endpoints/delete-a-webhook-endpoint.md)
(`DELETE /webhooks/endpoints/{id}`) removes it, and deliveries to it stop. If you create a
new endpoint for the same URL, it gets a new ID and a new secret, so update your receiver
with that secret.

## Troubleshooting

| Status | Message | What to do |
| - | - | - |
| `400` | **Validation failed**, with **`<id> is not the ID of a webhook event`** under `errors` | The ID is not one that List webhook events returns, for example a typo or an event that is no longer in the catalog. Send only IDs from that list. |
| `400` | **The maximum number of webhook endpoints has been reached.** | Delete an endpoint you no longer use, then create the new one. |
| `400` | **The webhook provider rejected the endpoint configuration.** | The response lists the reasons under `details.providerErrors`. Correct those fields and try again. |
| `404` | **The webhook endpoint could not be found.** | Check the ID. The endpoint may have been deleted, also in Settings. |
| `429` | **The webhook provider is rate limiting requests.** | Wait a moment and try again. A `429` with another message comes from Spott's own [rate limits](https://docs.spott.io/docs/developers/api-overview.md#rate-limits-and-pagination). |
| `503` | **The webhook provider is temporarily unavailable.** | Try again later. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.