TypeScript SDK
The official AgentPhone TypeScript library provides convenient, typed access to the AgentPhone API from any JavaScript or TypeScript project.
Installation
npm install agentphone
Quick start
import { AgentPhoneClient } from "agentphone";const client = new AgentPhoneClient({ token: "YOUR_API_KEY" });// Create an agentconst agent = await client.agents.createAgent({ name: "Support Bot" });// Buy a number and attach itconst number = await client.numbers.createNumber();await client.agents.attachNumberToAgent({agent_id: agent.id,numberId: number.id,});// Make a callawait client.calls.createOutboundCall({agentId: agent.id,toNumber: "+15559876543",initialGreeting: "Hi, this is Support Bot!",});
Request and response types
The SDK exports all request and response types under the AgentPhone namespace:
import { AgentPhone } from "agentphone";const request: AgentPhone.CreateAgentRequest = {name: "Sales Agent",voiceMode: "hosted",systemPrompt: "You are a helpful sales assistant.",};
Resources
Agents
// List all agentsconst agents = await client.agents.listAgents({ limit: 20, offset: 0 });// Create an agentconst agent = await client.agents.createAgent({name: "Support Bot",description: "Handles customer inquiries",voiceMode: "hosted",systemPrompt: "You are a helpful support agent.",beginMessage: "Hello! How can I help you?",});// Get agent detailsconst agent = await client.agents.getAgent({ agent_id: "agt_abc123" });// Update an agentawait client.agents.updateAgent({agent_id: "agt_abc123",name: "Updated Bot",systemPrompt: "New prompt",});// Delete an agentawait client.agents.deleteAgent({ agent_id: "agt_abc123" });// Attach a number to an agentawait client.agents.attachNumberToAgent({agent_id: "agt_abc123",numberId: "num_xyz789",});// List conversations for an agentconst convos = await client.agents.listAgentConversations({agent_id: "agt_abc123",});// List calls for an agentconst calls = await client.agents.listAgentCalls({agent_id: "agt_abc123",});
Numbers
// List numbersconst numbers = await client.numbers.listNumbers({ limit: 20 });// Provision a new numberconst number = await client.numbers.createNumber();// Get messages for a numberconst messages = await client.numbers.getMessages({number_id: "num_xyz789",limit: 50,before: "2024-01-01T00:00:00Z", // cursor-based pagination});// Release a number (irreversible)await client.numbers.deleteNumber({ number_id: "num_xyz789" });
Conversations
// List all conversationsconst convos = await client.conversations.listConversations({ limit: 20 });// Get a conversation with messagesconst convo = await client.conversations.getConversation({conversation_id: "conv_abc123",message_limit: 50,});// Update conversation metadataawait client.conversations.updateConversation({conversation_id: "conv_abc123",metadata: { customerName: "Jane Doe", orderId: "ORD-12345" },});// Get paginated messagesconst messages = await client.conversations.getConversationMessages({conversation_id: "conv_abc123",limit: 50,before: "2024-01-01T00:00:00Z", // cursor-based pagination});
Calls
// List all callsconst calls = await client.calls.listCalls({ limit: 20 });// Get a call with transcriptconst call = await client.calls.getCall({ call_id: "call_abc123" });// Make an outbound callawait client.calls.createOutboundCall({agentId: "agt_abc123",toNumber: "+15559876543",initialGreeting: "Hello!",systemPrompt: "You are a support agent helping with order inquiries.",});// List calls for a specific numberconst calls = await client.calls.listCallsForNumber({number_id: "num_xyz789",});
Webhooks
// Get webhook configconst webhook = await client.webhooks.getWebhook();// Create or update webhookconst result = await client.webhooks.createOrUpdateWebhook({url: "https://your-server.com/webhook",contextLimit: 10,});console.log(result.secret); // save this!// View delivery historyconst deliveries = await client.webhooks.listDeliveries({ limit: 50 });// Test webhookawait client.webhooks.testWebhook();// Delete webhookawait client.webhooks.deleteWebhook();
Usage
// Get usage statsconst usage = await client.usage.getUsage();console.log(usage.numbers.remaining); // remaining phone numbersconsole.log(usage.stats.messagesLast24h); // messages in last 24h
Error handling
import { AgentPhoneError } from "agentphone";try {await client.agents.createAgent({ name: "Bot" });} catch (err) {if (err instanceof AgentPhoneError) {console.log(err.statusCode); // e.g. 422console.log(err.message);console.log(err.body);console.log(err.rawResponse);}}
Advanced
Retries
The SDK automatically retries on 408, 429, and 5xx errors with exponential backoff (default: 2 retries).
await client.agents.listAgents({}, { maxRetries: 0 }); // disable retries
Timeouts
Default timeout is 60 seconds.
await client.agents.listAgents({}, { timeoutInSeconds: 30 });
Abort requests
const controller = new AbortController();const response = client.agents.listAgents({}, {abortSignal: controller.signal,});controller.abort();
Raw response access
const { data, rawResponse } = await client.agents.listAgents().withRawResponse();console.log(rawResponse.headers);
Logging
import { AgentPhoneClient, logging } from "agentphone";const client = new AgentPhoneClient({token: "YOUR_API_KEY",logging: {level: logging.LogLevel.Debug,logger: new logging.ConsoleLogger(),silent: false, // defaults to true},});
Runtime compatibility
The SDK works in Node.js 18+, Vercel, Cloudflare Workers, Deno 1.25+, Bun 1.0+, and React Native.

