# Plug in a voice agent

Answer WhatsApp calls with your WebRTC server.

This guide shows how to plug a voice agent (or a softphone) into the WhatsApp calls of your number. The idea fits in one sentence: the platform carries the signalling, and the sound goes straight from Meta to your server.

## The architecture

1. A customer calls your number. Meta sends the `calls` event to the platform, which passes it to your [webhook](/en/docs/webhooks) with the SDP offer.
2. Your server creates a WebRTC connection from that offer and derives an SDP answer.
3. It sends the answer back to the platform (`pre-accept`, then `accept`), which passes it to Meta.
4. The audio then flows between Meta and your server, which hands it to your agent: speech recognition, language model, speech synthesis.

Your server must be reachable by Meta: a public address, with the UDP ports of your WebRTC stack open. Keypad tones (DTMF) follow RFC 4733 at 8,000 Hz.

Before writing code, enable calls on the number as described in [WhatsApp calls](/en/docs/calling).

## The receiver

This example receives the webhook, answers an inbound call and hangs up when the call ends. `startWebRtcSession` is a function of your WebRTC stack: it takes the SDP offer and returns the SDP answer, plus an object to close the session.

```js title="Node.js"
import express from 'express';

const API = 'https://hira.localtechstudio.com/api/v1';
const headers = { Authorization: `Bearer ${process.env.WA_KEY}`, 'Content-Type': 'application/json' };
const sessions = new Map();

const act = (callId, action, body = {}) =>
  fetch(`${API}/calls/${callId}/${action}`, { method: 'POST', headers, body: JSON.stringify(body) });

const app = express();

app.post('/webhooks/whatsapp', express.raw({ type: 'application/json' }), async (req, res) => {
  res.sendStatus(200);

  const event = JSON.parse(req.body);
  if (event.event_type !== 'calls') return;

  for (const change of event.payload.entry.flatMap((e) => e.changes)) {
    for (const call of change.value.calls ?? []) {
      if (call.event === 'connect' && call.direction === 'USER_INITIATED') {
        const { sdpAnswer, session } = await startWebRtcSession(call.session.sdp);
        sessions.set(call.id, session);
        await act(call.id, 'pre-accept', { sdpAnswer });
        await session.ready();
        await act(call.id, 'accept', { sdpAnswer });
      }
      if (call.event === 'terminate') {
        sessions.get(call.id)?.close();
        sessions.delete(call.id);
      }
    }
  }
});

app.listen(3000);
```

Verify the `X-Wa-Signature` signature before handling the event, as explained in [Webhooks](/en/docs/webhooks).

## Call a customer

For an agent to call, for example to confirm a delivery:

1. Check the permission with `GET /connections/{id}/call-permissions?to=...`.
2. Without permission, send a request with `callPermissionRequest` in `POST /messages`, then wait for the customer's answer in your webhooks.
3. Create an SDP offer with your WebRTC stack and start the call with `POST /calls`.
4. On the `connect` event with direction `BUSINESS_INITIATED`, apply the SDP answer to your session: the audio opens.

## Tips

- Pre-accept as early as possible: it keeps the customer's first words from being lost.
- Answer the webhook with a `2xx` before any processing: a call only rings for 30 to 60 seconds.
- Plan for the case where your agent is not ready: call `reject` rather than letting it ring.
- Keep the call identifier (`wacid...`) in your logs: it is the one Meta and the platform use.
