TypeScript SDK

The official AgentPhone TypeScript library provides convenient, typed access to the AgentPhone API from any JavaScript or TypeScript project.

npm

Installation

1npm install agentphone

Quick start

1import { AgentPhoneClient } from "agentphone";
2
3const client = new AgentPhoneClient({ token: "YOUR_API_KEY" });
4
5// Create an agent
6const agent = await client.agents.createAgent({ name: "Support Bot" });
7
8// Buy a number and attach it
9const number = await client.numbers.createNumber();
10await client.agents.attachNumberToAgent({
11 agent_id: agent.id,
12 numberId: number.id,
13});
14
15// Make a call
16await 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:

1import { AgentPhone } from "agentphone";
2
3const 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
2const agents = await client.agents.listAgents({ limit: 20, offset: 0 });
3
4// Create an agent
5const 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
15const agent = await client.agents.getAgent({ agent_id: "agt_abc123" });
16
17// Update an agent
18await client.agents.updateAgent({
19 agent_id: "agt_abc123",
20 name: "Updated Bot",
21 systemPrompt: "New prompt",
22});
23
24// Delete an agent
25await client.agents.deleteAgent({ agent_id: "agt_abc123" });
26
27// Attach a number to an agent
28await client.agents.attachNumberToAgent({
29 agent_id: "agt_abc123",
30 numberId: "num_xyz789",
31});
32
33// List conversations for an agent
34const convos = await client.agents.listAgentConversations({
35 agent_id: "agt_abc123",
36});
37
38// List calls for an agent
39const calls = await client.agents.listAgentCalls({
40 agent_id: "agt_abc123",
41});

Numbers

1// List numbers
2const numbers = await client.numbers.listNumbers({ limit: 20 });
3
4// Provision a new number
5const number = await client.numbers.createNumber();
6
7// Get messages for a number
8const 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)
15await client.numbers.deleteNumber({ number_id: "num_xyz789" });

Conversations

1// List all conversations
2const convos = await client.conversations.listConversations({ limit: 20 });
3
4// Get a conversation with messages
5const convo = await client.conversations.getConversation({
6 conversation_id: "conv_abc123",
7 message_limit: 50,
8});
9
10// Update conversation metadata
11await client.conversations.updateConversation({
12 conversation_id: "conv_abc123",
13 metadata: { customerName: "Jane Doe", orderId: "ORD-12345" },
14});
15
16// Get paginated messages
17const 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
2const calls = await client.calls.listCalls({ limit: 20 });
3
4// Get a call with transcript
5const call = await client.calls.getCall({ call_id: "call_abc123" });
6
7// Make an outbound call
8await 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
16const calls = await client.calls.listCallsForNumber({
17 number_id: "num_xyz789",
18});

Webhooks

1// Get webhook config
2const webhook = await client.webhooks.getWebhook();
3
4// Create or update webhook
5const result = await client.webhooks.createOrUpdateWebhook({
6 url: "https://your-server.com/webhook",
7 contextLimit: 10,
8});
9console.log(result.secret); // save this!
10
11// View delivery history
12const deliveries = await client.webhooks.listDeliveries({ limit: 50 });
13
14// Test webhook
15await client.webhooks.testWebhook();
16
17// Delete webhook
18await client.webhooks.deleteWebhook();

Usage

1// Get usage stats
2const usage = await client.usage.getUsage();
3console.log(usage.numbers.remaining); // remaining phone numbers
4console.log(usage.stats.messagesLast24h); // messages in last 24h

Error handling

1import { AgentPhoneError } from "agentphone";
2
3try {
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).

1await client.agents.listAgents({}, { maxRetries: 0 }); // disable retries

Timeouts

Default timeout is 60 seconds.

1await client.agents.listAgents({}, { timeoutInSeconds: 30 });

Abort requests

1const controller = new AbortController();
2const response = client.agents.listAgents({}, {
3 abortSignal: controller.signal,
4});
5controller.abort();

Raw response access

1const { data, rawResponse } = await client.agents
2 .listAgents()
3 .withRawResponse();
4console.log(rawResponse.headers);

Logging

1import { AgentPhoneClient, logging } from "agentphone";
2
3const 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.