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#
- A customer calls your number. Meta sends the
callsevent to the platform, which passes it to your webhook with the SDP offer. - Your server creates a WebRTC connection from that offer and derives an SDP answer.
- It sends the answer back to the platform (
pre-accept, thenaccept), which passes it to Meta. - 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.
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.
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.
Call a customer#
For an agent to call, for example to confirm a delivery:
- Check the permission with
GET /connections/{id}/call-permissions?to=.... - Without permission, send a request with
callPermissionRequestinPOST /messages, then wait for the customer's answer in your webhooks. - Create an SDP offer with your WebRTC stack and start the call with
POST /calls. - On the
connectevent with directionBUSINESS_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
2xxbefore any processing: a call only rings for 30 to 60 seconds. - Plan for the case where your agent is not ready: call
rejectrather than letting it ring. - Keep the call identifier (
wacid...) in your logs: it is the one Meta and the platform use.