---
title: "Webhooks"
description: "Events Block-Auth sends to your backend, the request format, how to verify the signature and how delivery works."
url: "/webhooks/"
language: "en"
---

# Webhooks

Block-Auth calls your backend when something changes outside your app: a user closes their session or unlinks their account from Block-Auth Identity. Use it to end your own session or remove the link.

## Set up the URL

In the Dashboard, open your app and fill in the third URL field. It is labeled **When user fails to log in**, but it is the webhook URL. See [Webhook URL](/integration-quickstart.md#webhook-url).

- Write a path, such as `/auth/blockauth/webhook`, or a full URL. A path is sent to `https://` plus your app's domain.
- The URL must use `https` and be on your app's validated domain. Any other host is ignored.
- Webhooks only go to your app's own domain, not to the automatic development domain (`*.block-auth.io`). To test them, use an app whose domain points to a server you control.
- Don't use `/signout/callback`: it's the default value and means "no webhook".

## Events

| `event`            | When                                                                                                                                                                                | What to do                                                                           |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| `signout`          | The user closes their session for your app from Identity or the widget, or your backend calls [`/api/auth/signout`](/api-user-login.md) and the user had sessions on other devices. | End that user's session in your app.                                                 |
| `account.unlinked` | The user removes the link between their Block-Auth identity and their account in your app, from Identity.                                                                           | Delete the link for `externalId`. The next login with Block-Auth asks to link again. |

When your backend unlinks an account with [`/api/auth/link/revoke`](/api-user-login.md), no webhook is sent: you already know.

## The request

A `POST` with a JSON body and two headers:

| Header                  | Value                                   |
| ----------------------- | --------------------------------------- |
| `X-BlockAuth-Timestamp` | Unix time in seconds when it was sent.  |
| `X-BlockAuth-Signature` | `v1=` followed by the signature in hex. |

**signout**

```json
{
  "event": "signout",
  "address": "0x7f85…46d8",
  "externalId": "user-1234",
  "sessionId": "8f1c…",
  "reason": "signed_out",
  "apiKey": "your_api_key",
  "iat": 1762851600,
  "jti": "3b9e0f4c2a…"
}
```

**account.unlinked**

```json
{
  "event": "account.unlinked",
  "address": "0x7f85…46d8",
  "externalId": "user-1234",
  "apiKey": "your_api_key",
  "iat": 1762851600,
  "jti": "3b9e0f4c2a…"
}
```

> **Placeholders in this code:**
>
> - Replace `your_api_key` with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.

- `address` is always lowercase.
- `externalId` is your own user ID if the user is linked, or `null`.
- `sessionId` is `null` when the signout comes from your own `/api/auth/signout` call.
- `jti` is unique per request: use it to ignore a request you have already processed.

## Verify the signature

Never trust a webhook you haven't verified. The signature is an HMAC-SHA256 of `timestamp.body` (the timestamp header, a dot and the raw body), keyed with the [SHA-256 hash of your `apiSecret`](/api-intro.md#hash-secret).

1. Read the **raw** body, before parsing JSON.
2. Reject it if the timestamp is more than 5 minutes away from your clock.
3. Compute the signature and compare it with `X-BlockAuth-Signature` in constant time.
4. Check that `apiKey` is yours.

With the [Block-Auth package routes](/sdk-integration-spa-webhooks.md#webhooks) you don't write any of this: `POST /webhook` does the four steps, and its `isReplay` and `isRevoked` options handle repeated deliveries and closing sessions. If you write the route yourself, `verifyWebhook` in `AuthCodeClient` does steps 1 to 3 in every language. The .NET SDK also checks `apiKey`; in Node.js, PHP and Python, check it yourself:

> How you add the backend login routes: this page covers “With the Block-Auth package” or “My own implementation”. Both variants are written out below, each one marked. Follow only the one that matches the app.

### How you add the backend login routes: With the Block-Auth package.

**Node.js**

```js
const event = blockauth.verifyWebhook(
  req.body,
  req.get('X-BlockAuth-Timestamp'),
  req.get('X-BlockAuth-Signature'),
)
if (event.apiKey !== process.env.BLOCKAUTH_API_KEY) return res.status(400).end()
```

**PHP**

```php
$event = $blockauth->verifyWebhook(
    file_get_contents('php://input'),
    $_SERVER['HTTP_X_BLOCKAUTH_TIMESTAMP'] ?? '',
    $_SERVER['HTTP_X_BLOCKAUTH_SIGNATURE'] ?? '',
);
if (($event['apiKey'] ?? null) !== getenv('BLOCKAUTH_API_KEY')) {
    http_response_code(400);
    exit;
}
```

**Python**

```python
event = blockauth.verify_webhook(
    request.get_data(),
    request.headers.get("X-BlockAuth-Timestamp", ""),
    request.headers.get("X-BlockAuth-Signature", ""),
)
if event.get("apiKey") != os.environ["BLOCKAUTH_API_KEY"]:
    abort(400)
```

**.NET**

```csharp
var evt = blockauth.VerifyWebhook(
    rawBody,
    request.Headers["X-BlockAuth-Timestamp"],
    request.Headers["X-BlockAuth-Signature"]);
```

### How you add the backend login routes: My own implementation.

Without any SDK, in Node.js:

**Node.js**

```js
import { createHash, createHmac, timingSafeEqual } from 'node:crypto'

const secretHash = createHash('sha256')
  .update(process.env.BLOCKAUTH_API_SECRET)
  .digest('hex')

export function verifyBlockAuthWebhook(rawBody, timestamp, signature) {
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return null
  const expected = `v1=${createHmac('sha256', secretHash)
    .update(`${timestamp}.${rawBody}`)
    .digest('hex')}`
  let a = Buffer.from(expected)
  let b = Buffer.from(String(signature))
  if (a.length !== b.length || !timingSafeEqual(a, b)) return null
  let event = JSON.parse(rawBody)
  return event.apiKey === process.env.BLOCKAUTH_API_KEY ? event : null
}
```

The full endpoint for each language is in the guides: [React, Vue, Angular](/sdk-integration-spa-webhooks.md#webhooks) and [PHP, Python, .NET, Node.js](/sdk-integration-server-webhooks.md#webhooks).

## Delivery

- Block-Auth sends each event **once**. There are no retries.
- Your endpoint has **5 seconds** to answer. Redirects are not followed.
- Answer `2xx` as soon as the signature is valid, and do the work after.

Because a webhook can be lost if your server is down, don't rely on it alone: your session should also end at [`signedExpiredAt`](/security.md#session).
