Building your first voice AI agent

In this guide, we’ll create a simple agent that can make outbound calls and receive inbound calls. The example code for this guide is available here.

1. Create an API key

Navigate to the API keys page, create a new API key, and store it in a .env.local file.

.env.local
1PHONIC_API_KEY="your_api_key"

2. Install the SDK

Install the Phonic SDK and dotenv to load the API key from the .env.local file.

1npm install phonic dotenv

3. Initialize the client

1import { PhonicClient } from "phonic";
2import { config } from "dotenv";
3
4config({ path: ".env.local" });
5
6const client = new PhonicClient({
7 apiKey: process.env.PHONIC_API_KEY,
8});

4. Create an agent

Let’s create an agent and assign it a random phone number.

1await client.agents.create({
2 name: "my-first-agent",
3 phone_number: "assign-automatically",
4});

You should see now my-first-agent on the Agents page.

5. Make an outbound call

Let’s make the agent call you. Specify your phone number, the agent name, and a welcome message:

1await client.conversations.outboundCall({
2 to_phone_number: "YOUR_PHONE_NUMBER", // e.g. +19189391262
3 config: {
4 agent: "my-first-agent",
5 welcome_message: "Hello, how can I help you?",
6 }
7});

Now, pick up the phone and chat with your agent!

6. Make an inbound call

Grab the agent’s phone number on the Agents page, and give it a call!

See here for a complete code example.