# HTTP API: DLQ, Cron & Webhooks

HTTP REST endpoints for inspecting and retrying Dead Letter Queues, managing cron schedulers, and configuring event webhooks.

Canonical: https://bunqueue.dev/api/http/dlq-cron/

---

*[Interactive doc iso diagram: Inspect and replay dead letters, schedule recurring crons, and manage webhooks - 14 operational routes: triage DLQ entries with filters, create or remove cron schedules, and register HMAC-signed webhook endpoints - open the HTML page to view it.]*

All 14 endpoints for Dead Letter Queues (6), Cron schedulers (4), and Webhooks (4). See [HTTP API Overview](https://bunqueue.dev/api/http/) for auth and error envelopes.

## Dead Letter Queue (DLQ)

Each queue has a DLQ holding jobs that exhausted `maxAttempts`, failed with `unrecoverable: true`, exceeded `maxStalls`, or were discarded.

### List DLQ Entries

`GET /queues/:queue/dlq[?limit=100&offset=0]` (omit both for all entries; returns `DlqEntry[]` and `total`):

```json
{
  "ok": true,
  "entries": [
    {
      "job": { "id": "...", "data": {}, "attempts": 3 },
      "enteredAt": 1700000000000,
      "reason": "max_attempts_exceeded",
      "error": "SMTP timeout",
      "attempts": [{ "attempt": 1, "startedAt": 1700000000000, "failedAt": 1700000001000, "reason": "explicit_fail", "error": "SMTP timeout", "duration": 1000 }],
      "retryCount": 0
    }
  ],
  "total": 1
}
```

### DLQ Statistics

`GET /queues/:queue/dlq/stats`

```json
{
  "ok": true,
  "stats": {
    "total": 12,
    "byReason": { "explicit_fail": 4, "max_attempts_exceeded": 6, "timeout": 1, "stalled": 1, "ttl_expired": 0, "worker_lost": 0, "unknown": 0 },
    "byQueue": { "emails": 12 },
    "pendingRetry": 0,
    "expired": 0,
    "oldestEntry": 1700000000000,
    "newestEntry": 1700003600000
  }
}
```

### Retry & Purge DLQ

| Method | Path | Body | Response & Event |
| --- | --- | --- | --- |
| `POST` | `/queues/:queue/dlq/retry` | `{ "jobId"?: "..." }` | Re-queues one entry (or all when `jobId` omitted) and resets attempts. Returns `{ "ok": true, "count": N }`. Broadcasts `dlq:retried` / `dlq:retry-all`. |
| `POST` | `/queues/:queue/dlq/purge` | none | Permanently deletes all DLQ entries for `:queue`. Returns `{ "ok": true, "count": N }`. Broadcasts `dlq:purged`. |

```bash
curl -X POST http://localhost:6790/queues/emails/dlq/retry \
  -H "Content-Type: application/json" -d '{"jobId": "019ce9d7-..."}'
curl -X POST http://localhost:6790/queues/emails/dlq/purge
```

### DLQ Configuration

`GET /queues/:queue/dlq-config` · `PUT /queues/:queue/dlq-config`

```bash
curl -X PUT http://localhost:6790/queues/emails/dlq-config \
  -H "Content-Type: application/json" \
  -d '{"config": {"autoRetry": true, "autoRetryInterval": 3600000, "maxAutoRetries": 3, "maxAge": 604800000, "maxEntries": 10000}}'
```

| Field | Default | Description |
| --- | --- | --- |
| `autoRetry` | `false` | Auto-retry eligible DLQ entries after `autoRetryInterval`. |
| `autoRetryInterval` | `3600000` | Auto-retry delay in ms (1 hour). |
| `maxAutoRetries` | `3` | Max automatic retries per entry. |
| `maxAge` | `604800000` | Max entry age in ms (7 days; `null` disables expiry). |
| `maxEntries` | `10000` | Max DLQ entries per queue; oldest evicted when full. |

Broadcasts `config:dlq-changed`.

## Cron Schedulers

| Method | Path | Description |
| --- | --- | --- |
| `GET` | `/crons` | List all cron jobs (`{ "ok": true, "crons": [...] }`). |
| `POST` | `/crons` | Create or update a cron job (reusing `name` updates it). Broadcasts `cron:created` or `cron:updated`. |
| `GET` | `/crons/:name` | Get a single cron job (`404` if not found). |
| `DELETE` | `/crons/:name` | Delete a cron job (`404` if not found). Broadcasts `cron:deleted`. |

```bash
curl -X POST http://localhost:6790/crons -H "Content-Type: application/json" \
  -d '{"name": "daily-cleanup", "queue": "maintenance", "data": {"task": "cleanup-stale-sessions"}, "schedule": "0 2 * * *", "timezone": "America/New_York"}'
```

**`POST /crons` request body:**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | `string` | Yes | Unique cron name; existing names are updated in place. |
| `queue` | `string` | Yes | Target queue name. |
| `data` | `any` | Yes | Payload pushed on every tick. |
| `schedule` | `string` | \* | Cron expression (`"*/5 * * * *"`). Takes precedence if both `schedule` and `repeatEvery` are valid. |
| `repeatEvery` | `number` | \* | Positive safe-integer interval in ms (alternative to `schedule`). |
| `jobName` | `string` | No | Generated job name (default `"default"`). |
| `timezone` | `string` | No | IANA timezone (defaults to server/system timezone on raw HTTP). |
| `priority` | `number` | No | Priority for generated jobs. |
| `maxLimit` | `number` | No | Max total executions before auto-removal. |
| `immediately` | `boolean` | No | Fire once immediately upon creation (default `false`). |
| `skipIfNoWorker` | `boolean` | No | Skip tick if no worker is registered on `queue` (default `false`). |
| `preventOverlap` | `boolean` | No | Skip tick while the previous generated job is still pending or active (default `true`). |
| `uniqueKey` | `string` | No | Deduplication key for generated jobs. |
| `dedup` | `object` | No | `{ ttl?, extend?, replace? }` (`ttl` must be finite). |
| `skipMissedOnRestart` | `boolean` | No | Skip ticks missed during downtime (default `true`). |
| `jobOptions` | `object` | No | Per-job options: `maxAttempts`, `backoff`, `timeout`, `delay`, `stallTimeout`, `removeOnComplete`, `removeOnFail`. |

\* At least one of `schedule` or `repeatEvery` is required.

## Webhooks

HTTP callbacks triggered on job events. Failed deliveries retry up to 3 total attempts (`WEBHOOK_MAX_RETRIES`) with linear backoff (`WEBHOOK_RETRY_DELAY_MS * attempt`, default 1s then 2s).

| Method | Path | Body | Response & Event |
| --- | --- | --- | --- |
| `GET` | `/webhooks` | none | Lists all registered webhooks. |
| `POST` | `/webhooks` | `{ "url", "events", "queue"?, "secret"? }` | Registers a webhook and returns `{ "ok": true, "data": { "webhookId", "url", "events", "queue", "createdAt" } }`. Broadcasts `webhook:added`. |
| `DELETE` | `/webhooks/:id` | none | Deletes a webhook (`404` if missing). Broadcasts `webhook:removed`. |
| `PUT` | `/webhooks/:id/enabled` | `{ "enabled": false }` | Pauses or resumes deliveries without deleting config. Broadcasts `webhook:disabled` / `webhook:enabled`. |

```bash
curl -X POST http://localhost:6790/webhooks -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/hooks/bunqueue", "events": ["job.completed", "job.failed"], "queue": "emails", "secret": "whsec_abc123"}'
```

- `url` (required): HTTP(S) URL. Literal localhost, private IPs, and cloud metadata addresses are rejected at registration (DNS resolution and redirect targets are not validated; pair with network egress controls).
- `events` (required): Subset of `job.pushed`, `job.started`, `job.completed`, `job.failed`, `job.progress`.
- `queue` / `secret` (optional): Restrict to one queue; HMAC signing secret for payload verification.
