> For a complete page index, fetch https://docs.synthflow.ai/llms.txt. For full documentation content, fetch https://docs.synthflow.ai/llms-full.txt.

# Forward calls to the SIP trunk

> Forward calls into a SIP trunk from Synthflow and understand the steps needed to initialize the handoff.

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:**

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

**Request Body**

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

**Response Format**

```Text 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:

```javascript
const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;

const app = express();
const PORT = process.env.PORT || 3000;
const TERMINATION = "sip.synthflow.ai:32681";

app.get('/redirect_call', (req, res) => {
  const phone_number = "" // Custom phone number
  const call_id = "" // synthflow_call_id
  
  const voice = new VoiceResponse();
  voice.dial().sip(`sip:${phone_number}@${TERMINATION}?X-EI=S.${call_id};`);
  
  res.type('text/xml');
  res.send(voice.toString());
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
```

```Text 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.