Cloud Code Functions

Using cloud functions and Twilio API to send Text Message

Introduction

This guide explains how you can use the Twilio REST API to send SMS. After completing this step-by-step tutorial, you can use your cloud code function to send SMS to your device.

Prerequisites

To complete this tutorial, you will need:

Let’s get started!

Below are some steps you need to follow when writing a function to send SMS to a User and phone number.

To learn how to create or access an Account in Twilio, check the links given below:

Create a new account - Log in to your account

Step 1 - Activate your Phone Number

After logging in or creating a new account, you will be redirected to your Project. There, on the left, you need to click on the #Phone Numbers. Next, tap on the last link ‘Getting Started’, and then click on the button ‘Get your first Twilio phone number’. Same as shown below:

Generating a new Phone Number

After that you will receive your first Phone Number for your Twilio Account. If you can’t find your phone number, go to #Phone Numbers and Manage Numbers.

Phone Numbers

Step 2 - Get Account SID and Auth Token

To find your Account SID and Auth Token, log in to your Account, go to your Dashboard and click on Settings. All the important information about your Project will be available in that section; as shown in the image below:

Account Settings

Now, you can Copy your SID and Authentication Token for the Cloud Code.

Step 3 - Install Module from Twilio

After configuring the environment for the Command Line Interface in your computer, create a file called package.json, and inside this file, you need to install the Twilio module, like:

1
2
3
4
5
{
  "dependencies": {
    "twilio": "*"
  }
}

Step 4 - Implement Cloud Code

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Parse.Cloud.define("SendSMS", async(request) => {

	// Requiring the values to send
	let
		getMessage = request.params.message,
		getPhoneTo = '+Target test Phone number',
		getPhoneFrom = "+Your first Phone number",
		accountSid = 'AccountSID',
		authToken  = 'AuthToken';

	//require the Twilio module and create a REST client
	let client = require('twilio')(accountSid, authToken);

	return await client.messages
		.create({
		body: getMessage, // Any number Twilio can deliver to
		from: getPhoneFrom, // A number you bought from Twilio and can use for outbound communication
		to: getPhoneTo // body of the SMS message
	});
});

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Parse.Cloud.define("SendSMS",function(request,response){

	// Requiring the values to send
	var
		getMessage = request.params.message,
		getPhoneTo = '+Target test Phone number',
		getPhoneFrom = "+Your first Phone number",
		accountSid = 'AccountSID',
		authToken  = 'AuthToken';


	//require the Twilio module and create a REST client
	var client = require('twilio')(accountSid, authToken);

	client.messages
	.create({
		body: getMessage, // Any number Twilio can deliver to
		from: getPhoneFrom, // A number you bought from Twilio and can use for outbound communication
		to: getPhoneTo // body of the SMS message
	})
	.then(function(results) {
		response.success(results.sid);
	})
	.catch(function(error) {
		response.error(error);
	})
});

Step 5 - Test the function “sendSMS”

You can also test the function in client SDKs, but for now, we will use the REST API command to send it:

curl -X POST \
  	-H "X-Parse-Application-Id: APP_ID" \
  	-H "X-Parse-REST-API-Key: REST_KEY" \
  	-H "Content-Type: application/json" \
  	-d '{ "message": "Now, I can send SMS from Cloud Code using Twilio", "phone": "+Target test Phone number" }' \
https://parseapi.back4app.com/functions/SendSMS

And the result will be something like as this:

Send SMS using Twilio

Step 6 - It’s done!

With the guide described above, you’ll be able to use Twilio with a Cloud Code Function in Back4App and send SMS to your customers!

In case you need any help or a function/link doesn’t work, please contact our team via chat!