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

# Verifying signatures

> Check that a webhook request came from NowBookIt and wasn't changed.

Every webhook request includes an `NBI-Signature` header. Check it before you trust the request, so you know it came from NowBookIt and wasn't changed on the way.

```
NBI-Signature: t=1792675202,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
```

## How to check it

1. Take `t` (the time the request was sent) and `v1` (the signature) from the header.
2. Join `t`, a dot, and the request body exactly as you received it, e.g. `1792675202.{"eventId":...}`.
3. Create an HMAC-SHA256 of that text using your signing secret (including `whsec_`).
4. If it matches `v1`, the request is genuine. Also reject requests where `t` is more than 5 minutes old.

> [!NOTE]
>
> Use the body exactly as it arrived. If your framework parses the JSON first, the signature won't match. See [Reading the raw body](#reading-the-raw-body).

## Code examples

```js Node.js theme={null}
import { createHmac, timingSafeEqual } from 'node:crypto';

export function verifyNbiSignature(header, rawBody, secret, toleranceSeconds = 300) {
  const parts = header.split(',').map((part) => part.trim().split('='));
  const t = Number(parts.find(([key]) => key === 't')?.[1]);
  if (!Number.isSafeInteger(t) || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
  const expected = createHmac('sha256', secret).update(`${t}.`).update(rawBody).digest();
  return parts
    .filter(([key]) => key === 'v1')
    .some(([, value]) => /^[0-9a-f]{64}$/i.test(value ?? '') && timingSafeEqual(Buffer.from(value, 'hex'), expected));
}
```

```python Python theme={null}
import hashlib
import hmac
import time


def verify_nbi_signature(header: str, raw_body: bytes, secret: str, tolerance_seconds: int = 300) -> bool:
    parts = [part.strip().split("=", 1) for part in header.split(",") if "=" in part]
    timestamps = [value for key, value in parts if key == "t"]
    if not timestamps or not (timestamps[0].isascii() and timestamps[0].isdigit()):
        return False
    t = int(timestamps[0])
    if abs(time.time() - t) > tolerance_seconds:
        return False
    expected = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest()
    return any(hmac.compare_digest(expected.encode(), value.encode()) for key, value in parts if key == "v1")
```

```csharp C# (.NET 5+) theme={null}
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

public static class NbiWebhook
{
    public static bool VerifySignature(string header, byte[] rawBody, string secret, int toleranceSeconds = 300)
    {
        var parts = header.Split(',')
            .Select(part => part.Trim().Split('=', 2))
            .Where(pair => pair.Length == 2)
            .ToList();
        var t = parts.FirstOrDefault(pair => pair[0] == "t")?[1];
        if (!long.TryParse(t, out var timestamp)) return false;
        if (Math.Abs(DateTimeOffset.UtcNow.ToUnixTimeSeconds() - timestamp) > toleranceSeconds) return false;

        using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
        var expected = hmac.ComputeHash(Encoding.UTF8.GetBytes($"{timestamp}.").Concat(rawBody).ToArray());
        foreach (var pair in parts.Where(pair => pair[0] == "v1"))
        {
            byte[] given;
            try { given = Convert.FromHexString(pair[1]); }
            catch (FormatException) { continue; }
            if (CryptographicOperations.FixedTimeEquals(given, expected)) return true;
        }
        return false;
    }
}
```

## Reading the raw body

| Framework | How to get the raw body |
| - | - |
| Express | `express.raw({ type: 'application/json' })`, then `req.body` is a `Buffer` |
| Flask | `request.get_data()` |
| FastAPI | `await request.body()` |
| ASP.NET Core | `Request.EnableBuffering()`, then copy `Request.Body` to a `MemoryStream` before model binding |


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