6 things you didn't know you could do with the Twilio Serverless Toolkit
Tags: twilio, serverless, javascript
Published: 2020-07-15 01:14:09

---
title: 6 things you didn't know you could do with the Twilio Serverless Toolkit
published: true
description: 
tags: twilio, serverless, javascript
cover_image: https://twilio-cms-prod.s3.amazonaws.com/images/lCmnqaYFBoOo0bP0oglM4_ctHm5X1lILHkXGz0tzmw7GX.width-1616.png
canonical_url: https://www.twilio.com/blog/6-things-you-can-do-with-twilio-serverless-toolkit
---

The [Twilio Serverless Toolkit](https://www.twilio.com/docs/labs/serverless-toolkit) is a suite of command line tooling to help you develop and deploy functions and assets to [Twilio Runtime](https://www.twilio.com/runtime). It is under constant development as we add features that make it easier for you to develop your Twilio applications and you never know what you might be missing.

So, here are 6 things you might not know you can do with the Twilio Serverless Toolkit.

## 1. Use the toolkit from npm

If you have the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart) installed you can install the Serverless Toolkit as a plugin with the command `twilio plugins:install @twilio-labs/plugin-serverless`. If you prefer, you can use the toolkit without installing anything more than Node.js. Running the following command will scaffold a new Twilio Functions project for you:

```bash
npm init twilio-function project-name
```

This command will

* install `twilio-run` as a development dependency
* set up an `.env` file for your environment variables
* provide scripts for starting (`npm start`) and deploying (`npm run deploy`) the project
* create some example functions and assets

