# Webhooks

> Have Staffedly POST to your server when a shift, open shift, time off, clock record, person, credential, document or signing request changes, and check the signature.

A webhook tells your server when something changes, so you do not have to poll the API. You give Staffedly an address, choose what to hear about, and each change arrives as a signed `POST`.

![Settings, API: the Webhooks card with an address, its state and the form to add one](https://staffedly.com/docs/img/developers/webhooks.webp)

## Who can set one up

Admins, in **Settings → API**, under **Webhooks**. The organization's plan must include the API: Pro, Agency, Enterprise or the free trial. An organization can have up to 5 webhook addresses.

## Add an address

1. Open **Settings → API** and find **Webhooks**.
2. Enter the address. It must be `https` and reachable from the public internet.
3. Under **Tell me about**, tick the kinds of change you want. See [Webhook events](https://staffedly.com/docs/developers/webhook-events).
4. Click **Add address**.
5. Copy the signing secret straight away. It starts with `whsec_` and is shown once.
6. Click **Send a test**. Staffedly sends a signed `ping` event and shows what your server answered.

Events start with the next change after the address was added. Existing records are not replayed.

## What arrives

One `POST` for each event, with a JSON body:

```json
{
  "id": "evt_…",
  "type": "open_shift.filled",
  "createdAt": "2027-01-11T14:02:11.000Z",
  "organizationId": "cmwwzj8f40000qrmrky4ouqvg",
  "data": { "id": "…", "facilityId": "…", "start": "2027-01-12T12:00:00.000Z", "end": "2027-01-12T20:00:00.000Z", "role": "CRNA", "status": "filled" }
}
```

`data` is the record in the same shape the API returns for it, so nothing reaches a webhook that the API would not return.

These headers come with it:

| Header | Holds |
| --- | --- |
| `Staffedly-Signature` | `t=<unix seconds>,v1=<signature>` |
| `Staffedly-Event` | The event type, for example `open_shift.filled` |
| `Staffedly-Event-Id` | The event's id. Use it to ignore a repeat |
| `Staffedly-Delivery` | The id of this delivery attempt |
| `User-Agent` | `Staffedly-Webhooks/1.0` |

## Check the signature

Do this before trusting a message.

1. Read `t` and `v1` from the `Staffedly-Signature` header.
2. Compute an HMAC-SHA256, keyed with your signing secret, over the text `t`, a full stop, and the raw request body exactly as received.
3. Compare the result, as lowercase hex, with `v1` using a constant-time comparison.
4. Refuse the message if `t` is more than 5 minutes from your clock.

```js
import crypto from "node:crypto";

// rawBody: the request body as a string, before any JSON parsing
export function isFromStaffedly(secret, rawBody, header, toleranceSeconds = 300) {
  const m = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header ?? "");
  if (!m || Math.abs(Date.now() / 1000 - Number(m[1])) > toleranceSeconds) return false;
  const expected = crypto.createHmac("sha256", secret).update(`${m[1]}.${rawBody}`).digest("hex");
  return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(m[2]));
}
```

```python
import hashlib, hmac, re, time

def is_from_staffedly(secret: str, raw_body: bytes, header: str, tolerance: int = 300) -> bool:
    m = re.fullmatch(r"t=(\d+),v1=([0-9a-f]{64})", header or "")
    if not m or abs(time.time() - int(m.group(1))) > tolerance:
        return False
    expected = hmac.new(secret.encode(), m.group(1).encode() + b"." + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, m.group(2))
```

## Answer, retries and order

- Answer with any `2xx` status within 10 seconds. The body of your answer is not read. Do the real work after you have answered.
- A redirect counts as a failure. Redirects are not followed.
- After a failure the whole address waits 1, 5, 15, 30 and then 60 minutes between attempts. Its events stay queued in order behind the one that failed.
- After three days with nothing accepted, the address is switched off, the reason is shown on the screen, and its queue is dropped. Switch it back on when your server is ready; it starts clean from the next change.
- Delivery is at least once. The same event can arrive more than once, so ignore an event id you have already handled.
- Changes are noticed by a pass that runs about every 15 seconds, so an event usually arrives well within a minute of the change.

## See what was sent

**Show recent deliveries** on an address lists the latest attempts with the event, the result and your server's status. Delivery records are kept for 14 days.

## Switch off or remove

**Switch off** stops sending and nothing is queued while it is off. **Remove** deletes the address and its delivery history.

## Addresses that are refused

For safety the address must use `https`, must not carry a username or password, and must not point at a private or local network. A name such as `localhost` or one that resolves to a private address is refused when you save it and again each time a message is sent.

To try webhooks from a laptop, use a tunnel that gives your local server a public `https` address.

## Questions

### Are there "deleted" events?

No. Records are cancelled, voided or archived, not deleted, and those arrive as an update or a cancel event. See [Webhook events](https://staffedly.com/docs/developers/webhook-events).

### Does publishing a schedule send one event?

It sends one `assignment.created` event for each shift published. A month's schedule produces many events, in order.

### Can I get the secret again?

No. Remove the address and add it again to get a new secret.
