Skip to content

Introducing the Sent Voice API

A short tour of inbound, outbound, and in-app calling, plus an example receptionist agent.

Voice API: a call waveform marked with call.answered, user.said, agent.say, transfer and call.completed

Our Voice API is live today for inbound, outbound, and in-app calling.

It can place and answer calls on the numbers you already text from, and every call lands on the same contact record as that customer’s messages.

This post is a short tour of the API, and then a walkthrough of a sample receptionist agent that answers calls.

Turning on phone calls

Any US number you already text from can take calls, or you can request a new one. Voice is US-only for now.

The Request a voice number dialog in Sent, with Request a new number selected

Each voice number has a callback URL and a signing secret. The callback URL is an endpoint on your server that Sent asks what to do with each call. The signing secret is how your server checks that those requests came from Sent.

What the Voice API does

You can answer calls from your server, place them, make them from your app, or hand them to an agent. A number can do more than one.

  • Answer from your server. If your server can handle a webhook, it can answer a call. Set a callback URL on the number. When a call comes in, Sent sends a signed request to your callback URL, and your server replies with what to do: ring a phone number, ring a user of your app, play a menu, hold, or hang up. If you play a menu, the caller's keypresses come back as another request, so you can chain them.
JSON
{ "action": { "action": "playMenu", "prompt": { "text": "Press 1 for sales, 2 for support" }, "maxDigits": 1 } }
  • Place a call. One request from your number dials a person. When they answer, Sent asks your callback what to do next, the same as for an incoming call.
  • Call from your app. Your backend mints a short-lived voice token for the signed-in user. The browser registers with @sentdm/voice-client and calls client.connect({ to }). These use the same number, logs, and webhooks as every other call.
  • Hand calls to an agent. You can put your own agent on the line. Sent answers the call, turns the caller's speech into text, and speaks your replies out loud. Your program receives text and sends text over a WebSocket. You can bring your own model.

However a call is answered, Sent sends your server webhooks when it is answered, when it ends, and what it cost.

The receptionist agent

The agent is for a dental clinic. When the front desk is closed, it moves the caller's next appointment to an open slot, transfers emergencies to the dentist on call, and texts the caller a confirmation. For anything else it says the front desk opens at 8 AM.

It needs three things: a number with calls enabled, an agent record stored with Sent, and a program that holds a WebSocket. The program is one file, about 170 lines. You can see the full gist on our GitHub.

Sent handles everything else: the phone network, speech recognition, text-to-speech, and the webhooks.

Step 1: Create the agent record

An agent record tells Sent how your program wants to receive calls and what it is allowed to do. Only name and session_type are required.

Shell
curl -X POST https://api.sent.dm/v3/voice/agents \
  -H "x-api-key: $SENT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Front desk, after hours",
    "session_type": "text",
    "greeting": "Thanks for calling Northside Dental. I'\''m the clinic'\''s automated assistant and this call is transcribed. The front desk is closed, but I can move an appointment or reach the dentist on call. How can I help?",
    "tools": ["transfer", "hangup"],
    "transfer_targets": [{ "kind": "number", "value": "+12125550177" }],
    "fallback": { "kind": "number", "value": "+12125550177" },
    "limits": { "max_call_seconds": 600, "max_concurrent_calls": 5 }
  }'
  • session_type: "text" means Sent handles the audio. Your program receives what the caller said as text and replies with text for Sent to speak. The other value is audio, which gives you the raw audio in both directions if you want to run your own speech model.
  • greeting is spoken as soon as the call is answered, before your program has done anything.
  • tools is what the agent can do other than talk. The default is ["hangup"]. This agent can also transfer, which requires transfer_targets. Sent refuses a transfer to any number not on that list, so a caller can’t talk the model into dialing somewhere else. This list is on the record, not in your code.
  • fallback is who gets the call when your program isn’t connected. Without one, the call fails with agent_unreachable. It’s best to set one.
  • limits caps how long a call can last and how many can run at once. Sent enforces them.
  • websocket_url is left out of this example. If you set it, Sent connects to you for each call. If you leave it out, your program connects to Sent once and all calls arrive on that connection. The second is simpler for a program that is always running.

The response includes the agent id and a signing_secret. You only see the secret once - save it. An agent that connects to Sent doesn't use it, but if you later add a websocket_url, Sent signs the handshake with it. This is a different secret from the number's callback secret.

The Phone calls settings for a number in Sent: answered by the Front desk, after hours agent, with the callback URL and signing secret below

Step 2: Write the program

Sent and your program talk over one WebSocket by sending each other small JSON messages. Sent sends session.start when a call is answered, user.said for each thing the caller says, tool.result after an action, and session.end when the call ends. The program sends agent.say to speak and tool.call to act. There are SDKs for Python, TypeScript, .NET, Go, Rust, Java, and the browser, but the frames are small enough to handle directly.

The program is Node 22 with two dependencies, ws and the Anthropic SDK. It connects with the API key as a bearer token and the sent.voice.v1 subprotocol:

JavaScript
const ws = new WebSocket(`${VOICE_URL}?agent_id=${env.SENT_AGENT_ID}`, "sent.voice.v1", {
  headers: { Authorization: `Bearer ${env.SENT_API_KEY}` },
});

