An OTP plugin for Strapi that enables phone number verification using Twilio for sending and validating OTPs
A Strapi plugin that provides functionality for generating and validating OTPs (One-Time Passwords) for phone number verification. This plugin uses Twilio to send OTPs via SMS, making it easy to add OTP-based authentication to your Strapi application.
Before using this plugin, you must have a Twilio account. You'll need the following credentials from Twilio:
Step 1: Install via NPM To install the plugin, run the following command in your project:
1npm install otp-plugin
Step 2: Enable the Plugin In your config/plugins.js file, enable the otp-plugin:
1module.exports = {
2 'otp-plugin': {
3 enabled: true,
4 resolve: './node_modules/otp-plugin',
5 },
6};
Step 3: Configure Environment Variables You need to configure the following environment variables in your .env file to enable Twilio for sending OTPs via SMS:
1TWILIO_ACCOUNT_SID=your_twilio_account_sid
2TWILIO_AUTH_TOKEN=your_twilio_auth_token
3TWILIO_PHONE_NUMBER=your_twilio_phone_number
Step 4: Configure HTTPS Agent (Optional) If your environment requires custom HTTPS settings (e.g., for self-signed certificates), configure the HTTPS agent in server/config/index.js:
1const https = require('https');
2
3const agent = new https.Agent({
4 rejectUnauthorized: false,
5});
6
7module.exports = {
8 http: {
9 agent,
10 },
11};
This plugin provides several API endpoints to generate, validate, and manage OTP logins. Below are the key endpoints:
Generates an OTP and sends it to the specified phone number.
Example:
1curl -X POST http://localhost:1337/otp-logins/generate \
2-H "Content-Type: application/json" \
3-d '{"phoneNumber": "1234567890"}'
Validates the OTP for the given phone number.
Example:
1curl -X POST http://localhost:1337/otp-logins/validate \
2-H "Content-Type: application/json" \
3-d '{"phoneNumber": "1234567890", "otpCode": "123456"}'
The OTP plugin creates a content type called OtpLogin in your Strapi admin panel. You can view and manage OTP login records, including phone numbers, OTP codes, expiration times, and usage status.
The plugin exposes services that can be called from other parts of your Strapi application:
Generates and sends an OTP to the given phone number and logs the request in the database.
1const otpService = strapi.plugin('otp-plugin').service('otp');
2await otpService.generateAndCreateOtp('1234567890');
Validates the OTP for the given phone number.
1const isValid = await otpService.validateOtp('1234567890', '123456');
This plugin is licensed under the MIT License.
npm install strapi-otp-plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.