Skip to contentSkip to navigationSkip to topbar
On this page

Using Twilio Serverless with TypeScript


The Twilio Serverless runtime currently does not support running TypeScript by itself but you can use the TypeScript compiler(link takes you to an external page) to compile your Functions ahead of time.

We published TypeScript definitions for our Serverless Runtime at @twilio-labs/serverless-runtime-types(link takes you to an external page).


Creating a new TypeScript Twilio Serverless project

creating-a-new-typescript-twilio-serverless-project page anchor

You can create a new Twilio Serverless project via npm init or using the serverless plugin(link takes you to an external page) for the Twilio CLI. Either way you can opt to create your new project using TypeScript.

npm init

npm-init page anchor

To create a new TypeScript based Twilio Serverless project with npm(link takes you to an external page) you can run npm init twilio-function with the --typescript option.

New TypeScript Twilio Serverless project with npm

new-typescript-twilio-serverless-project-with-npm page anchor
npm init twilio-function project-name --typescript

You can also pass the --typescript option to the Twilio CLI serverless plugin's init command

New TypeScript Twilio Serverless project from Twilio CLI

new-typescript-twilio-serverless-project-from-twilio-cli page anchor
twilio serverless:init project-name --typescript

Both commands will generate a new project with a srcdirectory that contains your TypeScript source files. When you run npm start or npm run deploy the project will automatically be compiled into the dist directory and run or deployed from there.


Converting an existing Twilio Serverless project

converting-an-existing-twilio-serverless-project page anchor

Start by installing the TypeScript compiler for your project using npm(link takes you to an external page) or another Node.js package manager:

Install TypeScript compiler

install-typescript-compiler page anchor
1
# Using npm:
2
npm install --save-dev typescript
3
4
# Using yarn
5
yarn add --dev typescript

Afterwards create your TypeScript configuration(link takes you to an external page). You can do this by manually creating a tsconfig.json in your project or by using the TypeScript compilers --init flag.

Creating a TypeScript configuration

creating-a-typescript-configuration page anchor
1
# Using npm's npx command
2
npx tsc --init
3
4
# Using yarn
5
yarn tsc
6
7
# Without either
8
node_modules/.bin/tsc --init

Your resulting tsconfig.json should look something like this if you ignore the comments:

1
{
2
"compilerOptions": {
3
"target": "es5",
4
"module": "commonjs",
5
"strict": true,
6
"esModuleInterop": true
7
}
8
}
9

Install Twilio Serverless Runtime definitions

install-twilio-serverless-runtime-definitions page anchor

We need to be able to tell TypeScript about the different types related to the Serverless Runtime. These are called TypeScript definitions and the ones for Serverless Runtime are all bundled in the @twilio-labs/serverless-runtime-types module on npm. You'll need to install it as a dependency for your project.

Install Serverless Runtime TypeScript definitions

install-serverless-runtime-typescript-definitions page anchor
1
# Using npm
2
npm install @twilio-labs/serverless-runtime-types
3
4
# Using yarn
5
yarn add @twilio-labs/serverless-runtime-types

Convert your existing Functions

convert-your-existing-functions page anchor

If you want to convert your existing Functions from JavaScript to TypeScript you'll need to:

  • Rename your file to end with .ts instead of .js
  • Change from require() statements to import
  • Add import '@twilio-labs/serverless-runtime-types'; to the top of your file
  • Import additional types necessary from @twilio-labs/serverless-runtime-types
  • Use export const handler instead of exports.handler

This is how an example TypeScript version of a Twilio Function would look like.

Example Twilio Serverless Function in TypeScript

example-twilio-serverless-function-in-typescript page anchor
1
// Imports global types
2
import '@twilio-labs/serverless-runtime-types';
3
// Fetches specific types
4
import {
5
Context,
6
ServerlessCallback,
7
ServerlessFunctionSignature,
8
} from '@twilio-labs/serverless-runtime-types/types';
9
10
export const handler: ServerlessFunctionSignature = function(
11
context: Context,
12
event: {},
13
callback: ServerlessCallback
14
) {
15
const twiml = new Twilio.twiml.VoiceResponse();
16
twiml.say('Hello World!');
17
callback(null, twiml);
18
};
19

Since Twilio Serverless and the Toolkit don't support TypeScript out of the box yet, we need to run the compiler before the local development or deployment.

Run the TypeScript compiler

run-the-typescript-compiler page anchor
1
# Use npm
2
npx tsc
3
4
# Use yarn
5
yarn tsc
6
7
# Run the compiler directly
8
./node_modules/.bin/tsc

Afterwards you can deploy the project or locally run your project.

Right now we are compiling the TypeScript files in a way that results for the JavaScript output files to live side-by-side with the TypeScript files. This is great because that means the Toolkit commands work without any additional arguments.

To move the output somewhere else, set the outDir option of the compilerOptions inside the tsconfig.json file. Afterwards you'll have to call the Toolkit's start and deploy commands with the --functions-folder flag to point again the functions/ directory inside your output directory.


Now that you know how to combine Twilio Serverless and TypeScript, why not check out some other resources to see what you can do with the Twilio Serverless Toolkit?

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.