# Webhooks (API)

> Cadastre URLs de webhook, teste a entrega, consulte o log e reenvie entregas que falharam.

Fonte: https://avisa.dev/docs/api/webhooks

Veja os eventos, o formato do payload e como verificar a assinatura em [Webhooks](https://avisa.dev/docs/webhooks). Esta página mostra os endpoints para gerenciar os webhooks.

## O objeto Webhook

| Campo | Tipo | Descrição |
|---|---|---|
| `id` | string | Id com prefixo `whk_` |
| `url` | string | URL que recebe os eventos (HTTPS) |
| `events` | string[] | Eventos assinados |
| `number_id` | string \| null | Só eventos deste número. `null` = todos os números |
| `secret` | string | Segredo da assinatura. **Só aparece na criação** |
| `created_at` | string | Data de criação |

## Criar webhook

`POST /webhooks`

| Campo | Tipo | |
|---|---|---|
| `url` | string | Obrigatório. URL HTTPS que recebe `POST` |
| `events` | string[] | Opcional. Padrão: todos os eventos |
| `number_id` | string | Opcional. Só eventos deste número |

```bash title="cURL"
curl -X POST "https://api.avisa.dev/v1/webhooks" \
  -H "Authorization: Bearer $AVISA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://meusistema.com/webhooks/avisa",
    "events": [
      "message.received",
      "message.failed"
    ]
  }'
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "url": "https://meusistema.com/webhooks/avisa",
    "events": [
      "message.received",
      "message.failed"
    ]
  }),
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.post(
    "https://api.avisa.dev/v1/webhooks",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
    json={
        "url": "https://meusistema.com/webhooks/avisa",
        "events": ["message.received", "message.failed"],
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'url' => 'https://meusistema.com/webhooks/avisa',
        'events' => ['message.received', 'message.failed'],
    ]),
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```

```json title="Resposta 201"
{
  "id": "whk_7u8i9o0p",
  "url": "https://meusistema.com/webhooks/avisa",
  "events": ["message.received", "message.failed"],
  "number_id": null,
  "secret": "whsec_4f9a2c7e1b8d3a6f0c5e9b2d7a4f1c8e",
  "created_at": "2026-12-04T15:20:00Z"
}
```

Guarde o `secret` em `AVISA_WEBHOOK_SECRET`. Ele não aparece de novo.

## Listar webhooks

`GET /webhooks`

```bash title="cURL"
curl -X GET "https://api.avisa.dev/v1/webhooks" \
  -H "Authorization: Bearer $AVISA_API_KEY"
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks', {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
  },
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.get(
    "https://api.avisa.dev/v1/webhooks",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```

## Remover webhook

`DELETE /webhooks/{id}`

```bash title="cURL"
curl -X DELETE "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p" \
  -H "Authorization: Bearer $AVISA_API_KEY"
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p', {
  method: 'DELETE',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
  },
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.delete(
    "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```

## Testar webhook

`POST /webhooks/{id}/test`

Envia um evento `webhook.test` assinado para a URL e devolve o que o seu servidor respondeu. Use para conferir a verificação da assinatura.

```bash title="cURL"
curl -X POST "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/test" \
  -H "Authorization: Bearer $AVISA_API_KEY"
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/test', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
  },
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.post(
    "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/test",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/test');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```

```json title="Resposta"
{ "status_code": 200, "duration_ms": 184, "body": "ok" }
```

## Log de entregas

`GET /webhooks/{id}/deliveries`

Cada evento enviado para a URL, com o status HTTP, número de tentativas e a próxima retentativa.

```bash title="cURL"
curl -X GET "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries?limit=20" \
  -H "Authorization: Bearer $AVISA_API_KEY"
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries?limit=20', {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
  },
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.get(
    "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries?limit=20",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries?limit=20');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```

```json title="Resposta"
{
  "data": [
    { "id": "dlv_1q2w3e4r", "event": "message.received", "status_code": 500, "attempts": 2, "delivered_at": null, "next_retry_at": "2026-12-04T15:56:00Z", "created_at": "2026-12-04T15:25:00Z" }
  ],
  "next_cursor": null
}
```

## Reenviar entrega

`POST /webhooks/{id}/deliveries/{delivery_id}/retry`

Reenvia agora uma entrega, sem esperar a próxima retentativa automática.

```bash title="cURL"
curl -X POST "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries/dlv_1q2w3e4r/retry" \
  -H "Authorization: Bearer $AVISA_API_KEY"
```

```js title="JavaScript"
const res = await fetch('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries/dlv_1q2w3e4r/retry', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.AVISA_API_KEY}`,
  },
});

const data = await res.json();
if (!res.ok) throw new Error(data.error.message);
```

```python title="Python"
import os
import requests

res = requests.post(
    "https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries/dlv_1q2w3e4r/retry",
    headers={
        "Authorization": f"Bearer {os.environ['AVISA_API_KEY']}",
    },
)
res.raise_for_status()
data = res.json()
```

```php title="PHP"
<?php
$ch = curl_init('https://api.avisa.dev/v1/webhooks/whk_7u8i9o0p/deliveries/dlv_1q2w3e4r/retry');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('AVISA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
```
