# Start a workflow from Twilio events

> Run a workflow when Twilio reports that a message was delivered or failed, or that a call ended, with a Webhook block set to Twilio.

To run a workflow when Twilio reports that a message was sent, delivered, or failed, or that a call rang or ended, add a **Webhook** block whose **Source** is **Twilio** and give its URL to Twilio as a status callback. Twilio posts one status per request. Each status starts its own run.

The block receives status callbacks only. Do not set its URL as the address a Twilio phone number calls for an incoming message or call. Twilio expects instructions (TwiML) in the reply to those requests, and this source does not send them. For an incoming message or call, add a separate Webhook block whose **Source** is **Twilio incoming messages and calls**, which answers Twilio with TwiML. See [Start a workflow from Twilio incoming messages and calls](https://outwin.ai/docs/build/connect-an-api/start-a-workflow-from-twilio-incoming-messages-and-calls/). Both sources use the same Auth Token, so one connection serves both blocks.

You need a Twilio account whose Auth Token you can see, 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 and in the requests you send to Twilio's API.

## Add the Webhook block and generate its URL

1. In a draft workflow, add a **Webhook** block. The block palette also lists **Twilio webhook** in the **Start** group, which adds the block with its source already set.
2. Under **Source**, choose **Twilio**. 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 Twilio as the status callback

Twilio takes a status callback URL where a message or a call is created. Use the URL you copied in one of these places:

- For one message or call, set it as the `StatusCallback` parameter of the request to Twilio's API that sends the message or starts the call.
- For every message a Messaging Service sends, set it as that service's delivery status callback.

Leave the callback's method as `POST`, which is what Twilio uses unless told otherwise. The block does not accept a status callback sent with `GET`.

Twilio has no event names. The status is the event type. The Webhook block offers these Twilio statuses: `queued`, `sent`, `delivered`, `undelivered`, `failed`, `read`, `initiated`, `ringing`, `in-progress`, `completed`, `busy`, `no-answer`, and `canceled`.

## Save the Auth Token on a connection

Twilio signs each request with the account's **Auth Token**. That token is the same for every callback 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.

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. If your workflows also call the Twilio API, enter its base URL and credential instead, or add them later on the connection's page. This guide does not need them.
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**.

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.

## Choose which events start a run

Under **Events that start a run**, tick the statuses that start a run. With none ticked, every callback Twilio sends starts a run. To start runs for a status that is not listed, type it into the box under the list and select **Add**.

Tick at least the statuses your workflow acts on. 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 callback can send it again with its fields divided differently, and it still passes the check. Such a request arrives with no status. With any status ticked, it is listed as **Filtered out** with "The event has no type, and this trigger runs only for chosen types." and starts no run.

A callback whose status is not ticked is also listed as **Filtered out**. To narrow further by a field of the callback, add a row under **Only when**. [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 through Twilio with `StatusCallback` set to the block's URL.
2. In OutWin, look under **Recent deliveries** in the block's settings.

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

With a secret saved on the chosen connection, a callback 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 callback 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 event in the next block

Each run receives one Twilio callback. 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's callback has fields such as `MessageSid`, `MessageStatus`, `AccountSid`, and `From`. A call's callback has `CallSid` and `CallStatus`. Other fields depend on the channel and the status. |
| `{{input.body.MessageSid}}` | One field of the callback. Read any other field the same way. |
| `{{input.event.type}}` | The status, such as `delivered`. It is the callback's `MessageStatus` field, or `CallStatus` for a call. |
| `{{input.event.id}}` | An id OutWin builds from the callback's `MessageSid`, `CallSid`, `MessageStatus`, `CallStatus`, and `SequenceNumber` fields, because Twilio sends no id for a callback. |
| `{{input.event.time}}` | The callback's `Timestamp` field as an ISO 8601 date and time. Twilio sends that field with a call's callbacks. A callback without it has no time here. |
| `{{input.delivery.verified}}` | `true` only when the signature was checked and passed. |

A block further along reads the same values through the Webhook block's name, as in `{{steps["Twilio"].body.MessageSid}}` for a block named `Twilio`. [Data and context](https://outwin.ai/docs/reference/data-and-context/) describes what each block passes on.

## Publish and check the runs

[Publish the workflow](https://outwin.ai/docs/run/publish-and-start/). From then on, each callback that passes the filter starts a run. On a published workflow the block's settings are read-only, including 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 callback started a run. |
| **Waiting** | 10 runs of the workflow are already active. The callback starts a run when one of them ends. A callback that waits longer than an hour is listed as **Dropped**. |
| **Duplicate** | A callback for the same message or call with the same status already ran in the last 72 hours. The line reads "This event was already received and run, so it did not run again." |

Runs start in the order callbacks arrive. That is not always the order things happened, so do not build a workflow that relies on `sent` arriving before `delivered`.

Receiving a callback 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 callback.

| What you see | Cause | Fix |
| --- | --- | --- |
| "Nothing has arrived yet." | No request the block accepts has reached the URL. | Check that `StatusCallback` on the message or call is the block's current URL and that Twilio sends the callback with `POST`. **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. | Publish the workflow. Callbacks that arrived before then are not run later. |
| **Dropped** with "The workflow is paused, so nothing ran." | The workflow is paused. | Resume the workflow. Callbacks that arrived while it was paused are not run later. |
| **Filtered out** with `Events of type "queued" are not chosen on this trigger.` | The callback's status is not ticked under **Events that start a run**. | Tick the status. The settings are read-only on a published workflow. |
| **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 callback. This 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. The block cannot verify callbacks 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 through Twilio so that Twilio sends the callback. |

A delivery listed as **Refused** is answered with status `401` and starts nothing. OutWin keeps none of its data. For every other outcome in the table, Twilio receives a success response and OutWin does not run the event later.
