Forward calls to the SIP trunk

View as Markdown

This guide walks you through the process of initializing the call via the SIP trunk. The setup consists of three primary steps:

  1. Create the call
  2. Forward call to SIP Trunk

Final Step: Initializing the call

Creating the call

Endpoint:
POST /v2/prepare_inbound

Headers:

JSON
{
"Content-Type": "application/json",
"Authorization": "Bearer {TOKEN}"
}

Request Body

JSON
{
"from_number": "",
"to_number": "",
"model_id": "",
"workspace_id": "",
"custom_variables": {
"name": "value"
},
}

Response Format

JSON
{
"call_status": "registered",
"synthflow_call_id": "a29407fb-1bf2-4be6-9d1d-f501c3dd9a2b",
"direction": "inbound",
"model_id": "1744274157255x646200691892837600",
"workspace_id": null,
"custom_variables": {
"name": "erfan",
"job": "backend"
},
"duration": null,
"end_call_reason": null,
"judge_results": null,
"is_voicemail": null,
"lead_phone_number": "+15075785607",
"voicemail_not_detected_during_call_but_detected_in_post_call": null,
"voicemail_during_call_but_not_voicemail_in_post_call": null,
"timeline": [
],
"twilio_call_sid": null,
"egress_recording_url": null,
"agents_used": [
],
"original_payload": null,
"executed_actions": [
],
"prompt_variables": {
},
"voicemail_message_left": null,
"recording_sid": null,
"secondary_call_id": null,
"recording_url": null,
"payload_received": null,
"language": null,
"type_of_call": null,
"recording_duration": null,
"transcript": null,
"error_message_payload_validation": null,
"error_message": null,
"node_and_stage_time_track": {
},
"start_time": 1744309477691,
"campaign_name": null,
"lead_name": null,
"labels": [
],
"agent_phone_number": "",
"deployment_type": null,
"timezone": null
}

synthflow_call_id This is the call ID that should be sent to the SIP trunk via the X-EI header.

call_status Indicates the status of the call. A value of registered means that SynthFlow is operational, while failed indicates a service outage.

Forward call to SIP Trunk

After the call is created on SynthFlow, redirect Twilio calls to it using TwiML commands. Make sure to include the received synthflow_call_id in the SIP headers using the X-EI key, formatted as follows:

X-EI=S.synthflow_call_id;

Regex pattern: (S|E)\.([^\;]+);

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 = "sip.synthflow.ai:32681";
7
8app.get('/redirect_call', (req, res) => {
9 const phone_number = "" // Custom phone number
10 const call_id = "" // synthflow_call_id
11
12 const voice = new VoiceResponse();
13 voice.dial().sip(`sip:${phone_number}@${TERMINATION}?X-EI=S.${call_id};`);
14
15 res.type('text/xml');
16 res.send(voice.toString());
17});
18
19app.listen(PORT, () => {
20 console.log(`Server running on port ${PORT}`);
21});
python
from fastapi import FastAPI, Response, Request
app = FastAPI()
TERMINATION = "sip.synthflow.ai:32681"
@app.get("/redirect_call")
async def redirect_call(CallSid: str, From: str):
phone_number = "" # Custom Phone number
call_id = await create_call() # Creating call and returning synthflow_call_id
voice = VoiceResponse()
voice.dial().sip(f"sip:{phone_number}@{TERMINATION}?X-EI=S.{call_id};")
return Response(str(voice), media_type="text/xml", status_code=200)

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