Edit on GitHub
Report issue
Page history

Respond to SMS messages with Twilio and Cloud Functions

Author(s): @jmdobry ,   Published: 2017-02-17

Contributed by Google employees.

This tutorial demonstrates using Cloud Functions to reply to an SMS message using Twilio. The Cloud Function is implemented in Node.js.

The sample Cloud Function is triggered by a webhook request from Twilio when an SMS message is sent to your Twilio phone number. The webhook validates that the request came from Twilio and then sends a simple reply.


  1. Create a project in the Cloud Console.
  2. Enable billing for your project.
  3. Install the Cloud SDK.

Getting started with Twilio

  1. Create a Twilio account.
  2. In your Twilio console, create a phone number.
  3. Once you have a phone number, click Manage Numbers and then click on your phone number.

  4. Under Messaging:

    1. Set Configure with to Webhooks/TwiML.
    2. Set A Message Comes In to Webhook and enter the following URL:


      Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.

    3. Click Save.

  5. Return to your Twilio Account Settings and take note of the Auth Token for your Live Credentials. You will need it later in this tutorial.

Preparing the Cloud Function

  1. Create a package.json file by running the following:

    npm init


    yarn init
  2. Install the single dependency used by the Cloud Function:

    npm install --save twilio


    yarn add twilio

    This dependency is used by the Cloud Function validate the request and formulate the response.

  3. Create a file named config.json with the following contents:


    Replace [YOUR_TWILIO_AUTH_TOKEN] with your Twilio Auth Token from step 5 of the Getting Started with Twilio section.

Writing the Function code

Create a file named index.js with the following contents:

'use strict';

const twilio = require('twilio');
const config = require('./config.json');

const MessagingResponse = twilio.twiml.MessagingResponse;

const projectId = process.env.GCLOUD_PROJECT;
const region = 'us-central1';

exports.reply = (req, res) => {
  let isValid = true;

  // Only validate that requests came from Twilio when the function has been
  // deployed to production.
  if (process.env.NODE_ENV === 'production') {
    isValid = twilio.validateExpressRequest(req, config.TWILIO_AUTH_TOKEN, {
      url: `https://${region}-${projectId}.cloudfunctions.net/reply`

  // Halt early if the request was not sent from Twilio
  if (!isValid) {
      .send('Twilio Request Validation Failed.')

  // Prepare a response to the SMS message
  const response = new MessagingResponse();

  // Add text to the response
  response.message('Hello from Google Cloud Functions!');

  // Send the response

Notice the named export reply—this is the function that will be executed whenever an SMS message is sent to your Twilio number.

The reply function does the following:

  1. Validates that the request came from Twilio.
  2. Sends a reply to the SMS message.

Deploying and testing the Cloud Function

  1. Run the following to deploy the function:

    gcloud functions deploy reply --trigger-http
  2. Send an SMS message to your Twilio phone number and observe the response you receive from the Cloud Function.

To view the logs for the Cloud Function, run the following:

gcloud functions logs read reply

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.