# Webhooks

Source: [Webhooks](<https://docs.gptunnel.ru/en/creative-lab/webhooks/>)

<a id="_top"></a>
> **Note: Authentication**
>
> You need an API key to send requests. See [Quick start](<https://docs.gptunnel.ru/en/>)

Set `webhook_url` when [creating the task](<https://docs.gptunnel.ru/en/creative-lab/create-task/>). When the task reaches `done` or `failed`, GPTunneL sends a `POST` request to that URL. The notification uses the same `id` as the task creation response.

> **Caution: URL requirements**
>
> `webhook_url` must be a public HTTPS URL.

<a id="request-body"></a>
## Request body

The notification contains `id`, `status` and `result`. When the status is `failed`, `error` explains the failure and `result` is empty. The API response fields `code` and `webhook` are omitted. To check delivery status, [poll the task](<https://docs.gptunnel.ru/en/creative-lab/get-task/>).

```json
{
  "id": "6a61e021a6e77553a989c41d",
  "model": "kling-v3",
  "status": "done",
  "result": [ { "url": "https://.../6a61e021a6e77553a989c41d_result.mp4" } ],
  "created_at": 1784799265
}
```

 <a id="headings"></a>
 
<a id="headers"></a>
## Headers

| Header | Meaning |
| --- | --- |
| `X-Gptunnel-Event` | Type of event: `task.done` or `task.failed` |
| `X-Gptunnel-Delivery` | Task Identifier |
| `X-Gptunnel-Signature` | Body signature: `t=<unix>,v1=<hex>` |
| `User-Agent` | `gptunnel-webhook/2` |

<a id="outgoing-addresses"></a>
## Outgoing Addresses

If your server or WAF filters incoming requests, allow the webhook source IP listed on the [Network addresses](<https://docs.gptunnel.ru/en/getting-started/network-addresses/>) page.

We notify the account email before a planned address change. During an emergency migration, advance notice may be unavailable.

> **Caution: Check the signature even with an IP filter**
>
> Other outbound requests also use this IP address. Verify the signature to confirm the request is your webhook.

<a id="verification-of-signature"></a>
## Verification of signature

The signature lets you verify that GPTunneL sent the request. The header format is compatible with Stripe’s signature format.

```plaintext
X-Gptunnel-Signature: t=1784799265,v1=5f8c...e21a
```

Here `t` is the Unix timestamp in seconds, and `v1` is the hex-encoded `HMAC-SHA256` of `"<t>.<request body>"`. Use the API key that created the task as the signing secret. Verify the signature before reading the fields and reject unsigned requests.

Take `t` and the raw request body, compute `HMAC-SHA256` over `` `${t}.${rawBody}` `` using your API key as the secret, and compare the result with `v1`. The examples allow a five-minute clock difference.

<a id="tab-panel-0-0"></a>
**Python**

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

def verify(raw_body: bytes, header: str, api_key: str) -> bool:
    match = re.fullmatch(r't=([0-9]{1,12}),v1=([0-9a-f]{64})', header)
    if not match or abs(time.time() - int(match[1])) > 300:
        return False
    signed = match[1].encode('ascii') + b'.' + raw_body
    expected = hmac.new(api_key.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, match[2])
```

<a id="tab-panel-0-1"></a>
**TypeScript (Node.js)**

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

function verify(rawBody: Buffer, header: string, apiKey: string): boolean {
  const match = /^t=([0-9]{1,12}),v1=([0-9a-f]{64})$/.exec(header)
  if (!match || Math.abs(Date.now() / 1000 - Number(match[1])) > 300) return false
  const expected = createHmac('sha256', apiKey)
    .update(match[1] + '.')
    .update(rawBody)
    .digest()
  return timingSafeEqual(expected, Buffer.from(match[2], 'hex'))
}
```

<a id="retry"></a>
## Retry

Respond with a `2xx` status. If delivery fails, GPTunneL retries after increasing delays of about 5, 20, 60 and 180 seconds.

> **Note: Handle duplicate notifications idempotently**
>
> A webhook can be delivered more than once. If all attempts fail, poll the task for its final status. Deduplicate notifications using the task ID in `X-Gptunnel-Delivery`.