![Running the command shows progress and then a success message telling you what to do next.](https://twilio-cms-prod.s3.amazonaws.com/images/H-0kFfPxQ1NeHP4SU3C2kYLIW4qFaf6jRoMluJteE4Kxrc.width-500.png)

All of this is at your fingertips with just one npm command.

I'll show how to use both the Twilio CLI with the serverless plugin and npm/npx commands that don't require the Twilio CLI for the rest of the examples in this post.

## 2. Start or add to your project with pre-built functions

Do you prefer not to start completely from scratch if you don't have to? The [Twilio CodeExchange](https://www.twilio.com/code-exchange) lists example apps that can get you going, but did you know that the Serverless Toolkit also has a bunch of example Functions and applications you can use to get building your Twilio app quickly.

Over the years we have collected a number of [template functions](https://github.com/twilio-labs/function-templates), from creating a [conference line](https://github.com/twilio-labs/function-templates/tree/master/conference) or [voicemail](https://github.com/twilio-labs/function-templates/tree/master/voicemail) to a full [browser based voice client](https://github.com/twilio-labs/function-templates/tree/master/voice-client-javascript). Using the Serverless Toolkit you can either start a new project using one of these templates or you can add templates to an existing project.

To start a new project from a template you can run:

```bash
twilio serverless:init project-name --template TEMPLATE_NAME

# or

npm init twilio-function project-name --template TEMPLATE_NAME
```

where the `TEMPLATE_NAME` is any of the templates available in [the template repo](https://github.com/twilio-labs/function-templates). You can also list the available templates by calling `twilio serverless:list-templates` or `npx twilio-run list-templates`.

To add a template function to your existing Twilio Functions project you can run `twilio serverless:new` or `npx twilio-run new` and the command will walk you through choosing a template.

![Running the serverless:new command will list out templates you can choose from to create new functions.](https://twilio-cms-prod.s3.amazonaws.com/images/z4tVacNV1ahG0qYhoal26_h6FvneIuFWhfhtkLgtxeXj13.width-500.png)

If you have built a function that you think would be useful for others you can [propose it to the repo by opening a pull request on GitHub](https://github.com/twilio-labs/function-templates).

## 3. Change the deploy directory

By default a Serverless Toolkit project has a `functions` and an `assets` directory. This may not reflect the application you are building though. If, for example, you are building a front-end application that needs bundling or compiling, like a React or Angular application, your assets may end up in a `dist` or `build` directory.

When you run or deploy your application, you don't want to have to change that directory into an `assets` directory. Instead, the Serverless Toolkit makes it easy to change the directory from which you run and deploy your functions and assets. If you want to run your application where assets are in a `dist` directory and your functions are in a `src` directory you can run:

```bash
twilio serverless:start --functions-folder src --assets-folder dist

# or

npx twilio-run --functions-folder src --assets-folder dist
```

If you want to deploy the application, you can use the same options with the [](https://github.com/twilio-labs/twilio-run/#twilio-run-deploy)[deploy](https://github.com/twilio-labs/twilio-run/#twilio-run-deploy) [command](https://github.com/twilio-labs/twilio-run/#twilio-run-deploy) too.

This feature opens up the possibilities for working in TypeScript

## 4. Build your serverless project in TypeScript

Want to host your project on Twilio Functions but prefer writing in TypeScript? The Serverless Toolkit has got you.

There is a guide on [converting your existing Twilio Functions project to TypeScript](https://www.twilio.com/docs/labs/serverless-toolkit/guides/typescript#converting-an-existing-twilio-serverless-project) but if you are starting fresh, you can set up your project with TypeScript from the beginning. Run:

```bash
npm init twilio-function project-name --typescript
```

Your new project will include the TypeScript compiler as a dependency and scripts to build, run and deploy your TypeScript project.

## 5. Fetch or tail your function logs

The Serverless Toolkit doesn't just cover everything up to deploying your functions, you can also use it to keep tabs on your deployed functions too. Anything you log from within your function using `console.log` or any of the other logging functions (`info`, `error`, `debug`, `warn`, etc) is stored and can be retrieved via the [Logs API](https://www.twilio.com/docs/runtime/functions-assets-api/api/logs).

You can also follow those logs live using the Serverless Toolkit by running:

```bash
twilio serverless:logs --tail

# or

npx twilio-run logs --tail
```

![When you use the --tail option logs will be streamed into your console.](https://twilio-cms-prod.s3.amazonaws.com/original_images/S3FxkSjk6k5TDD6utrvqM-zSEZkGMK4uvQNlQT-CUNOyTndidRUtZFYi1cNRxz5L4K-sTkzgEXicTd)

Just want to see the latest logs? Drop the `--tail` option and you'll just get the last page of logs.

## 6. Test your project with ngrok

On this blog we're always telling you how you can [install and use ngrok to test your webhooks](https://www.twilio.com/blog/2015/09/6-awesome-reasons-to-use-ngrok-when-testing-webhooks.html). With the Serverless Toolkit ngrok is built in for your convenience.

You can run your Serverless Toolkit generated project locally with the command

```bash
twilio serverless:start

# or

npm start

# or

npx twilio-run
```

This makes your application available on localhost at port 3000\. If you pass the `--ngrok` flag your application will start with an automatically configured ngrok tunnel.

```bash
twilio serverless:start --ngrok=""

# or

npm start -- --ngrok

# or

npx twilio-run --ngrok
```

Your ngrok URL will be displayed on the terminal and you can use it to test webhooks for things like [incoming SMS messages](https://www.twilio.com/docs/usage/webhooks/sms-webhooks) or [voice calls](https://www.twilio.com/docs/usage/webhooks/voice-webhooks).

## Developing and deploying Twilio Serverless projects keeps getting easier

We're building the Serverless Toolkit to make your life easier developing Twilio applications. Hopefully you have learned something new that will help your workflow from this post. For more on what you can do with the Serverless Toolkit check out this post on [debugging with the Twilio Serverless Toolkit](https://www.twilio.com/blog/locally-developing-and-debugging-twilio-functions), this post on [developing Flex plugins with the Serverless Toolkit](https://www.twilio.com/blog/flex-plugins-vs-code-functions-cli) and [the documentation](https://www.twilio.com/docs/labs/serverless-toolkit).

If you're interested in the development of the Serverless Toolkit you can catch me working on bugs or features [live on Twitch](https://twitch.tv/phil_nash). Join me to ask questions, request features or just see the code coming together live.

If you have a feature you'd like to see in the Serverless Toolkit,  let us know by [raising an issue in the project in GitHub](https://github.com/twilio-labs/serverless-toolkit/issues).