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

# An integration that sets up its own webhooks

> For a tool that many groups connect: add your webhook endpoint with the group’s key, check it works, and rotate its secret, so no admin has to paste an address into Scoutworks.

> [!NOTE]
>
> * **For:** anyone building a tool that several groups connect to, such as a district volunteer’s dashboard or a booking system
> * **You get:** an endpoint your tool adds to each group itself, tested, with its signing secret stored
> * **Time:** about twenty minutes
> * **Scopes:** `webhooks:read`, `webhooks:write`, and the read scope of each event you want, such as `members:read` for `member.created`

When a group connects your tool, it gives you an API key. Without this recipe, a group admin then also has to open **Settings → Integrations → Webhooks**, paste your address and tick the right events. With `webhooks:write` on the key, your tool does that itself, the same way for every group.

> [!WARNING]
>
> There is no dry run, and the sandbox key cannot change anything. Use your own key, and try each step first on a test address of your own, such as a [webhook.site](https://webhook.site) URL, before you point it at your real receiver.

## 1. Ask for the right scopes

Tell each group admin which scopes to tick when they create the key for your tool:

* `webhooks:read` and `webhooks:write`, to manage the endpoint;
* the read scope of every event you want. An endpoint can receive only events whose records the key can read: `member.created` needs `members:read`, `event.updated` needs `events:read`. The [event list](https://developers.scoutworks.app/guides/webhooks.md#events) gives each one’s scope. Payment events cannot be chosen with a key: an admin adds those in Scoutworks.

Check what a key can do with `GET /me` before you go on:

```bash theme={null}
curl "https://api-demo.scoutworks.app/api/v1/me" \
  -H "Authorization: Bearer sw_test_473adcc6c7236916021f0f24559db36576a53bab071d0f0c08a025adb510b6ab"
```

## 2. Look for an endpoint you added before

A group may connect your tool twice, or reconnect it. List its endpoints first, and reuse yours if it is there:

```bash theme={null}
curl "https://api-demo.scoutworks.app/api/v1/webhook-endpoints?limit=20" \
  -H "Authorization: Bearer sw_test_473adcc6c7236916021f0f24559db36576a53bab071d0f0c08a025adb510b6ab"
```

Match on your own address. A group holds at most ten endpoints, so a tool should never add a second one of its own. Your key sees only endpoints whose every event it can read: one an admin set up for payment events, say, is left out, and answers `404`.

## 3. Add the endpoint

```bash theme={null}
curl -X POST https://api.scoutworks.app/api/v1/webhook-endpoints \
  -H "Authorization: Bearer $SCOUTWORKS_KEY" \
  -H "Idempotency-Key: 6a1f0c2e-8d7b-4e3a-9c5f-1b2d3e4f5a6b" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://hooks.example.com/scoutworks/PASTE_GROUP_ID", "description": "Our tool: new members", "eventTypes": ["member.created", "member.left"]}'
```

The answer carries the endpoint and its signing **`secret`**, which starts `whsec_`. **Store the secret now**, against the group, in your tool’s secret store: it is never shown again. If the request times out, send it again with the same `Idempotency-Key`: you get the same endpoint, without the secret, and no second endpoint is made. Then rotate the secret (step 5) to get one you can store.

Put the group’s id in the address, as above, so your receiver knows which group’s secret checks each message.

## 4. Check it works

Send a test event:

```bash theme={null}
curl -X POST https://api.scoutworks.app/api/v1/webhook-endpoints/PASTE_ENDPOINT_ID/test \
  -H "Authorization: Bearer $SCOUTWORKS_KEY" \
  -H "Idempotency-Key: 0b9c8d7e-6f5a-4b3c-8d2e-1f0a9b8c7d6e"
```

A `webhook.test` message reaches your receiver within a few seconds. [Check its signature](https://developers.scoutworks.app/guides/webhooks.md#check-the-signature) with the secret you stored. Then read the delivery to see what your receiver answered:

```bash theme={null}
curl "https://api.scoutworks.app/api/v1/webhook-endpoints/PASTE_ENDPOINT_ID/deliveries?limit=5" \
  -H "Authorization: Bearer $SCOUTWORKS_KEY"
```

A `status` of `delivered` means it worked. `failed`, or a `lastHttpStatus` other than `2xx`, means your receiver refused it: the `attempts` show what it answered.

## 5. Rotate the secret

Rotate each group’s secret on a schedule, and at once if you think one has leaked:

```bash theme={null}
curl -X POST https://api.scoutworks.app/api/v1/webhook-endpoints/PASTE_ENDPOINT_ID/rotate-secret \
  -H "Authorization: Bearer $SCOUTWORKS_KEY" \
  -H "Idempotency-Key: 4d3c2b1a-0f9e-4d8c-a7b6-5e4d3c2b1a0f"
```

The answer carries the new `secret`. Store it. The old secret keeps signing beside the new one for 24 hours, and each message carries both signatures, so your receiver can switch over without missing a message.

## 6. Look after it

* **It stops working with the key’s creator.** An endpoint added with a key belongs to the admin who created the key. If they stop being a group admin, the endpoint stops sending (`status: owner_inactive`), as the key stops working. Ask the group for a new key, delete the old endpoint and add it again.

* **A failing endpoint is switched off** after five days of failed tries (`status: disabled`, `disabledReason: failing`). Fix your receiver, then switch it back on:

  ```bash theme={null}
  curl -X PATCH https://api.scoutworks.app/api/v1/webhook-endpoints/PASTE_ENDPOINT_ID \
    -H "Authorization: Bearer $SCOUTWORKS_KEY" \
    -H "Content-Type: application/json" \
    -d '{"enabled": true}'
  ```

* **When a group disconnects your tool,** delete your endpoint with `DELETE /webhook-endpoints/{endpointId}`.

* **Catch anything you missed.** A message can go astray. Run [Keep a copy in sync](https://developers.scoutworks.app/guides/keep-a-copy-in-sync.md) now and then: `GET /changes` reports every change, webhook or not.

## Switch to your group

Every write here goes to `api.scoutworks.app` with the group’s own key, which needs the scopes in step 1. Scoutworks records each change as made by the admin who created the key, and marks it with the key’s name, so the group can see in its webhook settings what your tool did. Webhooks cannot be added in the demo.

## If something goes wrong

* **`403 insufficient_scope`** on add or change: the key lacks `webhooks:write`, or the read scope of one of the events. The message names the events.
* **`400 invalid_request`:** the address must start `https://` and use a public domain name, not an IP address.
* **`409 conflict`** on add: the group already has ten endpoints. On a test: the endpoint is switched off.
* **`412 stale_write`:** someone changed the endpoint since you read it. Read it again, then retry with its new `ETag`.

See [Errors](https://developers.scoutworks.app/guides/errors.md) for every code.


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