Code Examples
These starter examples cover browser calls and signed webhook handlers. Add durable processing and application-specific error handling before production.
Voice Webhook Response Reference
When your webhook handles a voice channel event, the JSON response controls what the agent says and does. All fields are optional.
For streaming responses, return Content-Type: application/x-ndjson — each line is a JSON object. Set "interim": true on non-final chunks so TTS starts immediately.
Browser Web Call
Start a voice call directly in the browser — no phone number needed. Create a web call via the API, then pass the returned connection details to the AgentPhone Web SDK (version 0.2.0 or later).
1. Create a web call (server-side)
The response also includes id — the AgentPhone call ID for use with the call APIs (transcripts, recordings, ending the call). callId identifies the media connection and is not interchangeable with id.
2. Install the Web SDK
Use version 0.2.0 or later — earlier versions only pass the access token and cannot establish current connections.
3. Connect from the browser (client-side)
Web calls use the same webhook flow as phone calls — your agent.message and agent.call_ended webhooks fire normally. The direction field will be "web" and fromNumber/toNumber will be "web" instead of E.164 numbers.
Live transcript events are not delivered over the media connection — fetch transcripts through the call API or receive them via webhooks, the same as phone calls.
Live Transcript Streaming (SSE)
Stream a call’s transcript in real time using Server-Sent Events. The stream replays existing turns on connect, then delivers new turns as they happen. Works for both live and completed calls.
Node.js
Python
See the Calls guide for the full SSE event reference.
JavaScript / Node.js
The examples below use environment variables for credentials. Copy verify-webhook.mjs from Webhook security into the same directory. It verifies raw bytes before JSON parsing.
Express.js Webhook Handler
Install Express with npm install express. Save the following as server.mjs, set AGENTPHONE_WEBHOOK_SECRET, and run node server.mjs.
Flask Webhook Handler
Copy verify_webhook.py from Webhook security, then install Flask with pip install flask. Set AGENTPHONE_WEBHOOK_SECRET and run flask --app server run --port 3000.
Voice Webhook with OpenAI
When connecting any LLM to a voice handler, build the conversation from recentHistory and the current data.transcript. History is context; do not assume it contains the latest caller turn. Keep model credentials on the server and choose a model available to your account.
Verify the webhook before starting a response. For slow tool calls, return Content-Type: application/x-ndjson and send an interim spoken acknowledgement, then a final text chunk. Bound tool execution and handle errors with a spoken fallback. The voice timeout still applies between reads; one interim chunk does not allow an unlimited wait. See streaming voice responses.
Next.js API Route
For the App Router, request.arrayBuffer() preserves the raw body. Place this in app/api/webhook/route.js and copy the verifier to lib/verify-webhook.mjs:
Do not use JSON.stringify() on a parsed request to verify the signature. In the Pages Router, disable its body parser and read the original stream instead. Configure an appropriate request-size limit in your hosting layer.
Python API Client
Use the Python SDK for supported methods or authenticated HTTP for endpoints it does not expose. See Authentication and Sub-accounts for account scoping.

