How to Generate One Time Password without any Charge using Twilio on Python

OTP as the name stands is an abbreviation to One Time Password that is used by many services providing organizations for their customer authentication-related matters. It is regarded as the safest way to maintain the privacy of a customer by providing only one-time access to the password provided and that too on the intended customer’s phone or mail. This is very helpful if you securely want to login into any kind of portal and also want to have tight security. Every application out there whether Netflix, Amazon, Google Play Store, Hotstar, etc. all require OTP to be put on by the user and then log in to their account. These OTPs are generated with the help of OTP providing companies and can be either alphanumeric, numeric, or alphabetical depending on the type of package you have chosen. These OTP providing companies generate these numbers with the help of programming languages or any kind of high-end tool and then sell them in the form of packages which are incurred by the bearer.

So, what if we create this OTP with the help of coding and then connect it to an API that provides initial free service for a few ones and then starts charging. Yes, it is possible with an API called Twilio. This is an amazing API that allows users to generate OTP free of cost for their work. Although the free service provides limited OTPs to be accessed by the users but yes getting free service for a particular p[eriod of time is desired by everyone out there.

The programming language that we will be using today for accessing this API is Python and the work will be carried out in Google Colaboratory that is a free cloud-based Jupyter Service provided by Google. So, let’s start!

Note: All the explanation stuff is done keeping in mind that you have Python or Anaconda already there in the system and you have already created a virtual environment in Anaconda. Also, pip must be there in the system to download Twilio or any other kind of API

Creating a Free Account in Twilio

For accessing the services of Twilio we need to create a free account in Twilio’s website. So let’s do this by following these steps:

  • Go to twilio.com and then click on the Sign-in option on the webpage and it will land you to a page wherein just fill up all the details and create your account.

Twilio for creating One time password

  • Once done just verify your email by clicking on the link sent by Twilio.
  • After this, it will again ask you to put your login credentials. So, just do that and wait.
  • Now, you need to verify your mobile number and then your account will be successfully created.

create twilio account

  • Click on yes where it will be asking “Do you Write Code?”
  • On the next page click on the preferred language you want to work on from the list given there. Here I am choosing Python.

Select Python to code with twilio

  • Now, just click on the skip to dashboard option to go to the main dashboard.

create twilio project to generate one time password

Dashboard Understanding for OTP

Here, under the dashboard, you will see various things like the amount up to which your free trial is active ($15), the authorization id, and the authorization token. These things are required by us to build the OTP generator. So just copy these under your notepad or any other text editor. Once done the next thing is to click on the Get Trial Number option provided in the dashboard as it will give you the number from which you will be receiving the OTPs. So, just copy that no. and save it somewhere in your text editor. Once all these things are done you are good to go to the implementation part using Python.

twilio project dashboard

Get a dial number

twilio phone number and API

Using Python to get OTP

Installation and Implementation part Using Python

  • To install the Twilio API you can just do pip install twilio in the command prompt provided by your system or through the command prompt provided by Anaconda and wait for the download to finish. Although here we are using Colab so the library can be downloaded within the console by typing !pip install twilio.
  • Once the library is installed the next step is to import the same using import twilio.
  • The next step is to import the random library from python by just typing import random. This library helps in generating random numbers from a given range using the random integer option.
  • The rest of the implementation part is shown below in the form of pictorial representation to give you a better idea as to things are working.

twilio 9

twilio 0

Once the OTP is created and generated then the same gets reflected on your mobile phone within seconds.

You can also use Twilio to generate messages and other kinds of stuff. For more understanding just read their official documentation contained in the website and then play around with the different services that this API provides.

Conclusion

So, this is how you can create your own OTPs with the help of Twilio API and that too with a few lines of code. Although the free service is limited to $15 the amount deducted per OTP is very minimal so we can use this service for a longer period. Once the credit limit is finished we can refill the same under the billing section of the dashboard. So, go to generate your OTP and receive the same in your mobile phone.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.