Best Practices
Follow these best practices to build reliable, low-latency integrations with the AgentPhone API.
Voice latency
Streaming is the single biggest improvement you can make to perceived voice latency. Instead of making the caller wait in silence for your full response, send an interim chunk immediately and let TTS start speaking while your server continues processing.
- Always use NDJSON streaming for voice webhooks. Return
Content-Type: application/x-ndjsonso TTS can start on the first chunk. - Send an interim filler chunk like “Let me check on that” before calling your LLM or external API. The caller hears natural speech instead of silence.
- Stream speakable phrases as they become available. Buffer individual model tokens into natural speech, then send each non-final phrase as an NDJSON line with
"interim": true. Omitinterim(or set it tofalse) on the final chunk to finish the turn. - Keep webhook response time under 5 seconds. If processing takes longer, stream an interim acknowledgement within the first second.
- Voice webhooks have a 30-second default timeout (configurable from 5–120 seconds per webhook). If your server doesn’t begin responding in time, the request is cancelled and the caller hears silence for that turn. This is especially important for tool-calling agents — always stream an interim chunk before running external API calls.
If your webhook calls an LLM with tools (e.g., calendar lookup, database query, CRM search), the total round-trip can add up quickly: LLM decides to call a tool (~1-3s) + external API responds (~1-5s) + LLM summarises (~1-3s). Always stream {"text": "Let me check on that.", "interim": true} as your first NDJSON line so the caller hears something immediately while your tools run in the background.
See the Calls guide for the full streaming response format, tool-calling examples, and troubleshooting tips.
Security
- Never expose API keys in client-side code or public repositories. Use environment variables.
- Always verify webhook signatures using the
X-Webhook-SignatureandX-Webhook-Timestampheaders to ensure requests are from AgentPhone. - Check the timestamp — reject requests older than 5 minutes to prevent replay attacks.
- Use HTTPS for all webhook endpoints in production.
- Rotate API keys periodically and revoke unused keys.
See Webhooks > Security for verification code examples.
Webhook reliability
- Acknowledge text events quickly after durable enqueueing. Voice turns require speech in the response; an empty acknowledgement produces silence.
- Handle duplicate deliveries — use the
X-Webhook-IDheader for idempotency. Retries can cause the same event to be delivered multiple times. - Know the event’s retry policy — non-voice
agent.messagedeliveries have up to eight attempts. Other notifications have one attempt, and live voice turns have no delayed retry queue. See retry behavior. - Log delivery IDs, status, and timing for debugging and audit purposes. Use
GET /v1/webhooks/deliveriesto monitor delivery status.
Pagination
Most list endpoints support offset-based pagination with limit and offset parameters. Message endpoints use cursor-based pagination with before and after timestamps.
Offset-based (agents, numbers, conversations, calls)
Cursor-based (messages)
Response format
Always check hasMore before fetching the next page. Use cursor-based pagination for time-ordered data as it’s more efficient than offset-based.
Performance
- Use webhooks instead of polling for real-time updates.
- Use SSE transcript streaming for live call monitoring —
GET /v1/calls/:id/transcript/streamdelivers turns in real time as a Server-Sent Events stream. No polling needed. See Calls > Stream transcript (SSE). - Use cursor-based pagination (
before/after) for loading message history. - Cache frequently accessed data like phone number lists.
- Respect rate limits — implement exponential backoff for retries. Check the
Retry-Afterheader on429responses.
Error handling
- Always check response status before processing data.
- Retry reads with bounded backoff. Do not automatically retry sends, purchases, or calls after an ambiguous timeout or 5xx; they may already have executed.
- Log errors with context for debugging.
- Handle validation errors gracefully — check the
detailsarray for field-level errors.
See the Error Handling page for the full error response format and code examples.

