Run Function
Description:
Run Function allows you to execute Twilio Functions (lightweight, serverless pieces of code that run in the Twilio cloud) within your Flow. Use this Widget to write business logic that works alongside Studio Widgets to fully realize your application.
When you invoke a Function, you have two possible options for using variables: (1) you can pass Flow variables as parameters into a Function (e.g. flow.data.foo
), and (2) you may set Flow variables with data returned from the Function (TwiML or JSON can be returned).
Note: For unique use cases, the Run Function widget can return custom TwiML to enhance your Studio Flow.
Required Configuration
The only required configuration for a Function Widget is the URL of the Function you would like to call. You may select it from a dropdown or search for the Function by name.
Functions are organized by the service they were created in. For Functions created via the Console UI, select the "Default" service. For Functions created via the Serverless API, select the service and environment where the Function was deployed.
Name |
Description |
Example |
Default |
Function URL |
The Function you’d like to invoke. |
Hello SMS |
None |
Optional Configuration
There are several optional configurations for the Function Widget:
Name |
Description |
Default |
Function Parameters |
Zero or more extra parameters that will be passed to the Function expressed as key/value pairs. String literals and variables are supported. |
None |
Transitions:
This Widget has two events that trigger transitions.
Name |
Description |
Success |
A successful return (HTTP 20X) |
Fail |
The Function does not successfully return (or has an error) |
Response:
The HTTP response from the Function must return a 2xx status code within 10 seconds, and the response body must not exceed 64kB.
Response | Recommendation | Notes |
Status Code | 200 or 204 | 3xx redirection is supported. 4xx or 5xx status code will transition to "failed" in the widget. |
Content Type | application/json | Content-Type header is not required if Status Code is 204 No Content. Other content types are supported, such as plain text or XML. But only application/json objects (e.g. {"foo":"bar"} ) will be automatically parsed into Studio variables. |
Body | valid JSON | Body content must match the Content-Type header. |
Response Time | 10 seconds or less | Since Twilio Functions support up to a 10 second execution limit, Studio will timeout the request at 10 seconds and transition to "failed" in the widget. |
Response Size | Maximum 64kb | Studio can only process responses up to 64kB. |
Variables:
Twiml: If your function returns Twiml, the twiml will be returned to the calling client.
Json: If your function returns valid Json, you should be able to access it via widgets.MY_WIDGET_NAME.parsed
For example, if you return {"message": "Hi", "person": {"name": "Bob", "age": 40}}
, you can reference that in subsequent widgets as:
widgets.MY_WIDGET_NAME.parsed.message
widgets.MY_WIDGET_NAME.parsed.person.name
widgets.MY_WIDGET_NAME.parsed.person.age
For all return types, you will have the following variables:
widgets.MY_WIDGET_NAME.body
widgets.MY_WIDGET_NAME.status_code
widgets.MY_WIDGET_NAME.content_type
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.