Creating a V2 agent via API

This guide walks you through the process of creating a V2 agent using the SynthFlow API. The setup consists of three primary steps:

  1. Creating a custom phone number
  2. Creating the agent and linking it to the phone number
  3. Dialing to the SIP Endpoint

API Base URL: https://api.synthflow.ai

Step 1: Create a Custom Phone Number

Endpoint:
POST /v2/custom-numbers

Headers:

1{
2 "Content-Type": "application/json",
3 "Authorization": "Bearer {TOKEN}"
4}

Request Body:

1{
2 "workspace_id": "",
3 "phone_number": "",
4 "friendly_name": "test",
5 "trunk_username": "test123", // Optional
6 "trunk_pwd": "dflkvn23REFD#@" // Optional
7}

trunk_username and trunk_pwd are optional and can be omitted unless required for your configuration.


Step 2: Create a V2 agent

This step registers a V2 agent and links it to the custom phone number created in Step 1. Ensure the phone number is successfully created before proceeding.

Endpoint:
POST /v2/agents

Headers:

1{
2 "Content-Type": "application/json",
3 "Authorization": "Bearer {TOKEN}"
4}

Request Body:

1{
2 "type": "inbound",
3 "agent": {
4 "llm": "gpt-4-turbo",
5 "language": "en-US",
6 "prompt": "hey",
7 "greeting_message": "hi",
8 "voice_id": "qTj9lsKOMtF3uVefOvPi"
9 },
10 "name": "test 12",
11 "phone_number": "",
12 "voice_engine_version": "2.0" // Required for V2
13}

For additional fields and configurations, refer to the official API documentation.


Final Step: Configure Twilio

After the trunk is set up on SynthFlow, redirect Twilio calls to it using TwiML commands. Be sure to include the GHL unique call ID or Twilio Call SID in the SIP header using the X-EI key.

Here’s a code snippet for redirecting a call:

1const express = require('express');
2const VoiceResponse = require('twilio').twiml.VoiceResponse;
3
4const app = express();
5const PORT = process.env.PORT || 3000;
6const TERMINATION = "sipin.synthflow.ai:32681";
7
8app.get('/redirect_call', (req, res) => {
9 const { Called, CallSid } = req.query;
10
11 const voice = new VoiceResponse();
12 voice.dial().sip(`sip:${Called}@${TERMINATION}?X-EI=${CallSid}`);
13
14 res.type('text/xml');
15 res.send(voice.toString());
16});
17
18app.listen(PORT, () => {
19 console.log(`Server running on port ${PORT}`);
20});

Pre-Actions with Custom Variables

You can use Pre-Actions to fetch custom variables via API and incorporate them into the agent prompt for more personalized responses by using the <twilio_call_sid> verb in API request.

Note: The transfer call will work the same as before.