In this guide · 8 sections
One-time passwords (OTPs) sent by SMS are how most Kenyan apps confirm a phone number, approve a login or authorise a payment. The SMS part is simple. The security comes from how you generate, store and check the codes. This guide covers both.
The short version
- Generate a random six-digit code on your server, store only a hash, and expire it in minutes.
- Limit attempts and resends per phone number.
- Send it through an SMS API under your sender ID, and use delivery reports to troubleshoot.
How an OTP flow works
- The user enters their phone number on your site or app.
- Your server creates a code, stores a hash of it with an expiry time, and sends it by SMS.
- The user types the code they received.
- Your server checks it against the stored hash, within the time limit and attempt limit, then marks it used.
Generate and store codes safely
- Use a cryptographically secure random generator (for example
crypto.randomIntin Node.js orrandom_intin PHP), neverMath.random. - Hash the code before storing it, like a password, and store the phone number, expiry time and attempt count with it.
- Expire codes after a few minutes and after one successful use.
- Never log codes in plain text, including in your SMS request logs.
Limit attempts and resends
Without limits, a six-digit code can be guessed, and an open “send code” button can be abused to send thousands of SMS at your expense. Set:
- a maximum number of wrong attempts per code (for example five), after which the code is cancelled;
- a wait before a new code can be requested (for example 30 to 60 seconds), with a daily cap per number;
- rate limits per IP address and device on the endpoint that sends codes, plus a CAPTCHA if you see abuse.
Write the OTP message
Keep it short, name your service, give the expiry, and warn against sharing:
Your [App] code is 482913. It expires in 5 minutes. Never share this code, even with [App] staff.
Send OTPs under your registered sender ID so customers can tell a genuine code from a scam, and keep them plain text so they fit one SMS. Never put marketing in an OTP message.
Send the code through an SMS API
Call the SMS API from your back end, never the browser. With Ndovubase, a send request is an HTTPS POST to https://bulk.ndovubase.com/sms/v3/sendsms with your API key, sender ID, the number and the message; the response gives a message ID for each recipient. A simplified Node.js example:
import crypto from "node:crypto";
const code = String(crypto.randomInt(0, 1_000_000)).padStart(6, "0");
await saveOtp(phone, hash(code), Date.now() + 5 * 60_000); // your storage
const res = await fetch("https://bulk.ndovubase.com/sms/v3/sendsms", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
api_key: process.env.NDOVUBASE_API_KEY,
shortcode: "YOURSENDERID",
mobile: phone,
message: `Your MyApp code is ${code}. It expires in 5 minutes. Never share this code.`,
service: 0,
response_type: "json",
}),
});
const [result] = await res.json(); // store result.message_id
Check the API documentation for the current fields and status codes, and our guide to integrating a Bulk SMS API for error handling and delivery reports.
Use delivery reports
An accepted request doesn’t guarantee delivery. Store the message ID, set a delivery-report callback, and show support staff the delivery status when a customer says a code never came. Repeated failures to one number usually mean a wrong or inactive number.
Get OTP SMS running
See OTP and transactional SMS for how we support verification messages, check Bulk SMS prices (OTPs are billed like any SMS), and create an account to get your API key.
Frequently asked questions
How long should an OTP be valid?
A few minutes is typical, often five to ten. Long enough for the SMS to arrive and be typed in, short enough that an old code is useless.
How many digits should an OTP have?
Six digits is the common choice: easy to type, and with attempt limits it is hard to guess.
What if the OTP SMS doesn’t arrive?
Check the delivery report for that message, let the user request a new code after a short wait, and confirm the number format. Offering a second channel, such as a voice call or WhatsApp, can help for persistent failures.