Introducing Twilio Functions — Public Beta Now Open

May 25, 2017
Written by

tw2_functions_blog
  • Write and run Twilio code without dealing with servers.
  • 10k free invocations every month—$0.0001/invocation after.
  • Now available to everyone in public beta.

Today, we’re excited to announce Twilio Functions, a serverless environment to build and run Twilio applications so you can get to production faster. Developers provide Twilio with Node.js code for handling their communication events, such as an incoming phone call, and Twilio executes this code on their behalf, ensuring a seamless communications experience.

In order to understand why we built Functions, let’s first look at how developers build apps without them.

How to send a Twilio SMS message without using Functions

The first step is to stand up a web server that instructs Twilio on what to do for a communication workflow. There are two reasons that Twilio was designed this way:

  1. Twilio communicates events to your software using webhooks. These events span the gamut from incoming SMS messages to new reservations in TaskRouter. Webhooks mean that you can build Twilio apps using any language or framework that you like, so long as it speaks HTTP.
  2. You must use credentials to authenticate to the REST API and to generate access tokens. The credentials and code need to operate in a secure environment.

Although this architecture works great for using webhooks securely, it does cause some problems. Having to stand up a public web server can create a lot of friction for people who just want to prototype a simple solution and test it with users before committing to building it out. Also, developers often run into internet – and HTTP – related issues (DNS blips, proxy configuration, caching behavior) that make it difficult to troubleshoot their Twilio applications. And finally, many customers face challenges with scaling. We’ve heard numerous stories about customers building awesome applications with Twilio, those applications becoming an overnight success, and their web infrastructure falling over due to the load.

We’ve designed Functions to solve these specific challenges. Using Functions, you can build production-ready Twilio applications that respond to Twilio events, mint Access Tokens, and invoke the REST API – all without needing to stand up or scale a web server.

Getting Started with Functions

Getting started is simple. First, visit the Functions section in the Twilio Console. You’ll see a list of your Functions as well as the ability to create a new one. When you create a new Function, you will be prompted to choose a Template to get started with.

Twilio Functions - choose a template

Templates are pre-written code and configuration that help you quickly build a Twilio application. There are currently several templates built-in to Functions that help you do everything from forwarding an incoming phone call to minting a Sync Access Token. For now, select the “Hello Voice” Template.

Now it’s time to build your Function. Make sure your Function has a descriptive name so you can reference it in other parts of the Twilio Console. For now, you can leave it as Hello Voice. Next, set the path for your Function. Every Twilio account is provisioned a unique domain to use with Functions. The combination of your unique domain and the path gives you a fully qualified URL that can be used to invoke your Function. You can set the path for this function to ‘/hello’. Since you used a Template, the code section has been pre-populated for you:

Twilio Function configuration

Functions are written in Node.js and executed in a v6.10 container. Every time your Function is invoked, three parameters are passed-in.

  1. context: variables passed in from your Function configuration such as credentials and environment variables.
  2. event: information about the specific invocation, such as HTTP parameters, that were passed-in.
  3. callback: the method you call to return from the Function.

Click “Save” and your new Function will get deployed to the Twilio Runtime.

Using a Function to Power an Incoming Phone Call

Once you’ve finished writing your Function, it’s time to put it to work! Go to your Phone Numbers and edit one that has the ability to handle inbound phone calls. Scroll down to the Voice & Fax section.

Twilio Functions - Voice and Fax

In the section where we specify what to do when a call comes in,  select Function from the drop-down to the right. Then choose the “Hello Voice” Function that you just created and click Save at the bottom of the page.

Now comes the moment of truth! Give your Twilio phone number a call and your new Function will say “Hello.”

Pricing

Functions are priced per-invocation. You only pay when your Function is run. All Twilio customers get 10,000 free invocations each month so they can try out Functions and prototype solutions without any friction. Beyond 10,000, each invocation is $0.0001. Volume pricing will be available in the future.

Wrapping It Up

Functions are the newest part of the Twilio Runtime and we’re excited to help developers prototype and build production-grade Twilio application faster than ever. Below are some resources to get help you get started building:

We can’t wait to see what you build!