Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Developing with the Serverless Toolkit


(information)

Info

This is a Twilio Labs project

This means this project is 100% open-source. You can find its source code in the Twilio Labs GitHub organization(link takes you to an external page).

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.

Learn more about Twilio Labs.

There are two ways you can use the toolkit. 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-in start 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.

(information)

Info

Please Note: The Toolkit is only emulating the environment that your Functions will be run in and as a result you might find some inconsistencies. It's always a good idea to additionally test your deployed Functions before putting them into a Twilio app. If you do find some inconsistencies, please file an issue in our GitHub repository(link takes you to an external page).


Starting a local development server

starting-a-local-development-server page anchor

If you are in a directory that follows the folder structure of a Serverless Toolkit project, you'll be able to start your local development server with the basic command without any configuration.

Run a Serverless project locally

run-a-serverless-project-locally page anchor

_10
# Start the local development environment with live reloading of Functions
_10
twilio serverless: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 the context 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.


Debugging your Functions

debugging-your-functions page anchor

The start 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 example Visual Studio Code(link takes you to an external page) or the Chrome Developer Tools(link takes you to an external page).

The Toolkit supports both the --inspect flag as well as the --inspect-brk flag of Node.js. You can read more(link takes you to an external page) about their possible values in the Node.js documentation.

Start local development server in debug mode

start-local-development-server-in-debug-mode page anchor

_10
# Start a local development server with the Node.js debugger enabled
_10
twilio serverless: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 with ngrok(link takes you to an external page) out of the box. You can use the --ngrokflag to enable it and if you are a paid customer of ngrok, you can set the value to a custom domain. For example: --ngrok=twilio-demo

Start local development server with ngrok

start-local-development-server-with-ngrok page anchor

_10
# Expose the local development server with a random ngrok URL
_10
twilio serverless: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.


Rate this Page: