API with NestJS #41. Verifying phone numbers and sending SMS messages with Twilio

JavaScript NestJS TypeScript

This entry is part 42 of 42 in the API with NestJS

In our web applications, we often need to send messages to our users. Doing that through email is enough in a lot of cases, but we can also use SMS. In this article, we look into how we can use Twilio for verifying phone numbers provided by our users and sending messages.

Setting up Twilio

First, we need to create a Twilio account. It is a straightforward process that doesn’t require us to provide a credit card number.

After creating the Twilio account, we need to set up a service. In Twilio, a service acts as a set of common configurations used to perform phone number verification.

To define a service, we need to go to the services dashboard. When choosing a name for the service, remember that our users can see it.

The crucial part of the above process is the service id. We need to use it along with the account sid and auth token that we can find in the console.

app.module.ts

.env

Using Twilio with Node.js

To use Twilio with Node.js, we can use the official Twilio library. It comes with all necessary TypeScript declarations built-in.

Make sure to install the correct library. A few months ago a malicious package called was published that aimed to compromise the machines of people who downloaded it. If you want to know more, check out this article.

Let’s create the that uses the above library along with our environment variables.

sms.service.ts

Verifying phone numbers

For us, the first step in verifying phone numbers is adding additional fields in the entity.

user.entity.ts

It is crucial for the to be in the right format. The Twilio documentation suggests a regular expression that we can use.

register.dto.ts

If we would like to be more strict with the phone number validation, we could use the Lookup API that Twilio provides. With it, we can make a request to the Twilio API every time our users set a phone number and check if it is valid. Remember that every request we make to the Lookup API costs a little.

Initiating the SMS verification

Let’s add a function to our that can initiate SMS verification.

sms.service.ts

Let’s also create a that uses it.

sms.controller.ts

Requesting the above endpoint results in Twilio sending the SMS to the user.

Twilio figures out the language based on the country code in the phone number. We could override it by using the property:

Confirming the verification code

Now, we need to create a way for the users to send the verification code back to our API. To do that, let’s create an additional method in our :

sms.service.ts

You might notice that we use the method above. We first need to define it.

users.service.ts

The last step of implementing the code verification is adding it to the controller.

sms.controller.ts

Sending messages

The first step in sending messages through Twilio is choosing a phone number from the provided list.

We have an amount of money we can spend for free during the trial period in Twilio.

We also need to add the purchased number to our environment variables.

app.module.ts

.env

The last step is adding a new method to our :

Using the above method results in sending SMS to the provided phone number.

Summary

In this article, we’ve used Twilio for implementing SMS messages. This included both validating the phone numbers of our users and sending messages. Although the trial period in Twilio has some limitations, we are free to experiment with the API. Therefore, feel free to look into more of the built-in features and explore.

 

Series Navigation<< API with NestJS #42. Authenticating users with Google
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments