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