APIs

How to Use the Twilio APIs on the RapidAPI Marketplace

Today we are excited to welcome our new partner Twilio to the RapidAPI Marketplace! Twilio is a cloud communications platform that allows developers to programmatically send and receive text messages and perform other communication functions using its web service APIs.

With Twilio now available on the RapidAPI Marketplace, it is easier than ever to integrate messaging in your web and mobile applications. The Twilio APIs now available to the RapidAPI developer community include Verify Phone Number, Lookup and SMS. In this post we will highlight the purpose of these APIs and go through the steps of subscribing to, testing and connecting to each API via the RapidAPI Marketplace.

Twilio Lookup API: What is It?

The Twilio Lookup Phone Number API helps verify numbers to reduce undeliverable messages, identify local-friendly number formats, and resolve caller names. Twilio Phone Number Lookup allows you to programmatically get information about phone numbers so you can be as effective as possible.

Lookup on the RapidAPI Marketplace currently supports the following types of data:

Use Cases for the Twilio Lookup API

This type of data can be used in a variety of ways.

Knowing the region-specific number formatting for a phone number is helpful since this format is often easier to display to end-users.

For example, if you are from the United States, which number format would you prefer?

+14153902337 or (415) 390-2337

These different formats refer to the same phone number. However, the second version is much easier to read for those who are familiar with the United States phone number format.

Number validation could be useful in a variety of situations. It provides a way to minimize fraud and ensure you have a way to contact users in the future.

The API can also be used to find Carrier Information or the Caller Name associated with the phone number.

How to Use the Twilio Lookup API

The easiest way to get started is to navigate to the Twilio Lookup API listing. From there, you can take a look at the pricing page and get subscribed to the API. You will need to subscribe to an API pricing plan before you can make any requests to the API. All of the Twilio APIs on the RapidAPI Marketplace are a pay per use. This means you do not have to pay a monthly subscription and only pay for the number of calls you make throughout the month.

For the Lookup API, getting a Caller Name is $0.01 per use and getting a Carrier Lookup is $0.005 per use. If you are using the basic “Lookup a Phone Number” endpoint, that includes number formatting and country information, it is free to use. Once you subscribe to the API plan, go to the endpoints page. There, you will find all of the code snippets you will need to integrate the API into your application.

Example: Lookup a Phone Number

For “Lookup a Phone Number” you need to provide a phone number in E.164 format. This format is [+][country code][number including area code].

Let’s use a California phone number as an example. This number would include +1 (since 1 is the US country code) followed by the rest of the phone number including the 415 area code. It will look something like this: +14153902337

For this tutorial, we will be using NodeJS, but you can use any of the SDKs we support.

To start using the API, copy the code snippet from RapidAPI into an index.js file and install the unirest npm module.

var unirest = require("unirest");

var req = unirest("GET", "https://twilio-lookup.p.rapidapi.com/PhoneNumbers/caller-name");

req.headers({
  "x-rapidapi-host": "twilio-lookup.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}"
});


req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Running the program will return validation and the region-specific number formatting. A successfully completed request means the number is valid. Here is the response that was returned:

"callerName":NULL
"countryCode":"US"
"phoneNumber":"+14153902337"
"nationalFormat":"(415) 390-2337"
"carrier":NULL
"addOns":NULL
"url":"https://lookups.twilio.com/v1/PhoneNumbers/+14153902337"
}

Notice in our response, “callerName” and “carrier” are null, but you can use the same process for the Lookup Phone Number Include Carrier Name or Lookup a Phone Number Include Caller Name endpoints. As a reminder, requesting the Carrier Name or Caller Name will cost $0.005 per call and $0.01 per call respectively.

Twilio Verify Phone Number API: What is it?

The Twilio Verify Phone Number API helps you fight fraud before it starts by validating users with SMS and voice. The Verify Phone Number API is used to ensure that a user is the owner of the phone number they provide.

How to Use the Twilio Verify Phone Number API

