***

title: How to Direct SIP Dialing
subtitle: null
slug: dial-to-the-sip-endpoint
description: null
-----------------

In scenarios where your telephony provider does not offer elastic SIP trunking or if you have a more complex telephony setup unsuitable for elastic SIP trunking, you can use direct SIP dialing to our endpoint.

In this scenario, Synthflow does not directly place or receive calls. Rather, your telephony system manages the dialing process to connect with the specified SIP endpoint. You will need to develop the integration logic tailored to your telephony provider's requirements. From Synthflow's standpoint, all call interactions will be treated as incoming, so your integration must explicitly define call direction handling.

The process involves three primary steps:

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

## Step 1: Import a Custom Phone Number

Use the [Create a custom phone number](/api-reference/platform-api/phone-numbers/create-custom-number) API endpoint to import a custom phone number into your workspace.

You will need to provide:

* `workspace_id`: Your workspace ID
* `phone_number`: The phone number to import
* `friendly_name` (optional): A human-readable name for the phone number
* `trunk_username` (optional): SIP trunk username for authentication
* `trunk_pwd` (optional): SIP trunk password for authentication

## Step 2: Create an agent

Use the [Create an agent](/api-reference/platform-api/agents/create-assistant) API endpoint to create an agent and link it to the custom phone number created in Step 1. Ensure the phone number is successfully created before proceeding.

You will need to provide:

* `type`: Set to `inbound` for receiving calls
* `name`: A name for your agent
* `phone_number`: The custom phone number created in Step 1
* `voice_engine_version`: Set to `2.0` for V2 agents
* `agent`: Agent configuration including `llm`, `language`, `prompt`, `greeting_message`, and `voice_id`

## Step 3: 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.

**Javascript Example**:

```javascript Dialing via TWIML
const express = require('express');
const { twiml: { VoiceResponse } } = require('twilio');

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

/**
 * Redirects a call to SynthFlow SIP endpoint.
 */
app.get('/redirect_call', (req, res) => {
  const { Called, CallSid } = req.query;
  
  if (!Called) {
    return res.status(400).send('Missing "Called" query parameter');
  }

  const voiceResponse = new VoiceResponse();
  voiceResponse.dial().sip(`sip:${Called}@${TERMINATION}?X-EI=${CallSid}`);

  res.set('Content-Type', 'text/xml');
  res.status(200).send(voiceResponse.toString());
});

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
```

**Python Example**:

```python Dialing via TWIML
from fastapi import FastAPI, Response
from twilio.twiml.voice_response import VoiceResponse

app = FastAPI()
TERMINATION = "sipin.synthflow.ai:32681"

@app.get("/redirect_call")
async def redirect_call(Called: str, CallSid: str): # pylint: disable=invalid-name
    voice = VoiceResponse()
    voice.dial().sip(f"sip:{Called}@{TERMINATION}?X-EI={CallSid}")
    return Response(str(voice), media_type="text/xml", status_code=200)
```

***

## 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.
