# REST API (/docs/api)

The endpoints of herald cloud and of herald, with their requests and responses.

There are two APIs, and the same API key works for both:

- **herald cloud's API**, at `<herald cloud URL>/api/cli/…`: the workspace, numbers, trunks, keys, the voice URL and the call history. It is what the [CLI](/docs/cli) uses.
- **herald's API**, at the address `herald-cloud whoami --json` prints as `api`: calls out, the calls in progress, hangup and transfer. It is what [`HeraldClient`](/docs/sdk#heraldclient) uses.

## Authentication [#authentication]

Send an API key of the workspace as a bearer token:

```bash
curl <herald cloud URL>/api/cli/workspace \
  -H "Authorization: Bearer hc_…"
```

Create keys in the dashboard (Developers → API keys), with `herald-cloud keys create`, or with [`POST /api/cli/keys`](#post-keys). A key sees and changes only its own workspace.

## Errors [#errors]

Responses are JSON. An error is a 4xx (or 502 when herald is not reachable) with a message:

```json
{ "error": "unknown API key: run herald-cloud login" }
```

| Status | |
|---|---|
| 400 | the request is not valid: the message says which field |
| 401 | no key, or a key that is not valid |
| 404 | no such number, trunk, key, call or endpoint |
| 409 | the key name matches several keys |
| 502 | herald is not reachable (live calls) |

Changes answer `{ "ok": true, "message": "…" }`. When herald could not be told about a change at once, the message says so: herald reads it again within a minute.

## herald cloud [#herald-cloud]

### GET workspace [#get-workspace]

```http
GET /api/cli/workspace
GET /api/cli/workspace?secret=1
```

The workspace of the key. With `secret`, it includes the signing secret.

```json
{
  "id": "w-…",
  "name": "Acme",
  "mode": "tunnel",
  "voice_url": "https://w-….<tunnel host>/herald",
  "path": "/herald",
  "secret": "…",
  "test_line": "712345678901",
  "sip": "<herald SIP host:port>",
  "dashboard": "<herald cloud URL>/app",
  "api": "<herald API URL>"
}
```

| Field | |
|---|---|
| `mode` | `tunnel`, `url` or `none`: how calls reach the program |
| `voice_url` | where herald sends the workspace's calls, or `null` |
| `path` | the tunnel path |
| `secret` | only with `?secret=1` |
| `test_line` | digits that ring the workspace from any SIP phone at `sip` |
| `api` | herald's API, for calls out and live calls, or `null` |

### PATCH backend [#patch-backend]

```http
PATCH /api/cli/backend
```

Changes where the workspace's calls go. Send only the fields to change.

| Field | |
|---|---|
| `mode` | `url` or `tunnel` |
| `voice_url` | `http(s)://…`, or `ws(s)://…` for a program that takes every call as a stream; required for `mode: "url"` unless already set |
| `tunnel_path` | the path on your machine, starting with `/` |
| `status_webhook` | where herald posts `call.ended`; empty or `null`: the voice URL |

```bash
curl -X PATCH <herald cloud URL>/api/cli/backend \
  -H "Authorization: Bearer hc_…" -H "content-type: application/json" \
  -d '{"mode": "url", "voice_url": "https://api.example.com/herald"}'
```

```json
{ "ok": true, "message": "Saved", "workspace": { "id": "w-…", "mode": "url", "voice_url": "https://api.example.com/herald", … } }
```

`workspace` is the object of [`GET workspace`](#get-workspace).

### POST backend/test [#post-backendtest]

```http
POST /api/cli/backend/test
```

Sends the voice URL a `call.incoming` as herald would, signed with the workspace's secret, and reports what came back. The test call's id starts with `test-` and it comes from `sip:test@herald.cloud`; the request body has `"test": true`.

```json
{ "ok": true, "status": 200, "ms": 41, "url": "https://api.example.com/herald", "verbs": [{ "verb": "play", "file": "?audio=…" }, { "verb": "gather", "max_digits": 1, "timeout": "6s", "action": "?h=…" }], "body": "…" }
```

```json
{ "ok": false, "status": 401, "ms": 12, "url": "https://api.example.com/herald", "error": "It answered 401", "hint": "It refused herald's signature: give your program this workspace's secret as HERALD_SECRET.", "body": "…" }
```

The answer must be `{"verbs": [...]}` (or a 204) within 10 seconds. A `ws(s)://` voice URL cannot be tested this way.

### POST tunnel [#post-tunnel]

```http
POST /api/cli/tunnel
```

Switches the workspace to the tunnel. `herald-cloud tunnel` calls it once connected.

| Field | |
|---|---|
| `path` | optional: the tunnel path, starting with `/` |

Answers the object of [`GET workspace`](#get-workspace).

### POST secret/rotate [#post-secretrotate]

```http
POST /api/cli/secret/rotate
```

A new signing secret. herald signs with it at once. Read it with `GET workspace?secret=1`.

```json
{ "ok": true, "message": "New secret: update your backend" }
```

### POST test-line/rotate [#post-test-linerotate]

```http
POST /api/cli/test-line/rotate
```

A new test line; the old one stops ringing.

```json
{ "ok": true, "message": "New test line: the old one no longer rings" }
```

### GET numbers [#get-numbers]

```http
GET /api/cli/numbers
```

```json
[{ "number": "+390212345678", "verified": true }]
```

### POST numbers [#post-numbers]

```http
POST /api/cli/numbers
```

| Field | |
|---|---|
| `number` | international format, e.g. `+390212345678` (required) |

At most 10 numbers in a workspace. A number takes calls once verified.

```json
{ "ok": true, "message": "+390212345678 added: verify it to take its calls" }
```

### POST numbers/:number/verify [#post-numbersnumberverify]

```http
POST /api/cli/numbers/+390212345678/verify
```

herald calls the number; the person who answers types the code on the keypad.

```json
{ "ok": true, "code": "4821", "message": "herald is calling +390212345678: answer, and type 4821 on the keypad" }
```

When the number is verified at once: `{ "ok": true, "verified": true }`. Poll [`GET numbers`](#get-numbers) to see when it is verified.

### DELETE numbers/:number [#delete-numbersnumber]

```http
DELETE /api/cli/numbers/+390212345678
```

```json
{ "ok": true, "message": "+390212345678 removed" }
```

### GET trunks [#get-trunks]

```http
GET /api/cli/trunks
```

```json
[
  {
    "name": "main",
    "registrar": "sip.provider.example",
    "username": "0212345678",
    "transport": "udp",
    "outbound": true,
    "no_register": false,
    "sources": [],
    "approved": null,
    "registered": true
  }
]
```

| Field | |
|---|---|
| `approved` | for a trunk without registration: whether it passed review; `null` otherwise |
| `registered` | whether herald is registered with it now; `null` when herald cannot be asked |

### POST trunks [#post-trunks]

```http
POST /api/cli/trunks
```

| Field | |
|---|---|
| `name` | lowercase letters, digits and `-`, up to 32 (required) |
| `registrar` | `host` or `host:port` (required) |
| `username` | the SIP username (required) |
| `password` | the SIP password; stored encrypted |
| `transport` | `udp` (default), `tcp` or `tls` |
| `outbound` | `true`: calls out go through this trunk |
| `caller_id` | the number calls out show |
| `no_register` | `true`: herald does not register; needs `sources` |
| `sources` | addresses or CIDR prefixes calls come from (at most /16 for IPv4, /32 for IPv6), as a list or comma-separated |
| `auth_username` | the authentication username, when different |
| `domain` | the SIP domain, when different from the registrar |
| `contact_user` | the user in the Contact header |

```json
{ "ok": true, "message": "Trunk added" }
```

A trunk with `no_register` takes calls once approved: `"Trunk added: we review trunks without registration before they take calls"`.

### DELETE trunks/:name [#delete-trunksname]

```http
DELETE /api/cli/trunks/main
```

```json
{ "ok": true, "message": "Trunk main removed" }
```

### GET keys [#get-keys]

```http
GET /api/cli/keys
```

```json
[{ "name": "laptop", "prefix": "hc_a1b2", "created_at": "2026-11-29T10:00:00.000Z", "this_cli": true }]
```

`this_cli` marks the key of the request.

### POST keys [#post-keys]

```http
POST /api/cli/keys
```

| Field | |
|---|---|
| `name` | optional: where you use it |

```json
{ "ok": true, "key": "hc_…", "message": "Copy the key now: it is not shown again" }
```

### DELETE keys/:prefix [#delete-keysprefix]

```http
DELETE /api/cli/keys/hc_a1b2
```

Revokes a key by its prefix or its name. A name shared by several keys answers 409.

```json
{ "ok": true, "message": "Key laptop revoked" }
```

### GET calls [#get-calls]

```http
GET /api/cli/calls?limit=20
GET /api/cli/calls?live=1
```

The ended and refused calls, newest first (`limit`: default 20, at most 200):

```json
[
  {
    "id": "…",
    "kind": "ended",
    "direction": "inbound",
    "from": "sip:+393331234567@…",
    "number": "+390212345678",
    "startedAt": "2026-11-29T10:00:00.000Z",
    "endedAt": "2026-11-29T10:01:12.000Z",
    "durationMs": 72000,
    "endReason": "caller_hangup",
    "limit": null
  }
]
```

| Field | |
|---|---|
| `kind` | `ended`, or `refused` when a limit refused the call |
| `direction` | `inbound` or `outbound` |
| `endReason` | how it ended, as herald reports it |
| `limit` | for a refused call: the limit that refused it |

With `live=1`, the calls in progress, from herald:

```json
[
  {
    "id": "…",
    "from": "sip:+393331234567@…",
    "number": "+390212345678",
    "tenant": "w-…",
    "direction": "inbound",
    "matched_by": "request-uri",
    "started_at": "2026-11-29T10:00:00Z",
    "answered": true,
    "streaming": false,
    "transferring": false
  }
]
```

### GET calls/:id [#get-callsid]

```http
GET /api/cli/calls/<id>
```

One ended or refused call, with the fields above plus `workspaceId` and `event`, the `call.ended` or `call.refused` herald sent.

### POST calls/:id/hangup [#post-callsidhangup]

```http
POST /api/cli/calls/<id>/hangup
```

Ends a call in progress of the workspace.

```json
{ "ok": true }
```

### GET prompt [#get-prompt]

```http
GET /api/cli/prompt?template=agent&port=4000&task=…
```

The prompt for a coding agent with the workspace's details, as `text/plain`. `template`: `agent`, `menu` or `custom` (default); `port`: default 4000; `task`: what the line should do.

### CLI sign-in [#cli-sign-in]

The CLI signs in with two unauthenticated requests:

```http
POST /api/cli/login
```

```json
{ "token": "…", "code": "BCDF-GHJK", "url": "<herald cloud URL>/cli?code=BCDF-GHJK", "interval": 2 }
```

Open `url`, sign in and approve the code. Then poll every `interval` seconds:

```http
GET /api/cli/login?token=…
```

| Status | |
|---|---|
| 202 | `{ "status": "pending" }`: not approved yet |
| 200 | `{ "status": "ok", "key": "hc_…", "workspace": "w-…", "name": "Acme", "tunnel": "<tunnel server>", "app": "<herald cloud URL>" }`, once |
| 410 | the code expired (after 10 minutes) |

The key is created with the name `CLI, <date>`.

## herald [#herald]

herald's API is at the address `herald-cloud whoami --json` prints as `api`, under `/v1`. It takes the same API keys, and a key sees only its workspace's calls. Request bodies are JSON (at most 1 MiB); unknown fields are refused with 400. Errors are `{ "error": "…" }`.

| Status | |
|---|---|
| 401 | `missing or wrong API key` |
| 403 | the request names another workspace, or needs herald's admin key |
| 404 | no such call in progress (unknown, ended, or another workspace's) |
| 409 | the call has no stream, or a transfer is already in progress |
| 410 | the call hung up meanwhile |
| 429 | a limit of the workspace refused the call: `limit <name>: <detail>` |

### POST /v1/calls [#post-v1calls]

```http
POST /v1/calls
```

Places a call out through a trunk of the workspace with calls out.

| Field | |
|---|---|
| `to` | the number to call (required) |
| `voice_url` | the program that takes the call once answered: it gets `call.incoming` with `"direction": "outbound"` |
| `verbs` | verbs to run once answered, instead of `voice_url` |
| `trunk` | the trunk to call through; default: the first with calls out |
| `caller_id` | the number shown; default: the trunk's |
| `timeout` | ringing, e.g. `"30s"` (default 30 s) |
| `customer` | a label carried in the call's events (default `"outbound"`) |
| `status_webhook` | where `call.ended` goes; default: the workspace's |

One of `voice_url` and `verbs` is required.

```bash
curl -X POST <herald API URL>/v1/calls \
  -H "Authorization: Bearer hc_…" -H "content-type: application/json" \
  -d '{"to": "+390212345678", "voice_url": "https://api.example.com/herald"}'
```

```json
{ "id": "6b1f…", "status": "calling" }
```

The status is 202: the answer comes at once. How the call went arrives as `call.ended` on the status webhook; an unanswered call ends with `end_reason` `no_answer`, `busy` or `failed`. Calls over the workspace's limits (calls at once, number prefixes, calls out per hour) answer 429.

### GET /v1/calls [#get-v1calls]

```http
GET /v1/calls
```

The workspace's calls in progress.

```json
{
  "calls": [
    {
      "id": "6b1f…",
      "sip_call_id": "…",
      "from": "sip:+393331234567@carrier",
      "to": "sip:+390212345678@herald",
      "request_uri": "sip:+390212345678@herald",
      "number": "+390212345678",
      "matched_by": "request-uri",
      "customer": "",
      "started_at": "2026-11-29T10:00:00Z",
      "direction": "inbound",
      "tenant": "w-…",
      "answered": true,
      "streaming": false,
      "transferring": false
    }
  ]
}
```

| Field | |
|---|---|
| `matched_by` | how herald found the number: `diversion`, `history-info`, `request-uri`, `to` or `test_line` |
| `tenant` | the workspace id |
| `streaming` | a program has the call's audio (`call.stream()`, `connect`) |
| `transferring` | a person has the call through a transfer |

### GET /v1/calls/:id [#get-v1callsid]

```http
GET /v1/calls/6b1f…
```

One call in progress, as in the list.

### POST /v1/calls/:id/hangup [#post-v1callsidhangup]

```http
POST /v1/calls/6b1f…/hangup
```

```json
{ "status": "hung_up" }
```

The call ends with `end_reason: "api_hangup"`.

### POST /v1/calls/:id/redirect [#post-v1callsidredirect]

```http
POST /v1/calls/6b1f…/redirect
```

Replaces what the call is doing with these verbs; the verb that is running stops.

```json
{ "verbs": [{ "verb": "play", "file": "https://cdn.example.com/closing.wav" }, { "verb": "hangup" }] }
```

```json
{ "status": "redirected" }
```

### POST /v1/calls/:id/transfer [#post-v1callsidtransfer]

```http
POST /v1/calls/6b1f…/transfer
```

Passes a call whose audio a program has (a stream) to a person.

| Field | |
|---|---|
| `target` | a number (`tel:+39…` or digits) (required) |
| `trunk`, `caller_id` | the trunk with calls out, and the number shown (`inherit`: the caller's) |
| `mode` | `return` (default): the call comes back to the program when the person hangs up; `handoff`: it stays with them |
| `timeout` | ringing (default 30 s) |
| `reason` | why, for the program |
| `summary` | what the person sees before answering |

```json
{ "status": "transferring" }
```

The status is 202. The program gets the outcome on its stream.

### POST /v1/calls/:id/resume [#post-v1callsidresume]

```http
POST /v1/calls/6b1f…/resume
```

Hands a transferred call back to the program, with an optional note it receives.

```json
{ "note": "customer verified" }
```

```json
{ "status": "resuming" }
```

## What herald sends your program [#what-herald-sends-your-program]

Your program does not call herald for a call: herald calls it. With [`herald()`](/docs/sdk#herald) the SDK handles these requests; this is the protocol underneath, for a program written without it.

Every request is signed with the workspace's signing secret:

```http
X-Herald-Timestamp: 1795980000
X-Herald-Signature: sha256=<hex HMAC-SHA256(secret, "<timestamp>.<body>")>
```

For a WebSocket voice URL the signed payload is the URL herald connects to instead of the body. [`verifySignature`](/docs/sdk#verifysignature) checks it.

| Event | Sent to | When | Your answer |
|---|---|---|---|
| `call.incoming` | the voice URL | a call arrives, before it is answered (within 5 s) | `{"verbs": [...]}`; a 404 refuses the call |
| `call.result` | the `action` URL of a verb | a verb with a result is done | `{"verbs": [...]}`: what runs next |
| `call.redirect` | the URL of a `redirect` verb | the verb runs | `{"verbs": [...]}` |
| `call.ended` | the status webhook | the call is over | ignored |

```json
{
  "event": "call.incoming",
  "call": {
    "id": "6b1f…",
    "from": "sip:+393331234567@carrier",
    "to": "sip:+390212345678@herald",
    "number": "+390212345678",
    "matched_by": "request-uri",
    "direction": "inbound",
    "tenant": "w-…",
    "started_at": "2026-11-29T10:00:00Z"
  },
  "stream_url": "wss://…"
}
```

`call.result` adds `result`, the verb's result; `call.ended` adds `results` (every result, in order), `last_result`, `ended_at` and `end_reason`. `end_reason` is one of `flow_hangup`, `flow_end`, `caller_hangup`, `stream_hangup`, `api_hangup`, `rejected`, `error`, `step_limit`, `rtp_timeout`, `session_expired`, `shutdown`, `max_duration`, and for unanswered calls out `no_answer`, `busy`, `failed`. `stream_url` is a single-use WebSocket for the call, valid while that request waits for its answer: a program that opens it runs the call on it (verbs with `herald.verb`, and the audio), and herald answers the call only when the program needs the audio.

The verbs are listed under [`call.run()`](/docs/sdk#callrun). An answer that ends the list ends the call.

## Other endpoints [#other-endpoints]

These serve the site itself, herald and the tunnel server; the ones herald and the tunnel server call are signed with secrets only they have. A program does not call them.

| Endpoint | |
|---|---|
| `/api/auth/*` | the dashboard's sign-up and sign-in |
| `POST /api/demo/code`, `GET /api/demo/live`, `/api/demo/herald` | the homepage demo: a code to call with, its events, its program |
| `GET /api/search` | the search of these docs |
| `GET /api/herald/tenants` | herald reads every workspace: numbers, trunks, voice URLs, keys, limits |
| `POST /api/herald/events` | herald posts `call.ended` and `call.refused`: the call history |
| `GET`, `POST /api/verify/:id` | the verification call of a number |
| `POST /api/internal/tunnel/auth` | the tunnel server asks which workspace a key belongs to |
| `GET /api/health` | `{ "ok": true }` when the app and its database answer |