Once again, the easiest way to get started is to navigate to the Twilio Verify Phone Number API listing. You will need to subscribe to an API pricing plan before you can make any requests to the API. All of the Twilio APIs on the RapidAPI Marketplace are pay per use. This means you do not have to pay a monthly subscription and only pay for the number of calls you make throughout the month.

For the Verify Phone Number API, verifying a phone number will cost $0.05. Once you are subscribed to the API plan, go to the endpoints page to find all of the endpoints and code snippets you will need to integrate the API into your application.

List of Verify Phone Number API Endpoints

The current endpoints for the Verify API are:

Example: Verify a Phone Number

For this tutorial, we will be using NodeJS, but you can use any of the SDKs we support.

In order to verify a phone number, we need to Create a Verification Service using the Services endpoint. The only required parameter is “friendlyName” which is a descriptive string to describe the verification service. The friendly name is utilized in the messages template. (Ex: “Your {{friendlyName}} verification code is: {{code}}”). There are several additional optional parameters that you can use to further customize the verification service, but we will keep it simple for now.

For this example, we set friendlyName to “RapidAPI” and codeLength to 6.

var unirest = require("unirest");

var req = unirest("POST", "https://twilio-verify-phone-number.p.rapidapi.com/Services");

req.query({
  "codeLength": "6",
  "friendlyName": "RapidAPI"
});

req.headers({
  "x-rapidapi-host": "twilio-verify-phone-number.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}",
  "content-type": "application/x-www-form-urlencoded"
});

req.form({});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

To start using the API, copy the code snippet from RapidAPI into an index.js file and install the unirest npm module.

Running the program will return data including an sid. This sid is what you will use for the serviceSid parameter moving forward.

Now that we have the serviceSid, we can Start a New Verification using the Verifications endpoint. The required parameters for this endpoint are:

  • to: the phone number you want to verify.
  • channel: set to sms or call. For this example we used sms.
  • serviceSid: the number we just generated in the previous step.

With these assigned, we can start the verification.

var unirest = require("unirest");

var req = unirest("POST", "https://twilio-verify-phone-number.p.rapidapi.com/Services/{{insert-sid-here}}/Verifications");

req.query({
  "to": "+14153902337",
  "channel": "sms"
});

req.headers({
  "x-rapidapi-host": "twilio-verify-phone-number.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}",
  "content-type": "application/x-www-form-urlencoded"
});

req.form({});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

When we run this program, the “to” number will receive a text with the generated verification code.

Once the number is sent, we can enter the verification code, serviceSid, and the phone number to the Check a Verification endpoint to confirm that the number was verified. Note that even though the phone number is not listed as a required parameter, you need to enter either the phone number or the verificationSid to run the program.

var unirest = require("unirest");

var req = unirest("POST", "https://twilio-verify-phone-number.p.rapidapi.com/Services/{{insert-sid-here}}/VerificationCheck");

req.query({
  "to": "+14151234567",
  "code": "123456"
});

req.headers({
  "x-rapidapi-host": "twilio-verify-phone-number.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}",
  "content-type": "application/x-www-form-urlencoded"
});

req.form({});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Once we run the program, we will see that our number has been verified. That is all you have to do to validate a phone number using the Twilio Verify API! With these code snippets, it is easy to see how you can integrate this API into your application’s sign up process.

Twilio SMS API: What is it?

The Twilio SMS API helps you add robust messaging capabilities to your applications. This API allows you to send and receive SMS messages, track the delivery of sent messages, and retrieve and modify message history. You can also buy available phone numbers using the SMS API.

How to Use the Twilio SMS API

To get started, navigate to the Twilio SMS API. From the API listing, you will need to subscribe to an API pricing plan before you can make any requests to the API. All of the Twilio APIs on the RapidAPI Marketplace are pay per use, so you do not have to pay a monthly subscription and only pay for the number of calls you make throughout the month.

