# Create Webhook (POST)

Create a new webhook to receive notifications from external services.

## Endpoint

```
POST https://api.pocketalert.app/v1/webhooks
```

## Authentication

> **Info: Required**
>
> Include one of these headers in your request:
> - `Token: <your-api-key>` — Get it from [API Keys](https://pocketalert.app/account/apikeys)
> - `Authorization: Bearer <jwt-token>` — From [Login](https://info.pocketalert.app/api/user/login.md)

## Request

### Headers

| Header | Required | Description |
|--------|:--------:|-------------|
| `Token` | ✅ | Your API key |
| `Authorization` | ✅ | `Bearer <jwt-token>` |

### Body Parameters

| Parameter | Type | Required | Description |
|-----------|------|:--------:|-------------|
| `name` | string | ✅ | Webhook name (e.g., "Sentry", "GitHub") |
| `message` | string | ✅ | Message template (use `*` for raw JSON) |
| `application_id` | string | ❌ | Application TID to categorize messages |
| `device_id` | string | ❌ | Target device TID (omit for all devices) |
| `default_level` | int | ❌ | Default priority level (`-2`..`2`) applied to messages from this webhook when the payload doesn't specify one. See [Message priority levels](https://info.pocketalert.app/api/messages/create.md#priority-levels). |
| `actions` | string | ❌ | JSON action-buttons template. Same shape as [message actions](https://info.pocketalert.app/api/messages/create.md#action-buttons), but `label`/`value` may contain `%field.path%` placeholders resolved against the incoming payload. Stored as a JSON string. |

### Action Buttons Template

`actions` lets webhook-generated notifications carry up to 3 buttons, with `%field.path%` placeholders substituted from the incoming payload (same syntax as the message template):

```json
[
  { "type": "view", "label": "Open issue", "value": "%issue.html_url%" },
  { "type": "http", "label": "Ack", "value": "{\"url\":\"https://ops.example.com/ack/%issue.id%\",\"method\":\"POST\"}" }
]
```

After substitution the result is validated like [message actions](https://info.pocketalert.app/api/messages/create.md#action-buttons) (max 3, valid types, `http` needs a `url`). Invalid actions are skipped; the message is still delivered.

### Message Template Syntax

Use `%field.path%` to extract values from incoming JSON:

| Template | Description |
|----------|-------------|
| `*` | Output entire JSON payload |
| `%field%` | Extract top-level field |
| `%object.field%` | Extract nested field |
| `%array.0.field%` | Extract from array by index |

See [Webhook Settings](https://info.pocketalert.app/api/webhooks/settings.md) for detailed examples.

### Example Request

**cURL**

```bash
curl -X POST "https://api.pocketalert.app/v1/webhooks" \
  -H "Token: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "GitHub Push",
    "message": "Push to %repository.name%: %head_commit.message%",
    "application_id": "s9h1ekk5xb46jikthl"
  }'
```

**JavaScript**

```javascript
const response = await fetch('https://api.pocketalert.app/v1/webhooks', {
  method: 'POST',
  headers: {
    'Token': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'GitHub Push',
    message: 'Push to %repository.name%: %head_commit.message%',
    application_id: 's9h1ekk5xb46jikthl'
  })
});

const webhook = await response.json();
console.log('Webhook URL:', `https://api.pocketalert.app/v1/webhooks/receive/${webhook.url}`);
```

**Python**

```python
import requests

response = requests.post(
    'https://api.pocketalert.app/v1/webhooks',
    headers={'Token': 'your-api-key'},
    json={
        'name': 'GitHub Push',
        'message': 'Push to %repository.name%: %head_commit.message%',
        'application_id': 's9h1ekk5xb46jikthl'
    }
)

webhook = response.json()
print(f"Webhook URL: https://api.pocketalert.app/v1/webhooks/receive/{webhook['url']}")
```

**PHP**

```php
$response = Http::withHeaders([
    'Token' => 'your-api-key',
])->post('https://api.pocketalert.app/v1/webhooks', [
    'name' => 'GitHub Push',
    'message' => 'Push to %repository.name%: %head_commit.message%',
    'application_id' => 's9h1ekk5xb46jikthl',
]);

$webhook = $response->json();
echo 'Webhook URL: https://api.pocketalert.app/v1/webhooks/receive/'.$webhook['url'];
```

## Response

### Success Response

> **Tip: 201 Created**
>
> Webhook created successfully

| Field | Type | Description |
|-------|------|-------------|
| `tid` | string | Unique webhook identifier |
| `name` | string | Webhook name |
| `url` | string | Webhook URL slug — use this in external services |
| `message` | string | Message template |
| `actions` | string | Action-buttons JSON template (empty if not set) |
| `is_active` | boolean | Whether webhook is active |

```json
{
  "tid": "kblf7mh9j8jjyeh4fx",
  "name": "GitHub Push",
  "url": "cs994rqfkk8ljngt",
  "message": "Push to %repository.name%: %head_commit.message%",
  "actions": "",
  "application_tid": "qm47b9pzxzx7applfd0",
  "device_tid": "",
  "is_active": true,
  "created_at": "01.09.2023 22:38:48"
}
```

> **Tip: Using Your Webhook**
>
> Configure your external service to POST to:
> ```
> https://api.pocketalert.app/v1/webhooks/receive/{url}
> ```

### Error Responses

| Status | Description |
|:------:|-------------|
| `401` | Unauthorized — Invalid or missing token |
| `422` | Validation Error — Missing required fields |
