- Error Handling in Dev Server
You can install the CLI tool via
npm or another package manager. Ideally install it as a dev dependency instead of global:
# Install it as a dev dependencynpm install twilio-run --save-dev# Afterwards you can use by using:node_modules/.bin/twilio-runnpx twilio-run# Or inside your package.json scripts section as "twilio-run"
Check out the commands for in depth usage, but here are some things you will want to know:
Create a new project
To create a new project with the Twilio Serverless Toolkit you can use
create-twilio-function which will scaffold a new project that is ready to be used with
# Create a valid project, for example:npm init twilio-# Navigate into projectcd my-project
You can then use
twilio-run to run a local development server to serve your functions and assets.
npx twilio-run start
assets directory. You can choose other directories by providing a
--assets-folder option to
Twilio Functions and Assets can be public, protected or private. The differences are:
- Public: Any one with the URL can visit the Function or Asset
- Protected: Twilio signs webhook requests, making a Twilio Function protected means that the Function will validate the webhook signature and reject any incoming requests that don't match
- Private: The Function or Asset doesn't a URL, it can only be required within another Function or Asset
twilio-run you can make your Functions or Assets public, protected or private by adding to the function filename. Functions and Assets are public by default. To make a Function or Asset protected or private, add
.private to the filename before the extension. For example:
There are a number of pre-written Function templates that you can add to your project. The templates are available on GitHub and you can also propose your own via pull request.
To list the available templates you can run:
npx twilio-run list-templates
To add a new function into your project from a template you can run:
npx twilio-run new namespace
The command will walk you through choosing the template.
Deploy a project
To deploy a project to the Twilio infrastructure you can run the command:
npx twilio-run deploy
This will deploy your project to the "dev" environment by default. You can then promote the project from "dev" to other environments with the command:
npx twilio-run promote --from=dev --to=stage
The CLI exposes a variety of commands. The best way to find out about the flags and commands available is to run
twilio-run --help or
twilio-run [command] --help
twilio-run start [dir]
Starts a local development server for testing and debugging of your environment. By default only variables in the
.env file will be available via
process.env or through the
context argument inside Twilio Functions.
# Serves all functions in current functions sub directorytwilio-run# Serves all functions in demo/functionstwilio-run demo# Serves functions on port 9000PORT=9000 twilio-run# Serves functions on port 4200twilio-run --port=4200# Starts up the inspector mode for the node processtwilio-run --inspect# Exposes the Twilio functions via ngrok to share themtwilio-run --ngrok
Deploys your project to Twilio. It will read dependencies automatically from your
dependencies field and install them. It will also upload and set the variables that are specified in your
.env file. You can point it against a different
.env file via command-line flags.
# Deploys all functions and assets in the current working directorytwilio-run deploy# Creates an environment with the domain suffix "prod"twilio-run deploy --environment=prod
# List available templatestwilio-run list-templates
twilio-run new [namespace]
Creates a new set of functions and/or assets inside your current project based on a template.
# Create a new function using the blank template# in a subfolder (namespace) demotwilio-run new demo --template=blank
twilio-run list [types]
Lists a set of available resources for different types related to your Account. Available resources that can be listed:
- Environments or Builds (requires to pass a Service)
- Functions, Assets or Variables (requires to pass a Service and Environment)
# Lists all existing services/projects associated with your Twilio Accounttwilio-run list services# Lists all existing functions & assets associated with the `dev` environment of this service/projecttwilio-run ls functions,assets --environment=dev --service-name=demo# Outputs all environments for a specific service with extended output for better parsingtwilio-run ls environments --service-sid=ZSxxxxx --extended-output# Only lists the SIDs and dates of last update for assets, variables and functionstwilio-run ls assets,variables,functions --properties=sid,date_updated
Promotes an existing deployment to a new environment. It can also create a new environment if it doesn't exist.
# Promotes the same build that is on the "dev" environment to the "prod" environmenttwilio-run activate --environment=prod --source-environment=dev# Duplicates an existing build to a new environment called `demo`twilio-run activate --environment=demo --create-environment --build-sid=ZB1234xxxxxxxxxx
Print logs from your Twilio Serverless project
# Gets the latest logs for the current project in the dev environmenttwilio-run logs# Continuously streams the latest logs for the current project in the dev environmenttwilio-run logs --tail# Gets the latest logs for the function sid in the production environmenttwilio-run logs --function-sid ZFXXX --environment production
The module also exposes two functions that you can use outside of the CLI tool to spin up local development.
If you want to interact with the Runtime API instead, check out the
runDevServer(port: number, baseDir: string): Promise<Express.Application>
This allows you to trigger running an express server that will expose all functions and assets. Example:
const runDevServer = ;;
handleToExpressRoute(handler: TwilioHandlerFunction): Express.RequestHandler
You can take the
handler function of a Twilio Function file and expose it in an existing Express server. Example:
const express = ;const bodyParser = ;const handlerToExpressRoute = ;const handler = ;const app = ;app;appall;app;
Error Handling in Dev Server
If your local Twilio Function throws an unhandled error or returns an
Error instance via the
callback method, we will return an HTTP status code of
500 and return the error object as JSON.
By default we will clean up the stack trace for you to remove internal code of the dev server and add it as
at [Twilio Dev Server internals] into the stack trace.
An example would look like this:
Error: What? at format (/Users/dkundel/dev/twilio-run/examples/basic/functions/hello.js:5:9) at exports.handler (/Users/dkundel/dev/twilio-run/examples/basic/functions/hello.js:13:3) at [Twilio Dev Server internals]
If you want to have the full un-modified stack trace instead, set the following environment variable, either in your Twilio Function or via
This will result into a stack trace like this:
Error: What? at format (/Users/dkundel/dev/twilio-run/examples/basic/functions/hello.js:5:9) at exports.handler (/Users/dkundel/dev/twilio-run/examples/basic/functions/hello.js:13:3) at twilioFunctionHandler (/Users/dkundel/dev/twilio-run/dist/runtime/route.js:125:13) at app.all (/Users/dkundel/dev/twilio-run/dist/runtime/server.js:122:82) at Layer.handle [as handle_request] (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/layer.js:95:5) at next (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/route.js:137:13) at next (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/route.js:131:14) at next (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/route.js:131:14) at next (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/route.js:131:14) at next (/Users/dkundel/dev/twilio-run/node_modules/express/lib/router/route.js:131:14)
In general you'll want to use the cleaned-up stack trace since the internals might change throughout time.
This project welcomes contributions from the community. Please see the
CONTRIBUTING.md file for more details.
Code of Conduct
Please be aware that this project has a Code of Conduct. The tldr; is to just be excellent to each other ❤️
Thanks goes to these wonderful people (emoji key):
🐛 💻 👀
This project follows the all-contributors specification. Contributions of any kind welcome!