The best way to understand how Twilio Functions work is to start by understanding the HTTP request flow and how Twilio ultimate executes your code. Twilio Functions is designed to handle as much of the infrastructure work of building a web app as possible. For Twilio Functions this work begins at the front door handling the HTTP request for your Function and continues through the pipeline up to your code.
- Every Function Invocation begins with an HTTP Client issuing a request to your Twilio Function. For most Functions, the HTTP client is the Twilio Voice or Messaging API responding to an incoming phone call or text message
- Twilio Function Gateway receives this request and attempts to validate it. If your Twilio Function has Signature Validation enabled the Function Gateway will attempt to validate the signature. If the request or Twilio Signature are invalid the request is rejected with the appropriate HTTP 400 response.
- After Twilio Function Gateway has accepted the HTTP request, it will normalize the request into a payload for Your Twilio Function. The normalized request is provided to your Twilio Function as two arguments: context and event. Once the payload has been constructed, it is used to invoke your Twilio Function.
- At invocation your Twilio Function begins executing the code you have provided. When your Twilio Function completes executing, it can emit a response using the callback method provided. The emitted response will be transmitted to the Twilio Functions Gateway.
- Upon receiving the result from your Twilio Function, the Twilio Function Gateway will construct an HTTP Response and return it to the HTTP Client.
Currently, Twilio Functions only supports HTTP requests. Twilio Functions will respond to three HTTP verbs: GET, POST and OPTIONS. PUT and DELETE are currently not supported.
For POST requests, Twilio Functions natively supports application/json and application/x-www-form-urlencoded. This means that JSON bodies and form parameters will be normalized into the event parameter.
There are several important limitations to Twilio Functions that you should consider when designing your application:
- Execution Time: Twilio Functions execute for at most 5 seconds. Any Twilio Function that exceeds the 5 second limit will be terminated, and the client will receive an HTTP 504 Gateway Time-out response.
- Path Parameter Support: Twilio Functions does not provide support for path parameters. This means that you can not use the URL to pass information into your application. It is recommended that you use either a GET or POST parameter.
- Maximum Response Size: Twilio Functions have a constrained response size of 4 MB.