# Concepts (/docs/concepts)

Workspaces, numbers, programs, calls, and the events and audio herald sends.

## Workspace [#workspace]

A workspace holds your numbers, the SIP accounts that bring their calls, where the calls go, and the call history. Each workspace has:

- a **signing secret**: herald signs every request to your program with it, and your program checks it (`HERALD_SECRET` with the SDK). Rotating it takes effect at once.
- **API keys** (`hc_…`): for the tunnel, the CLI, herald cloud's API and herald's API. A key sees only its workspace. Keys are shown once, when created.
- a **test line**: digits that ring the workspace from any SIP phone, at herald's SIP address. Calls to it reach your program like calls to your numbers.
- **limits**. A new workspace takes 3 calls at once, calls out to `+39` numbers only, 20 calls out per hour, and calls of up to 30 minutes. A call over a limit is refused and shows in the history as refused, with the limit.

## Numbers and lines [#numbers-and-lines]

A **line** is a number of yours (E.164, e.g. `+390212345678`), the SIP account that brings its calls, and where they go. A workspace has up to 10 numbers.

- **Verification.** A number takes calls once verified: herald calls it, and whoever answers types the 4-digit code the dashboard or `herald-cloud numbers verify` shows.
- **SIP accounts (trunks).** The account at your phone provider. herald registers to the provider's `registrar` with the username and password, over UDP, TCP or TLS. A provider that sends calls from fixed addresses instead takes a trunk without registration: its source addresses (at most a /16 for IPv4) and a review before it takes calls. A trunk with **calls out** places the calls of `call.dial()` and `POST /v1/calls`, showing its caller id.
- **Where calls go.** Each line uses the workspace's voice URL, its own URL, or a path through the tunnel. The test line uses the workspace's.
- **WhatsApp.** A line can take the calls of WhatsApp users to its number, registered on the WhatsApp Business Platform: on WhatsApp only (**Add line › On WhatsApp**), or on both channels. Add the number in WhatsApp Manager, then connect it on the line's page with its phone number ID and a system user's token with `whatsapp_business_management`: herald cloud sets the number's calling settings so Meta sends its calls to herald. When the line already takes phone calls, choose verification by phone call in WhatsApp Manager: herald cloud answers Meta's call and shows the code on the line's page. Phone calls can be turned on or off on the line's page; each channel can come first. WhatsApp calls go where the line's calls go, with `call.channel` set to `"whatsapp"` (`"phone"` for the others) and `call.from` the WhatsApp user, `sip:+<number>@wa.meta.vc`.

herald finds the line from the called number in the call (`matched_by`: `diversion`, `history-info`, `request-uri`, `to`, or `test_line` for the test line).

## Programs [#programs]

Your program is the server that handles the calls. herald reaches it at a **voice URL**:

| Voice URL | |
|---|---|
| `https://…` | herald posts each call as HTTP requests and the program answers with verbs. Any HTTP host works, serverless functions included |
| `wss://…` | herald opens a WebSocket for each call and keeps it for the whole call. Needs a long-running process |
| the tunnel | `https://<workspace>.<tunnel host><path>`, forwarded to a port on your machine by `herald-cloud tunnel` |

herald posts `call.ended` to the **status webhook**: the voice URL unless you set another (`herald-cloud backend --status-webhook`, or Developers → Status webhook).

With the [SDK](/docs/sdk) a program is one `herald({ secret, onCall })`: `onCall` runs for every call, on either kind of voice URL.

```ts
import { herald } from "@herald-voice/sdk";

const app = herald({
  secret: process.env.HERALD_SECRET,
  onCall: async (call) => {
    if (call.number === "+390212345678") await call.play("/audio/office.wav");
    await call.hangup();
  },
});
```

`call.number` is the number dialled: branch on it to treat lines differently in one program.

## Calls [#calls]

**Calls in.** A call to a verified number or the test line arrives at herald from the provider. Before answering, herald asks your program what to do (`call.incoming`); the caller hears ringing meanwhile. The program answers within 5 seconds, or the call is refused.

**Calls out.** `POST /v1/calls` ([REST API](/docs/api#post-v1calls)), `HeraldClient.calls.create()` or `herald-cloud call` place a call through a trunk with calls out. Once answered, the program at `voice_url` takes it like a call in, with `direction: "outbound"`.

**During a call.** The program plays audio, reads keys, records, dials a number, or takes the call's audio. `GET /v1/calls` and `herald-cloud calls --live` list the calls in progress: `answered`, `streaming` (a program has the audio), `transferring` (a person has it).

**The end.** Every call ends with an `end_reason`: `flow_end` (the program's verbs ran out), `flow_hangup` (it hung up), `caller_hangup`, `stream_hangup` (the program on the stream hung up), `api_hangup`, `rejected`, `error`, `max_duration`, `rtp_timeout`, `session_expired`, `step_limit`, `shutdown`; a call out nobody answered ends with `no_answer`, `busy` or `failed`. The call history (Calls in the dashboard, `herald-cloud calls`, `GET /api/cli/calls`) keeps every ended and refused call with its duration and end reason.

## The event stream [#the-event-stream]

herald tells your program about a call with signed events, and the program answers each with verbs:

1. `call.incoming`: a call arrived. The program answers with verbs, e.g. play a file, then gather keys with an `action` URL.
2. herald runs them. When a verb with a result (keys, a recording, a dial, a connection) is done, it posts `call.result` with the result to the `action` URL, and the answer's verbs run next.
3. `call.ended`, to the status webhook, with every verb's result, in order.

The SDK runs this differently: its `herald()` app opens the event's `stream_url` and runs `onCall` on that WebSocket, so each `await call.…()` is a verb herald runs at once and returns the result, and the code runs once. `call.next()` ends the request with a verb and an `action` that brings its result to the next handler. The JSON is in [What herald sends your program](/docs/api#what-herald-sends-your-program).

### The call's audio [#the-calls-audio]

A program can also take the call's audio and talk on it, live, over a WebSocket: for a voice model, its own speech, or listening to the caller. With a `wss://` voice URL herald connects to the program; with an `https://` one the program opens the request's `stream_url` (an SDK app does it for every call). A stream the program opens does not answer the call: herald answers when the program needs the audio, so it can still refuse the call or ring someone first.

The audio is G.711 μ-law at 8 kHz, mono. On the WebSocket herald sends JSON messages:

| From herald | |
|---|---|
| `start` | the call: `call_id`, `from`, `to`, `number`, `direction`, and the `params` of a `connect` |
| `media` | 20 ms of the caller's audio, base64 |
| `dtmf` | a key the caller pressed |
| `mark` | the audio sent before a mark has played |
| `stop` | the caller hung up, or herald ended the stream |

| From the program | |
|---|---|
| `media` | audio to play, base64 μ-law of any length; herald paces it |
| `mark` | ask to be told when the audio so far has played |
| `clear` | drop the audio not played yet (barge-in) |
| `herald.verb` | have herald run a verb, e.g. play a file; the answer is `herald.result` |
| `herald.transfer` | pass the call to a person; `herald.resumed` when it comes back |
| `herald.redirect` | end the stream and run these verbs |
| `herald.hangup` | hang up |

With the SDK this is [`HeraldCall`](/docs/sdk#the-stream): `live.on("audio")`, `live.sendAudio()`, `live.mark()`, `live.clear()`, `live.transfer()`. When the program closes the WebSocket, the call goes on with what follows.