Developing with the Serverless Toolkit
This is a Twilio Labs project
This means this project is 100% open-source. You can find its source code in theTwilio Labs GitHub organization.
We currently don't support the projects through our official support channels. But you are welcome to reach out to us on GitHub for any questions, issues or suggestions or to contribute to this project.
There are two ways you can use thetoolkit. If you are already using the Twilio CLI, you can install it via a plugin. Alternatively, you can use the toolkit as a standalone using twilio-run via npm or another Node.js package manager.
Throughout the docs we'll reference the Twilio CLI commands.
One of the primary goals of the Serverless Toolkit is to enable you to use your already existing development tools while developing Twilio Functions. With the built-instart command you can run a local development server that mimics the Twilio Functions and Assets environment and enables you to locally develop, test and debug your applications before deploying them to Twilio.
Info
The Toolkit is only emulating the environment that your Functions will be runin and as a result you might find some inconsistencies. It's always a goodidea to additionally test your deployed Functions before putting them into aTwilio app. If you do find some inconsistencies, pleasefile an issue in ourGitHub repository.
If you are in a directory that follows thefolder structure of a Serverless Toolkit project, you'll be able to start your local development server with the basic command without any configuration.
1# Start the local development environment with live reloading of Functions2twilioserverless:start
This command will kick off the local development server and make any Functions and Assets that you have in your directory available for you.
By default the server will run in "live mode" meaning that any modifications to an existing Function will automatically reflect when do another request to the endpoint without you having to restart the server.
However, you will have to restart the server if you:
- Rename a file
- Create a new file
- Change your .env file
To have access to a configuration value via an environment variable or thecontext
argument of your Twilio Function, you'll have to add it to the.env
file in your project. We do not expose any system environment variables by default to make the experience as consistent as possible.
You can change the file that contains the environment variables using the--env
flag and specifying a path to another.env
file.
Alternative, use the--load-local-env
flag to make your local environment variables available. These will not be uploaded during deployment though.
Thestart command provides the ability to start the Node.js debugger via a command-line flag. This allows you to use any debugger tool that supports Node.js to attach to it and use the debugging tools. For exampleVisual Studio Code or theChrome Developer Tools.
The Toolkit supports both the--inspect
flag as well as the--inspect-brk
flag of Node.js. You canread more about their possible values in the Node.js documentation.
1# Start a local development server with the Node.js debugger enabled2twilioserverless:start--inspect=""
While developing an application especially for Twilio, you might want to make your local development server available for others to access. For this, the Serverless Toolkit ships withngrok out of the box. You can use the--ngrok
flag to enable it and if you are a paid customer ofngrok, you can set the value to a custom domain. For example:--ngrok=twilio-demo
1# Expose the local development server with a random ngrok URL2twilioserverless:start--ngrok=""
Once the server is started up and the ngrok connection is established, you'll be able to see the URLs printed in your terminal reflecting the new ngrok URLs as well as a link to the ngrok inspector.
Now that you know how to locally develop your Functions with the Serverless Toolkit, why not learn how you can do more with it.