Twilio Functions Now Supports npm

September 19, 2017
Written by

functions_npm_support
  • Import third-party libraries into your functions using npm package manager.
  • Leverage free, reusable code from the community.
  • Available today in Twilio Functions.

Today, we’re excited to announce npm support for Twilio Functions. npm is a package manager for JavaScript that makes it easy to share and reuse code. With npm support in Twilio Functions, you can now leverage the ecosystem of packages and modules provided by the Node.js community.

npm support is the first addition in what we’re calling Twilio Functions Packages—a service of Twilio Functions that automatically retrieves, installs, and deploys the third-party packages you require. With Twilio Functions Packages, you never have to upload a package or library to Twilio. All of the logistics of package management is taken care of behind the scenes, so you can stay focused on putting them to work.

How to Install an npm Module

You can get started installing a npm module with a few simple steps:

  1. Start by heading over to the Configure page for Functions. Under the ‘Dependencies’ header you will find all of the currently installed NPM modules.
  2. Click the plus (“+”) button in the top-left hand corner of the table to add a new npm module. This will add a new row to the Dependencies table.
  3. Enter the name and version of the npm module you want to include in the new row.
  4. When you’re done, click ‘Save’. This will trigger a deployment of your functions.

Configuring Twilio Functions with NPM modules

That’s it—you’re done!

Once your deployment completes all of your functions will be able to access the new npm module. To start using this new npm module just use therequire() method to include it in your function.

To minimize maintenance and support headaches, every change to the npm list will be deployed instantly, ensuring the function environment stays consistent and up-to-date.

Using npm in Functions

Developers can start using npm modules inside their functions by including them with the require() method. Below is example of a function, including the SendGrid npm package to send an email.

exports.handler = function(context, event, callback) {
    
    // Start by including the npm module with the require() method
    const sendgrid = require('sendgrid')(context.SENDGRID_API_KEY);
    
    // Then just start using the method to build your solution
    const mail = new sendgrid.helper.Mail(context.From, context.Subject, context.To, context.Body); 
    const request = sendgrid.emptyRequest({
      method: 'POST',
      path: '/v3/mail/send',
      body: mail.toJSON()
    });
    
    callback(null);
};

See npm+Functions in action in the demo vid below:

Wrapping It Up

npm support is the latest in a long list of upcoming releases for Twilio Functions. We’re excited to see how developers use this new capability to prototype and build production-grade applications on top of Twilio. Below are some resources to get help you get started building:

We can’t wait to see what you build with Twilio Functions and npm!