Use Twilio's Lookup API to automatically verify numbers submitted by new users.
Speaker 0: Twilio provide a lookup API, which allows you to validate a phone number against a set of different criteria. And today in quick connect, we're going to use it to validate that a new user's phone number is a mobile number. If it is, that user can be created. And if it isn't, then we will block creation until they provide a mobile number. So with that, let's get started.
In your Twilio console, take note of your account SID and auth token. Make sure to keep these secret. We'll be using them later. Before starting this recording, I also added a phone string field, to our direct to c users collection. So on top of the additional kind of required fields, we now also have a phone number being stored.
So let's get started with our flow. Inside of directors, create a new flow. I'm going to call this one validate phone is mobile. We're going to make this an event hook, but, critically, we're going to make it a blocking hook, which means all of the logic of this flow will be executed before the database transaction is made, meaning that if we create some form of error inside of the flow, the user will not be created. Inside of collections, make sure direct to users is ticked and, and that the scope is items dot create.
You may additionally choose to run this logic on items dot update, but for now, we'll stick with create. So once a user is created, let's actually do that. All we're gonna do is add a phone number here. Here we go. We'll hit check, and we'll refresh just to see what that data looks like.
We see that inside of payload are all of the fields that had data entered into them, including, and in this case, only phone. So there's our phone number there. Twilio also provides some tools to help you format phone numbers that may be formatted inconsistently, but that's outside of the scope of this episode. So we are formatting this as required by Twilio with the plus, the country code, and then the phone number itself. Let's go and do a lookup.
So we'll call this lookup lookup And we're going to do this as a webhook request URL, a get request to this URL which will look up phone numbers And then in here, we're going to insert the phone number of the new user. So trigger dot payload dot phone. We will get some data back if we stop here, but we're additionally going to add the line type intelligence data package, I think they called them, which will provide more information about the number, including what we need. So field capital f equals line type intelligence. Fantastic.
Now we need to authenticate ourselves with our SID and our auth token authorization now authorization. Now this is important because what Twilio require is what is known as basic h HTTP auth and flows only, only support adding headers. So what we're gonna do here is use the word basic, and now we need to turn our SID and token into a value that will be supported in this format. This is how you do it on the terminal. So you start with the first command, you store your Sid.
So you replace this with your Sid, you replace this with your token, and then you run this command in your terminal with the dollar signs. And what that will do is spit out a string, which you can use. I've done this ahead of time. So heading back to our flow, we provide the output of what was in our terminal for our auth. Let's hit save now, and let's run this to test it.
So let's once again create a new user. We'll put in a valid phone number. We'll go back to our flow and refresh to see our new log, and we'll look at the latest run. And we see here that inside of data, we have some information like the country call, the calling country code, the country code, the actual two digits for the for the country, the national format, the international format. But what we also have here is the line type intelligence, which was the data packet that we asked for.
Now for some applications, it's important that the phone number provided is a mobile, isn't a virtual number, and can support SMS, of which type mobile is true. There's a whole list in the Twilio documentation as to which types can exist. But right now, for this example, we're going to check that mobile exists. So what we're gonna do here is check inside of data, line type intelligence type, and make sure that that is mobile. So let's add a condition in here, and what we're gonna do is check that the lookup data Line line type intelligence, like so, is, there was one inside of it, type.
That's right. Type is equal to mobile. Fantastic. So this is us just traversing that object that Twilio provided back. We'll hit save.
And so when it's true, we're actually going to add nothing. We're going to say, hey. This flow concludes. There was no error. This was blocking.
Now it isn't blocking anymore. Go create the user. But in the case that it fails, we want to stop this in its steps. And the way we're gonna do that is we're gonna run a script. It'll not terribly elegant, but we're just gonna throw a new error.
So throw new error. Incorrect, phone type must be a mobile. And let's save let's try this once more let's create the user and this should be successful Fantastic. And I don't have another phone number to test it with, but that means all of this was successful, and we went out the success path, which then just, resolved. And if that was not true, if this condition validation was not met, we would run the script and throw an error.
Now there's a lot we can do with the lookup API, actually. You can check which carrier it is. You can check the fraud risk of a phone number. And all of those, basically, here's all the things we can do. The SMS pumping risk, whether or not the number has been reassigned, whether or not there's been a SIM swap, and so on.
And all you would do is add extra query parameters to the end of the lookup and then change the conditional to check that certain criteria are met. So this was a little simple example of using the Twilio lookup API to validate phone numbers against some property that is provided by Twilio. You can tweak this for your specific use cases and build more secure applications. Hope you enjoyed this episode of quick connect, and I'll see you in the next episode.