twilio-run
CLI tool to locally develop and deploy to Twilio Functions
About
This project is part of the Serverless Toolkit. For a more extended documentation, check out the Twilio Docs.
Installation
You can install the CLI tool via npm
or another package manager. Ideally install it as a dev dependency instead of global:
npm install twilio-run --save-dev
node_modules/.bin/twilio-run
npx twilio-run
Usage
npx create-twilio-function my-project
cd my-project
twilio-run start
twilio-run deploy
Commands
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]
- Aliases:
twilio-run dev
, twilio-run
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.
Examples
twilio-run
twilio-run demo
PORT=9000 twilio-run
twilio-run --port=4200
twilio-run --inspect
twilio-run --ngrok
twilio-run deploy
Deploys your project to Twilio. It will read dependencies automatically from your package.json
's 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.
Examples
twilio-run deploy
twilio-run deploy --environment=prod
twilio-run list-templates
Lists the templates available to that you can use to generate new functions and/or assets inside your current project with the twilio-run new
command below.
Examples
twilio-run list-templates
twilio-run new [namespace]
Creates a new set of functions and/or assets inside your current project based on a template.
Examples
twilio-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:
- Services
- Environments or Builds (requires to pass a Service)
- Functions, Assets or Variables (requires to pass a Service and Environment)
Examples
twilio-run list services
twilio-run ls functions,assets --environment=dev --service-name=demo
twilio-run ls environments --service-sid=ZSxxxxx --extended-output
twilio-run ls assets,variables,functions --properties=sid,date_updated
twilio-run activate
- Aliases:
twilio-run promote
Promotes an existing deployment to a new environment. It can also create a new environment if it doesn't exist.
Examples
twilio-run activate --environment=prod --source-environment=dev
twilio-run activate --environment=demo --create-environment --build-sid=ZB1234xxxxxxxxxx
API
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 @twilio-labs/serverless-api
package.
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 } = require('twilio-run');
runDevServer(9000)
.then(app => {
console.log(`Server is running on port ${app.get('port')})`);
})
.catch(err => {
console.error('Something failed');
});
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 = require('express');
const bodyParser = require('body-parser');
const { handlerToExpressRoute } = require('twilio-run');
const { handler } = require('./path/to/function.js');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.all(handlerToExpressRoute(handler));
app.listen(3000, () => console.log('Server running on port 3000'));
Contributing
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 ❤️
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT