# Start a workflow from Twilio incoming messages and calls

> Run a workflow when a message or a call reaches your Twilio number, with a Webhook block set to Twilio incoming messages and calls.

To run a workflow when someone sends a message to your Twilio number or calls it, add a **Webhook** block whose **Source** is **Twilio incoming messages and calls** and give its URL to the number as its incoming webhook. Twilio posts one request per message or call. Each request starts its own run.

This source receives the request Twilio makes to ask what to do with a message or a call. Reports that a message you sent was delivered, or that a call ended, are status callbacks. They are set in a different place in Twilio and go to a block of their own: [start a workflow from Twilio events](https://outwin.ai/docs/build/connect-an-api/start-a-workflow-from-twilio-events/). Use one block and one URL for each kind.

Twilio acts on the reply to this request: the reply holds instructions (TwiML) that send a message back or speak to the caller. A workflow cannot write it, because a run starts after the reply is sent. OutWin answers Twilio at once, with instructions that do nothing or with one fixed text you set on the block. That text is the only answer the person gets: a later message from the workflow is [not reliably possible today](#answer-from-the-workflow).

You need a Twilio account whose Auth Token you can see, a Twilio phone number, edit access to the workflow's project, and the member role or higher in the workspace to save the token on a connection. The Twilio steps happen in the Twilio Console.

## Add the Webhook block and generate its URL

1. In a draft workflow, add a **Webhook** block. The block palette also lists **Twilio incoming messages and calls webhook** in the **Start** group, which adds the block with its source already set.
2. Under **Source**, choose **Twilio incoming messages and calls**. The line under the setting reads "Verifies the signature · one run per event · skips duplicates".
3. Under **Webhook URL**, select **Generate URL**, then copy the URL.

The badge beside the URL reads "Runs start after publish". The URL contains a token, and anyone who has the URL can send requests to it. Keep it out of shared documents.

## Give the URL to your Twilio number

The URL goes into the number's incoming webhook. It is not a status callback, and it is not the `StatusCallback` parameter of a request to Twilio's API.

1. In the Twilio Console, open the page of the phone number.
2. For messages, go to the number's messaging settings. At **A message comes in**, select **Webhook** and paste the URL you copied.
3. For calls, do the same in the number's voice settings, at **A call comes in**.
4. Set the method of each webhook to `POST`, and save.

If the number belongs to a Messaging Service that handles its incoming messages, set the URL as that service's incoming webhook in place of step 2.

Paste the URL exactly as you copied it. Twilio's signature covers the URL the webhook is set to, so a request that reaches the block through any other address fails the check. The block does not accept a request sent with `GET`. Twilio treats that as a failed request.

One block can receive both the messages and the calls of a number. Give them two blocks, with two URLs, when they need different reply texts or different workflows.

## Save the Auth Token on a connection

Twilio signs each request with the account's **Auth Token**, the same token it signs status callbacks with. That token is the same for every request of the account, so OutWin does not save it on the block. You save it once on an API connection, and each Webhook block that receives from the account chooses that connection. If a connection already holds the token for a [Twilio events](https://outwin.ai/docs/build/connect-an-api/start-a-workflow-from-twilio-events/) block, choose that connection in step 4 and skip the first three steps.

1. In the Twilio Console, copy the **Auth Token** from the account's dashboard.
2. In OutWin, open **Connections**. If the workspace has no API connection for Twilio, [add one](https://outwin.ai/docs/build/connect-an-api/#add-an-api-connection). In the dialog, leave **Base URL** empty and set **Authentication** to **None**. The connection then only receives events. This guide gives no steps that call Twilio's API, so the connection needs no base URL or credential.
3. On the connection's page, under **Receiving events**, paste the token into the field that shows "Paste the signing secret" and select **Save**. Paste it here even when the connection already signs in to Twilio's API with the same token: the two are saved apart. The section then shows only a masked hint and the saved date, with **Replace** and **Remove**.
4. In the block's settings, under **Connection that holds the auth token**, choose the connection. Each connection in the list is marked "secret saved" or "no secret yet".

The line under the setting then starts "Verifying with the secret ending", followed by the hint. Under **Set up**, the second line changes to "The auth token is saved: deliveries are verified."

In a workspace with no API connection, the setting shows "No API connections yet." in place of the list, with an **Add one** link that opens **Connections**. If you choose a connection that has no secret, the line reads "This connection has no auth token yet." with a **Save it on the connection** link to its page.

The block checks signatures made with the Auth Token only. Twilio can also sign an account's webhooks with a shared key. A request signed that way is listed as **Refused**.

Twilio's signature does not mark where a field's name ends and its value begins, and it holds no time. Someone who has captured one signed request can send it again, also with its fields divided differently, and it still passes the check. The block therefore runs only a request that carries a message or call id in Twilio's form. A copy with its id cut away or changed is listed as **Filtered out**, and an exact copy of a request that ran is listed as **Duplicate**. Neither starts a run.

With no connection chosen, or with a connection that has no secret, the block still starts runs, and nothing is verified: anyone who has the URL can start the workflow. Publishing then shows a warning that ends "has no signing secret saved, so its deliveries are not verified: anyone who has this URL can start this workflow". The warning does not block publishing.

## Set what the person gets at once

The **Reply** setting holds the one text OutWin answers Twilio with. The field shows "Got it, working on it." as a placeholder and takes up to 1,500 characters, because Twilio limits the text of a message.

| **Reply** | What OutWin sends | What the person gets |
| --- | --- | --- |
| Empty | TwiML that holds no instruction. | Nothing. Twilio sends no message back. |
| A text, and the request is a message | TwiML that tells Twilio to send the text as a message. | The text, as a message from your number. |
| A text, and the request is a call | TwiML that tells Twilio to speak the text. | The text, spoken to the caller. |

The reply is always TwiML, sent with status `200` and the content type `text/xml`. OutWin reads the request only far enough to tell a call from a message: a request with a `CallSid` field and no `MessageSid` or `SmsSid` is a call. The text is otherwise the same for every request. It takes no expressions, so it cannot repeat what the person wrote or say what the workflow found.

The lines under the field read "Twilio sends this text back as a message, or speaks it to a caller. It is the same every time, and is sent only when the event is queued to run: not on a draft, a paused workflow, or an event the filter turns away." and "Left empty, nothing is sent back."

OutWin sends the text only when the request is queued to run. On a draft, on a paused workflow, for a request the filter turns away, for a duplicate, and when too many events are already waiting, it sends the TwiML that holds no instruction. Credits and the subscription are checked later, when the run starts. A workspace that is out of credits is therefore found after the reply: the person gets the text and no run follows.

A call follows only the instructions in the reply. The run starts after the reply is sent, so a workflow cannot speak to the caller or keep the call open.

## Choose which requests start a run

Twilio sends no event type with a message or a call. Under **Events that start a run**, the block shows "Every request starts a run: Twilio incoming messages and calls sends no event type to choose by." in place of a list.

To tell a call from a message, or to narrow by any other field, select **Add a condition** under **Only when**. For a block that receives both and runs only for messages, enter the field `body.MessageSid` and choose **is present**. For calls only, use `body.CallSid`. A request that does not pass is listed as **Filtered out** and starts no run. [Webhook settings](https://outwin.ai/docs/reference/triggers-approvals-and-waits/#webhook-settings) lists the comparisons.

## Send a test and check what arrived

1. Send a message to the Twilio number from a phone, or call it.
2. In OutWin, look under **Recent deliveries** in the block's settings.

**Recent deliveries** lists every request that reached the URL, including while the workflow is a draft. On a draft each request is listed as **Dropped** with "The workflow is not published, so nothing ran." It starts no run, it is not run later, and the person gets nothing back. The last line under **Set up** changes from "Waiting for the first event…" to "Received an event. Dropped."

With a secret saved on the chosen connection, a request listed as **Dropped** on a draft has passed the signature check. A delivery that fails the check is listed as **Refused**.

If you have edit access, select a request to see what the block passed on. The list is kept for seven days, and the data of the newest 20 events is kept.

## Read the message or call in the next block

Each run receives one Twilio request. Twilio sends it as a form, and the block passes the form's fields on as the body. An HTTP request block or a database block directly after the Webhook block reads it with these expressions. An AI block such as **Think** receives the whole value as its input.

| Expression | What it holds |
| --- | --- |
| `{{input.body}}` | The fields Twilio sent. A message has fields such as `MessageSid`, `AccountSid`, `From`, `To`, and `Body`. A call has `CallSid`, `AccountSid`, `From`, `To`, and `CallStatus`. Twilio adds others by channel. |
| `{{input.body.From}}` | The number, or the channel address, the message or call came from. |
| `{{input.body.Body}}` | The text of a message. A call has none. |
| `{{input.body.MessageSid}}` | Twilio's id of the message. A call has none. |
| `{{input.body.CallSid}}` | Twilio's id of the call. A message has none. |
| `{{input.event.id}}` | The `MessageSid` of a message, or the `CallSid` of a call. A request without one in Twilio's form starts no run. |
| `{{input.delivery.verified}}` | `true` only when the signature was checked and passed. |

`{{input.event.type}}` holds nothing for this source. A block further along reads the same values through the Webhook block's name, as in `{{steps["Twilio incoming messages and calls"].body.From}}` for a block with that name. [Data and context](https://outwin.ai/docs/reference/data-and-context/) describes what each block passes on.

## Answer from the workflow

The **Reply** text is the only answer this source sends today. A later message would go through Twilio's REST API, which takes a form body. The HTTP request block sends a **Text** body as written and does not form-encode the values that expressions fill in. A phone number such as `{{input.body.From}}` starts with `+`, which a form reads as a space, and an `&` in a text divides the field. A request built that way is not reliable, so this guide gives no steps for it.

A workflow can still act on the message or call in every other way: save it with a database block, pass it to an AI block, or call another service's API that takes JSON.

## Publish and check the runs

[Publish the workflow](https://outwin.ai/docs/run/publish-and-start/). From then on, each request that passes the filter starts a run, and the person gets the **Reply** text if you set one. On a published workflow the block's settings are read-only, including **Reply** and the choice of connection, and **Recent deliveries** is in the block's inspector. You can still replace the secret on the connection's page.

| Outcome | What happened |
| --- | --- |
| **Ran** | The message or call started a run. |
| **Waiting** | 10 runs of the workflow are already active. The request starts a run when one of them ends. A request that waits longer than an hour is listed as **Dropped**. |
| **Duplicate** | A request for the same message or call already ran in the last 72 hours. The line reads "This event was already received and run, so it did not run again." |

Twilio sends later requests about a call, such as its status callbacks, with the same call id. Do not point them at this block's URL: after the first has run, the others are listed as **Duplicate**.

Receiving a request costs no credits, and neither does one that is filtered out or a duplicate. A run that starts uses credits for the blocks that run in it.

## If nothing runs

Open **Recent deliveries** and read the outcome and the sentence beside the newest request.

| What you see | Cause | Fix |
| --- | --- | --- |
| "Nothing has arrived yet." | No request the block accepts has reached the URL. | Check that the number's incoming webhook is the block's current URL and that its method is `POST`. A URL set as a status callback receives no incoming messages or calls. **Rotate token** on a published workflow changes the URL, and the old one stops working. |
| **Dropped** with "The workflow is not published, so nothing ran." | The workflow is a draft. The person got nothing back. | Publish the workflow. Requests that arrived before then are not run later. |
| **Dropped** with "The workflow is paused, so nothing ran." | The workflow is paused. | Resume the workflow. Requests that arrived while it was paused are not run later. |
| **Dropped** with "The workspace is out of credits, so nothing ran." | The workspace has no credits left. The person got the **Reply** text, because the reply is sent first. | Add credits. The message or call is not run later. |
| **Filtered out** | A row under **Only when** does not hold for this request. | Select the request to see its fields, and correct the row. The settings are read-only on a published workflow. |
| **Filtered out** with "The request carries no id of the form this sender uses, so it is not one the sender made as it is. Nothing ran." | The request has no `MessageSid`, `SmsSid`, or `CallSid` in Twilio's form. A request sent by hand to try the URL, or a changed copy of a real one, has none. | Send a message to the number so that Twilio sends the request. |
| **Refused** with "The signature did not match. Check that the secret saved here is the one the sender shows for this URL." | The secret on the chosen connection is not the **Auth Token** of the account that sent the request, or the URL set in Twilio is not the block's URL exactly. The first can happen when the block chose another service's connection, when the token was copied from another account, or when the token was replaced in Twilio. It also happens when the account signs its webhooks with a shared key. | Copy the **Auth Token** from the Twilio Console again and select **Replace** under **Receiving events** on the connection's page. You do not republish the workflow. Then paste the block's URL into Twilio again. The block cannot verify requests signed with a shared key. |
| **Refused** with "The request carried no signature." | A secret is saved on the chosen connection, and the request did not carry Twilio's signature. A request sent by hand to try the URL has none. | Send a message to the number so that Twilio sends the request. |

A delivery listed as **Refused** is answered with status `401` and starts nothing, and OutWin keeps none of its data. Twilio's documentation says that when a webhook fails or answers with an error, Twilio requests the number's fallback URL if one is set. For every other outcome in the table, Twilio receives status `200` with TwiML and OutWin does not run the request later.
