# Twilio Exit API

If you already have an integration using the Twilio SMS API to send and receive SMS messages (basic send and receive only), you can use this guide to start sending your SMS messages through Bird without having to rewrite your API integration.

The Bird Twilio Exit API allows you to send SMS messages through Bird's Engagement Platform, using your existing Twilio Integration. It acts as an adapter that translates the requests and responses from the Twilio format to Bird format and vice versa.

## How does it work?

Your Twilio integration will send the request using the Twilio API format to the Bird Twilio Exit API. Our API will accept the Twilio formatted request, translate it to Bird's format, process the message, and then translate our response to the Twilio API response format you're expecting, ta-da! You don't have to rewrite all of your integration to start sending messages via Bird.

## Setup the integration

1. Setup your Bird Engagement Platform account
2. Install an SMS channel
3. Copy your main keys
4. Update your Twilio integration URL and keys
5. Setup your Webhook to receive events and incoming messages

### Setup your Bird account

Login to your Bird Engagement Platform account or [Sign up](https://app.messagebird.com/auth/sign-up) for a new account if you don't have one.

### Install an SMS channel

You will need to install an SMS channel on a working phone number, follow this guide on how to [install an SMS channel](https://docs.messagebird.com/applications/enable-channels/sms/install-sms).

### Copy your main keys

#### You will need:

* Your [workspace ID](https://docs.messagebird.com/applications/general/organization-settings/how-to-find-a-workspace-id) (click **Edit workspace** to see your ID)
* [Access key](https://app.messagebird.com/settings/access-keys)
* The phone number of the SMS channel you're sending messages from

### Update your Twilio Integration

Now that you have all the keys you need, you will need to do minimal change to your existing Twilio integration.&#x20;

1. Update the domain name from **<https://api.twilio.com>** to one of the following domains:
   * EU (for Europe region): `https://eu-west-1.twilio.to.api.bird.com`
   * US (for US region): `https://us-west-1.twilio.to.api.bird.com`
2. Update your **AccountSid** to use your Bird **WorkspaceID**
   * To change your WorkspaceID into a AccountSid, simply remove all the dashes and prefix it with AC

     eg: e5181997-2c83-4a0d-959a-612248ff5a19 → ACe51819972c834a0d959a612248ff5a19
3. Update your **Token** to use your Bird **AccessKey**
4. Update the “**From**” number to the phone number you used to setup the SMS channel on our end

That's it! You're ready to start sending SMS messages via Bird.

{% hint style="info" %}
If you are using one of Twilio's SDKs we also advise you to go through the following guides for additional tips:

* [I am using Twilio's PHP SDK](https://docs.bird.com/api/channels-api/supported-channels/programmable-sms/twilio-exit-api/using-twilio-php-sdk)
* [I am using Twilio's Go SDK](https://docs.bird.com/api/channels-api/supported-channels/programmable-sms/twilio-exit-api/using-twilio-go-sdk)
* [I am using Twilio's Ruby SDK](https://docs.bird.com/api/channels-api/supported-channels/programmable-sms/twilio-exit-api/using-twilio-ruby-sdk)
  {% endhint %}

### Setup a Webhook subscription

Setting up a webhook subscription can currently be done via the API using the following endpoint:

{% openapi src="<https://3210271997-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdnJZeZvhOMhDQA8SpjQM%2Fuploads%2FQ8gpihs9epXmbMZgHjmT%2FSMS-Webhook-twilio-2.yml?alt=media&token=1bce1175-4759-4cb7-84c1-c2bed4f949b4>" path="/organizations/{organization\_id}/workspaces/{workspace\_id}/webhook-subscription" method="post" %}
[SMS-Webhook-twilio-2.yml](https://3210271997-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdnJZeZvhOMhDQA8SpjQM%2Fuploads%2FQ8gpihs9epXmbMZgHjmT%2FSMS-Webhook-twilio-2.yml?alt=media\&token=1bce1175-4759-4cb7-84c1-c2bed4f949b4)
{% endopenapi %}

Here is an example of such a request

<pre class="language-json"><code class="lang-json">{
<strong>    "service": "channels",
</strong>    "event": "sms.inbound",
<strong>    "eventFilters": [{
</strong>        "key": "channelId",
        "value": "{{channel_id}}"
    }],
    "template": "twilio",
    "url": "https://your.site/webhook/url",
    "signingKey": "your-secret-signing-key"
}
</code></pre>

Unlike Twilio, Bird does not sign webhooks with your AccessKey/ApiToken. Instead, webhooks are signed using the `SigningKey` specified during your webhook subscription.

{% hint style="warning" %}
Make sure you keep your SigningKey safe, as it's used to verify the authenticity of the webhook requests.
{% endhint %}

Full webhooks API documentation can be found [here](https://docs.messagebird.com/api/notifications-api/api-reference/webhooks).

#### Setup a Webhook subscription to receive incoming messages

Setting up a webhook subscription is an optional step, and is only needed if you need to receive inbound messages (2-way messaging use case). Much like Twilio, Bird's webhooks are set up by channel instance. So, for SMS, webhooks are set up by number.

To receive webhooks for incoming SMS messages on your SMS channel, you need to set up a webhook subscription by using the above  request. For incoming messages, set the event to `sms.inbound`. You can choose to receive this webhook in the twilio format by setting the template parameter to `twilio`.

#### Setup webhook subscription for status reports for outbound messages

You can also set up webhooks for status reports for outbound messages by using the `sms.outbound` event. This is required to make webhooks work for outbound messages, since we need a singing key to sign the request.

When you use `StatusCallback` to subscribe to webhooks for outgoing messages, this URL overwrites the default URL set in the above request and sets the template to use the twilio template, regardless of the subscription settings.

Bird also employs a more secure way of signing requests. When using the twilio template, we will sign our requests in two ways: the Twilio method, using the Twilio header, and the Bird method, with the Bird header.<br>

## Twilio Exit API Explained

Our Twilio Exit API translates requests using the Twilio format to the Bird format, before sending it to the Bird API. And on receiving a response form our API, it translates it back into the Twilio format. This section explains which endpoints are supported and how we translate and map fields between both APIs.

### Endpoints

Currently, the Twilio Exit API is limited to sending SMS messages exclusively through your Twilio Integration. The supported endpoints are:

<table><thead><tr><th width="171">Operation</th><th>Endpoint</th></tr></thead><tbody><tr><td>Send message</td><td><code>POST /2010-04-01/Accounts/{{workspaceID}}/Messages.json</code></td></tr><tr><td>Fetch message</td><td><code>GET /2010-04-01/Accounts/{{workspaceID}}/Messages/{{messageID}}.json</code></td></tr><tr><td>Delete message</td><td><code>DELETE /2010-04-01/Accounts/{{workspaceID}}/Messages/{{messageID}}.json</code></td></tr><tr><td>List messages</td><td><code>GET /2010-04-01/Accounts/{{workspaceID}}/Messages.json</code></td></tr></tbody></table>

### Message response

The message object's response resembles Twilio's, though some fields may be empty. Below is an example of our responses:

<table><thead><tr><th width="173">JSON field</th><th width="160">Status</th><th>Bird field</th><th>Example</th></tr></thead><tbody><tr><td><code>body</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>body.text.text</code></td><td><code>"body": "Hello World!"</code></td></tr><tr><td><code>direction</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>direction</code></td><td><code>"direction": "outbound-api"</code></td></tr><tr><td><code>from</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>sender.connector.identifierValue</code></td><td><code>"from": "+1234567890"</code></td></tr><tr><td><code>date_updated</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>updatedAt</code></td><td><code>"date_updated": "Fri, 18 Aug 2023 09:13:21 +0000"</code></td></tr><tr><td><code>uri</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td>-</td><td><code>"uri": "/2010-04-01/Accounts/{account_sid}/Messages/{message_sid}.json"</code></td></tr><tr><td><code>account_sid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td>-</td><td><code>"account_sid":"ACe22001f4705b4f97b58b897891f60000"</code></td></tr><tr><td><code>to</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><p><code>receiver.contacts.</code></p><p><code>identifierValue</code></p></td><td><p><code>"to":"+31623456789"</code></p><p>It is possible to send to more than 1 recipient in Bird. We will always return the first recipient.</p></td></tr><tr><td><code>date_created</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>createdAt</code></td><td><code>"date_created": "Fri, 18 Aug 2023 09:13:21 +0000"</code></td></tr><tr><td><code>status</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>status</code></td><td><p><code>"status": "queued"</code></p><p><code>"status": "delivered"</code></p></td></tr><tr><td><code>sid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td><code>id</code></td><td><code>"sid": "SM2c315abb9c2f418ea6b0d5bedc530000"</code></td></tr><tr><td><code>api_version</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td>-</td><td><code>"api_version": "2010-04-01"</code></td></tr><tr><td><code>subresource_uris</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td><td>-</td><td><code>"subresource_uris": {"media": "....."}</code></td></tr><tr><td><code>subresource_uris.media</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Returned but not supported</td><td>-</td><td><p><code>"media": "/2010-04-</code></p><p><code>01/Accounts/ACe22001f4705b4f97b58b897891f60000/Messages/S</code></p><p><code>M2c315abb9c2f418ea6b0d5bedc530000/Media.json"</code></p></td></tr><tr><td><code>subresource_uris.feedback</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Not returned</td><td>-</td><td>-</td></tr><tr><td><code>num_media</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Always <code>"0"</code> for SMS</td><td>-</td><td><p><code>"num_media": "0"</code></p><p></p><p>SMS has no media</p></td></tr><tr><td><code>messaging_service_sid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Always <code>null</code> </td><td>-</td><td><p><code>"messaging_</code></p><p><code>service_sid": null</code></p></td></tr><tr><td><code>price</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>null</code></td><td>-</td><td><code>"price": null</code></td></tr><tr><td><code>price_unit</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>""</code></td><td>-</td><td><code>"price_unit": ""</code></td></tr><tr><td><code>error_code</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>null</code></td><td>-</td><td><code>"error_code": null</code></td></tr><tr><td><code>error_message</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>null</code></td><td>-</td><td><code>"error_message": null</code></td></tr><tr><td><code>num_segments</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>"1"</code></td><td>-</td><td><code>"num_segments": "1"</code></td></tr><tr><td><code>date_sent</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Always <code>null</code></td><td>-</td><td><code>"date_sent": null</code></td></tr></tbody></table>

### Create message

| Form field                       | Status                                                                          |
| -------------------------------- | ------------------------------------------------------------------------------- |
| `AccountSid` (Path)              | :green\_circle: Use your Bird `WorkspaceID`                                     |
| `From` (Required)                | :green\_circle: Supported. Must be the number of an active SMS channel in Bird. |
| `To` (Required)                  | :green\_circle: Supported                                                       |
| `Body` (Optional)                | :green\_circle: Supported                                                       |
| `StatusCallback` (Optional)      | :green\_circle: Supported                                                       |
| `ProvideFeedback` (Optional)     | :yellow\_circle: Ignored                                                        |
| `Attempt` (Optional)             | :yellow\_circle: Ignored                                                        |
| `SmartEncoded` (Optional)        | :orange\_circle: Always true, returns error if false                            |
| `ShortenUrls` (Optional)         | :orange\_circle: Always true, returns error if false                            |
| `SendAsMms` (Optional)           | :orange\_circle: Always true, returns error if false                            |
| `ApplicationSid` (Optional)      | :red\_circle: Returns error if used                                             |
| `MaxPrice` (Optional)            | :red\_circle: Returns error if used                                             |
| `ValidityPeriod` (Optional)      | :red\_circle: Returns error if used                                             |
| `ForceDelivery` (Optional)       | :red\_circle: Returns error if used                                             |
| `ContentRetention` (Optional)    | :red\_circle: Returns error if used                                             |
| `AddressRetention` (Optional)    | :red\_circle: Returns error if used                                             |
| `PersistentAction` (Optional)    | :red\_circle: Returns error if used                                             |
| `ScheduleType` (Optional)        | :red\_circle: Returns error if used                                             |
| `SendAt` (Optional)              | :red\_circle: Returns error if used                                             |
| `ContentVariables` (Optional)    | :red\_circle: Returns error if used                                             |
| `RiskCheck` (Optional)           | :red\_circle: Returns error if used                                             |
| `MessagingServiceSid` (Optional) | :red\_circle: Returns error if used                                             |
| `MediaUrl` (Optional)            | :red\_circle: Returns error if used                                             |
| `ContentSid` (Optional)          | :red\_circle: Returns error if used                                             |

#### Response

See [message response](#message-responses).

### List messages

#### Request

Filtering messages can be done using the following query string parameters:

<table><thead><tr><th width="245">Query parameter</th><th>Status</th></tr></thead><tbody><tr><td><code>AccountSid</code> (Path)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Use your Bird <code>WorkspaceID</code></td></tr><tr><td><code>Page</code> (Optional)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>PageSize</code> (Optional)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Supported, but with a maximum of 100</td></tr><tr><td><code>PageToken</code> (Optional)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e1">🟡</span> Supported, but not a UUID<br><br>Twilio example: <code>PASMd37199c9492c41d6aba793e6420f70df</code><br><br>Adapter example: <code>WyJ3b3Jrc3BhY2UjNjViNzU1ZTItNGRmNS00MTE0LTg3NWItNmZmY2Y5NzllNDk2I21lc3NhZ2VzI2VlOThhMTZlLTMyNDEtNDhkMy04Y2Q1LTg5OTBlMmM2OTkwMSIsIm1lc3NhZ2UiLCJjaGFubmVsIzVlNzg3ZGQxLWQzNjEtNDU3Ni1hY2MwLTNiOWUxMTRmMWY4OCNtZXNzYWdlcyIsIm1lc3NhZ2UjMTY5MjExODg4ODQ0NCNlZTk4YTE2ZS0zMjQxLTQ4ZDMtOGNkNS04OTkwZTJjNjk5MDEiXQ==</code></td></tr><tr><td><code>From</code> (Optional)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Required parameter</td></tr><tr><td><code>To</code> (Optional)</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Returns error if used</td></tr><tr><td><code>DateSent</code> (Optional )</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Returns error if used</td></tr></tbody></table>

#### Response

<table><thead><tr><th width="248">JSON response</th><th>Channels field</th></tr></thead><tbody><tr><td><code>page</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>page_size</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>start</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>end</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>first_page_url</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>previous_page_url</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>null</code></td></tr><tr><td><code>url</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>next_page_url</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>messages</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported: See message response</td></tr></tbody></table>

### Webhooks

Request for outgoing messages:

<table><thead><tr><th width="251">Key</th><th>Description</th></tr></thead><tbody><tr><td><code>AccountSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>SmsSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>MessageSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>SmsStatus</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>MessageStatus</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>To</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>From</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>ApiVersion</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>RawDlrDoneDate</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f534">🔴</span> Not set</td></tr></tbody></table>

Request for incoming messages:

<table><thead><tr><th width="255">Key</th><th>Description</th></tr></thead><tbody><tr><td><code>AccountSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>SmsSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>SmsMessageSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>MessageSid</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>SmsStatus</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>Body</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>From</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>To</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>ApiVersion</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e2">🟢</span> Supported</td></tr><tr><td><code>NumMedia</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>"0"</code></td></tr><tr><td><code>NumSegments</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>"1"</code></td></tr><tr><td><code>ToCountry</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>ToState</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>ToZip</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>FromCountry</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>FromState</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>FromCity</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr><tr><td><code>FromZip</code></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f7e0">🟠</span> Always <code>""</code></td></tr></tbody></table>
