The process of invoking your Twilio Function code is quite simple. By design, the Twilio Function lightweight environment provides developers with all the flexibility they need. Read on to learn about how Twilio Functions runs your code.
During invocation the following steps occur:
- Environment Bootstrap - The Twilio Function environment is bootstrapped, and any resources your Function code may rely on are quickly initialized.
- Handler Execution - The Twilio Environment will then execute the
exports.handler()method and provide the
- Response Emitted - When your Twilio Function code has completed, you call the
callback()method to emit your response. After executing the
callback()method, your Twilio Function execution will be terminated. This includes any asynchronous processes that may still be executing.
The handler method is the interface between Twilio Functions and your application logic. You can think of the handler method as the entry point to your application. This is somewhat analogous to a
main() function in Java or
__init__ in Python.
Twilio Functions will execute your handler method when it is ready to hand off control of execution to your application logic. If your Function Code does not contain a handler method, Twilio Functions will be unable to execute your logic and will emit an HTTP 500.
||Provides information about the current execution environment|
||Contains the request parameters passed into your Twilio Function|
||Function used to complete execution and emit responses|
Twilio Functions provides the
context object as an interface between the current execution environment and the handler method. The
context object provides access to helper methods as well as your Environment Variables.
context object provides helper methods that preinitialize common utilities and clients that you might find useful when building your application. These helper methods extract all their required configuration from Environment Variables.
||Twilio REST Helper||If you have enabled the inclusion of your account credentials in your Function, this will return an initialized Twilio REST Helper Library. If you have not included account credentials in your Function, calling this method will result in an error. If your code doesn’t catch this error, it will result in an HTTP 500 response.|
Developers are encouraged to use Environment Variables to separate their code from configuration. Using Environment Variables ensures that your code is portable and that simple configuration changes can be made instantly.
For a more in-depth explanation and examples, refer to the Environment Variables documentation.
event object contains the request parameters and headers that are being passed into your Function. Both POST and GET parameters will be collapsed into the same object. For POST requests, you can pass either form encoded parameters or JSON documents; both will be collapsed into the
When you have finished processing your request, you need to invoke the
callback function to emit a response and complete execution. The
callback method will automatically determine the data type of your response and serialize the output appropriately.
You must invoke the
callback method when your Function is done processing. Failure to invoke
callback will cause your Function to continue running up to the 10 second execution time limit. When your Function reaches the execution time limit, it will be terminated, and a 504 Gateway timeout will be returned to the client.
It is important to note that when the
callback function is invoked, it will terminate all execution of your Function. This includes any asynchronous processes you may have kicked off during the execution of your handler.
For this reason, if you are using libraries that are natively asynchronous, you need to nest your call to the
callback function inside the promise handler.
||Error indicating what problem was encountered during execution. This will result in the client receiving a HTTP 500 response with the provided payload.|
||Successful response generated by the Function. Providing this argument will result in the client receiving a HTTP 200 response containing the provided value.|
In some instances, your Function may need greater control over the response it is going to emit. For those instances, you can use the Twilio Response object included in your Function by default.
By using the Twilio Response object, you will be able to specify the status code, headers, and body of your response. You can begin constructing a custom response by creating a new Twilio Response object, like so:
const response = new Twilio.Response();
||Sets the status code in the HTTP response|
||Sets the body of the HTTP response. Takes either an object or string. When setting the body to anything other than text, make sure to set the corresponding Content-Type header with
||Adds a header to the HTTP response. The first argument specifies the header name and the second argument the header value|
||Sets all of the headers for the HTTP response. Takes an object mapping the names of the headers to their respective values|