ws.on("message", async (data) => {
  const frame = JSON.parse(data.toString());
  const id = frame.session_id;

  switch (frame.type) {
    case "ping":
      return send({ type: "pong", ts: frame.ts });

    case "session.start": {
      const s = { caller: frame.from.value, appointment: null, history: [] };
      s.queue = findAppointment(s.caller).then((a) => (s.appointment = a));
      sessions.set(id, s);
      return;
    }

    case "user.said": {
      const s = sessions.get(id);
      if (!s) return;
      s.queue = s.queue
        .then(() => takeTurn(send, id, s, frame))
        .catch(() => send({ type: "agent.say", session_id: id, text: "Sorry, could you say that again?" }));
      return;
    }
  }
});

When the call starts, Sent tells the program who is calling. The program uses that number to look up the caller's appointment before they've said anything.

The program handles one thing the caller says at a time, in order. Without that, if the caller says two things quickly, the model could be answering the second before it has finished with the first, and the replies would come back out of order.

Each time the caller says something, the model returns a small JSON object: what to say, and one of three actions. The code checks the action before acting on it:

JavaScript
const turn = await decide(s); // { say, action, slot }
let say = turn.say;

if (turn.action === "move") {
  const moved = await moveAppointment(s.caller, turn.slot);
  if (moved) {
    s.appointment = moved;
    await textConfirmation(s.caller, moved.when);
  } else {
    say = "Sorry, that time was just taken. Shall I read you the open slots again?";
  }
}
send({ type: "agent.say", session_id: id, text: say });

if (turn.action === "hangup") {
  send({ type: "tool.call", session_id: id, call_ref: `hangup-${Date.now()}`, name: "hangup", arguments: {} });
}

The model never writes to the calendar. If it proposes a slot that doesn't exist, moveAppointment returns nothing, no text is sent, and the caller is told so.

The confirmation text is one request to the messages API, from the same process that is on the call:

JavaScript
await fetch(`${API_URL}/v3/messages`, {
  method: "POST",
  headers: { "x-api-key": env.SENT_API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ to: [to], template: { id: env.CONFIRM_TEMPLATE_ID, parameters: { when } } }),
});

It uses a template rather than free text, because the first message to someone who has never texted you must be a template. Submit the template for review before you need it. A message that uses a template still in review is accepted and then blocked.

Three things to know about this protocol:

  • The program never handles audio. It reads strings and writes strings.
  • One connection carries every call to the number, so state is a map keyed by session id.
  • Ending a call is a tool.call, not a socket close. If you close the socket, Sent assumes the connection dropped.

Step 3: Bind the number

Turning on voice for a number takes the agent id, so one request does both:

Shell
curl -X POST https://api.sent.dm/v3/channels/voice \
  -H "x-api-key: $SENT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "number": "+12125550100", "agent_id": "agent_eeac3a63-c61b-4060-a352-e612f68b88f4" }'

The response says "routes_to": "agent_pending". Sent records the binding immediately, but the phone network takes up to about an hour to start sending calls to the new destination.

Until then, calls may still go to the old one. Bind the number before you need it, and confirm with a real call.

The agent's tools settings in Sent: Transfer and Hang up turned on, a transfer target number, a fallback number, a 600 second call limit and 5 calls at once

Step 4: Call it

Shell
SENT_API_KEY=... SENT_AGENT_ID=agent_... ON_CALL_NUMBER=+1212555... \
CONFIRM_TEMPLATE_ID=... ANTHROPIC_API_KEY=... \
node agent.mjs

I called the number and asked to move Thursday's appointment. I asked for Friday at 8 PM (where there aren’t any slots available) and was offered three available slots. I took Friday at 8 AM. The confirmation text actually arrived before the call ended.

Most of the pause before the agent replies is the model thinking. Sent’s side of the latency is <100 ms, so if the agent feels slow, look at the model first. When I talked over the agent, it stopped. Sent handles that - your program doesn't have to.

The call appears in Activities next to the text it sent: one minute eleven seconds, about three cents. The backend received three webhooks:

JSON
{ "event": "call.answered",        "call_id": "call_8a1f...", "mode": "agent", "agent_id": "agent_eeac..." }
{ "event": "call.agent_connected", "call_id": "call_8a1f...", "mode": "agent", "agent_id": "agent_eeac..." }
{ "event": "call.completed",       "call_id": "call_8a1f...", "duration_seconds": 71, "price": "0.03" }

Failure modes

  • If the program isn’t running, the call goes to the fallback number. Without a fallback it fails with agent_unreachable and you get a call.failed webhook.
  • If the agent is at max_concurrent_calls, the next call fails with agent_busy. It doesn’t go to the fallback. The fallback is for when the agent is absent, not when it is busy. If you want overflow to ring a person, the agent should transfer.
  • If a transfer target doesn't answer, the caller stays with the agent and the tool.result comes back failed. The program has to say something, and this one says the team will call in the morning.
  • If the program drops mid-call, Sent holds the session for 15 seconds. If it’s reconnected within that window, the call continues. This program doesn't implement resume, so a crash sends the caller to the fallback. The resume handshake is in our docs.

Two more things

  • Run your own speech model. Set session_type to audio and the socket carries raw audio instead of text. Sent stops doing speech recognition and text-to-speech, and the greeting and voice fields no longer apply. This skips the text step, which is where most of the latency is.
  • Text and voice in one agent. The chat-voice-agent-openai-sent sample runs one agent across SMS, WhatsApp, RCS, and phone calls on one number. It replies to texts, answers and places calls, and hands over to a person. All of this is on the same contact record.

Try it out

The agent is a gist. The other samples are on GitHub. The docs cover the voice SDKs (browser, iOS, Android), the agent SDKs, and how to connect an agent.

Email support@sent.dm to enable phone calls for your account.

Share

Published on

Read More

One API. Every channel.