Once you are subscribed to the API plan, go to the endpoints page to find all of the endpoints and code snippets you will need to integrate the API into your application.

List of SMS API Endpoints

The current endpoints for the SMS API are:

Example: Send a Text Message

For this tutorial, we will be using NodeJS, but you can use any of the SDKs we support.

To send a text message, you need to have a Twilio phone number. You can buy one by using the Buy a Phone Number endpoint. You can either make this request in the browser with the Test Endpoint button or implement it into your application using the code snippets. Both options will successfully purchase a functioning phone number.

There are three required parameters for Buy a Phone Number:

  • phoneNumberType: Select for ‘local’ ‘mobile’ or ‘toll free’ for this parameter. We purchased a ‘local’ for this example.
  • AccoundSid: If you do not already have an AccountSid, simply type “a” into the parameter and it will automatically be replace.
  • countryCode: The ISO-3166-1 country code for the country you want to buy the phone number from. For our example, we did the United States, written as “US”
var unirest = require("unirest");

var req = unirest("POST", "https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/{{a}}/IncomingPhoneNumbers.json");

req.query({
  "phoneNumberType": "local",
  "countryCode": "US"
});

req.headers({
  "x-rapidapi-host": "twilio-sms.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}",
  "content-type": "application/x-www-form-urlencoded"
});

req.form({});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Running the program will purchase a phone number. Make sure to take note of the phone number you purchased, you will need it when sending an SMS. If you forget, utilize the Fetch Multiple Phone Numbers endpoint to retrieve all of the phone numbers associated with your account.

Now that we have successfully purchased a phone number, we can use that number to send an SMS with the Message Resource endpoint.

We can use Send SMS (Create a Message Resource), which requires 4 parameters:

  • from: a Twilio phone number in E.164 format (see the section above called “Example: Lookup a Phone Number” for an explanation of E.164 format).
  • body: the text of the message you want to send (up to 1600 characters).
  • to: the destination phone number in E.164 format.
  • AccountSid: again, if you don’t have an AccountSid already just put “a” in this parameter.
var unirest = require("unirest");

var req = unirest("POST", "https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/{{your-accountsid-here}}/Messages.json");

req.query({
  "from": "{{phone_number_purchased_from_twilio}}",
  "body": "This is an example text message using the Twilio SMS API",
  "to": "{{to_phone_number}}"
});

req.headers({
  "x-rapidapi-host": "twilio-sms.p.rapidapi.com",
  "x-rapidapi-key": "{{your_rapidapi_key}}",
  "content-type": "application/x-www-form-urlencoded"
});

req.form({});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Running the program will send a text message with the body content to the destination we specified. We have now successfully used the Twilio SMS API to send a text message!

Conclusion

We can’t wait to see what you create using the Twilio APIs! Let us know what you end up making on Twitter @Rapid_API. Feel free to reach out to support@rapidapi.com if you have any questions about the API.

How to Use Twilio on RapidAPI

Related Twilio Resources

4.7/5 - (3 votes)
Share
Published by

Recent Posts

Power Up Your Enterprise Hub: New March Release Boosts Admin Capabilities and Streamlines Integrations

We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…

2 weeks ago

Unveiling User Intent: How Search Term Insights Can Empower Your Enterprise API Hub

Are you curious about what your API consumers are searching for? Is your Hub effectively…

3 weeks ago

Rapid Enterprise API Hub Levels Up Custom Branding, Monetization, and Management in February Release

The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…

1 month ago

Supercharge Your Enterprise Hub with January’s Release: Search Insights, Login Flexibility, and More!

This January's release brings exciting features and improvements designed to empower you and your developers.…

3 months ago

Enhanced Functionality and Improved User Experience with the Rapid API Enterprise Hub November 2023 Release

Rapid API is committed to providing its users with the best possible experience, and the…

5 months ago

The Power of Supporting Multiple API Gateways in an API Marketplace Platform

In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…

6 months ago