To add OTP verification to a React app with Fast2SMS, drop the <F2sOtp /> component into your page and point it at your own backend. The component shows the mobile field, the code boxes and the resend timer, and calls onVerified with a signed token once the number is verified through Smart OTP (SMS and WhatsApp with fallback). It works with Vite, Create React App, Remix and other React 17+ set-ups.
Download the React OTP widget example (zip)Download .zip
Download the complete OTP widget kit (zip)Download .zip
What you get
| File | What it is |
|---|---|
src/F2sOtp.jsx |
The component. Props: endpoint, otpId, form, onVerified, onSent, onError, texts, className, style. |
src/Examples.jsx |
LoginExample and SignupExample. |
src/f2s-otp-widget.js |
The browser widget the component wraps. |
The component talks only to your backend, never to Fast2SMS. React needs one of the kit’s backends (see the OTP widget overview); the Node.js with Express backend is the natural fit.
Before you start
- A Fast2SMS account with wallet balance and an approved DLT OTP template or WhatsApp Authentication/Utility template.
- A React 17 or newer app.
- A server where one of the kit’s backends can run.
Set up OTP in React
-
Create a Smart OTP template and copy its OTP ID
In the Fast2SMS panel, open Smart OTP > OTP Template List and click Add OTP Template. Pick SMS Channel with your approved DLT OTP template, or WhatsApp Channel with an approved Authentication or Utility template. You can add a fallback on the other channel. Then copy the 10-character OTP ID from the card. It decides the channel, the template and the fallback for every code the widget sends.

Copy the OTP ID from the template card. -
Run a backend that holds your API key
The widget never calls Fast2SMS. It calls a small backend that you run, and only that backend knows your API key. The full kit has ready backends for PHP hosting (one file), Node.js with Express, Python Flask, Django, Laravel, Cloudflare Workers, Vercel or Netlify functions and Google Apps Script (see the OTP widget overview). Copy the key from API Key under UTILITY in the panel, and give the backend these three settings:
FAST2SMS_API_KEY=YOUR_API_KEY F2S_OTP_ID=YOUR_OTP_ID F2S_TOKEN_SECRET=at-least-32-random-charactersF2S_TOKEN_SECRETis any random text of 32 or more characters. The backend will not start (or, on PHP and serverless hosts, refuses every request) if one of these is missing or wrong. For local work, proxy/f2s-otpfrom your dev server to the backend.
The key goes into your backend settings only. -
Proxy the endpoint in development
With Vite, add a proxy so the app and the backend look like one site. The Node.js backend kit listens on port 3000 by default.
// vite.config.js export default { server: { proxy: { '/f2s-otp': 'http://127.0.0.1:3000' } }, };In production, serve the endpoint from the same domain, or add your site to the backend’s
F2S_ALLOWED_ORIGINS. -
Add the component
Copy
F2sOtp.jsxandf2s-otp-widget.jsintosrc/, then use it on your log-in page:import F2sOtp from './F2sOtp.jsx'; export default function Login() { async function onVerified({ token }) { await fetch('/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ f2s_token: token }), }); } return <F2sOtp endpoint="/f2s-otp" onVerified={onVerified} />; }
After Send OTP: six code boxes and a Resend countdown.
Protect a sign-up form in React
Pass a form ref instead of using the log-in box. The form can then be submitted only after verification, and the widget adds a hidden f2s_token field.
import { useRef } from 'react';
import F2sOtp from './F2sOtp.jsx';
export function Signup() {
const form = useRef(null);
return (
<>
<form ref={form} action="/signup" method="post" data-mobile-field="mobile">
<input name="name" required />
<input name="mobile" type="tel" required />
<div data-f2s-slot />
<button type="submit">Create account</button>
</form>
<F2sOtp endpoint="/f2s-otp" form={form} />
</>
);
}
Check the token on your server
Never log anyone in from the onVerified event alone. Your /login or /signup route must check the token with the backend kit’s helper and use the number it returns:
// Node.js / Express backend kit
const v = await otp.verifyToken(req.body.f2s_token, { mobile: req.body.phone });
if (!v) return res.status(403).send('Mobile number not verified.');
// v.mobile is the verified 10-digit number. A token works only once.
<?php
// PHP single-file backend kit
require __DIR__ . '/f2s-otp.php';
$v = \F2sOtp\verify_token($_POST['f2s_token'] ?? '', $_POST['phone'] ?? null);
if ($v === null) { http_response_code(403); exit('Mobile number not verified.'); }
// $v['mobile'] is the verified 10-digit number.
Good to know
- Keep the key out of React. Never put the Fast2SMS API key in React code or in
VITE_*orREACT_APP_*variables. Those end up in the browser bundle. - Texts are read once. To change the texts, give the component a new
keyso it rebuilds. - StrictMode is fine. The component mounts one widget even when React mounts, unmounts and mounts again.
- Theming: CSS variables such as
--f2s-primaryon.f2s-otp.
Limits and cost
| Backend setting | Default | What it does |
|---|---|---|
F2S_RESEND_WAIT |
30 seconds | Wait between two sends to one number. |
F2S_LIMIT_PER_NUMBER |
5 per hour | Sends to one mobile number. |
F2S_LIMIT_PER_IP |
20 per hour | Sends from one IP address. |
F2S_LIMIT_VERIFY_PER_IP |
60 per hour | Verify calls from one IP address. |
F2S_MAX_VERIFY |
5 | Wrong codes allowed for one code. |
F2S_TOKEN_TTL |
600 seconds | How long the verified token lasts. |
F2S_CAPTCHA |
off | turnstile or recaptcha, with F2S_CAPTCHA_SECRET. |
Resend sends the same code again. Fast2SMS allows it within 10 minutes of the first send, up to 5 sends in total. Sending a fresh code for a number cancels the older code. If the template has a fallback and the first channel fails, Fast2SMS sends the same code once on the other channel.
Each send and each resend is charged at your Smart OTP rate. Verify is free. If a fallback message goes out on the second channel, that message is charged too, and so is a Test OTP from the template card, because it is a real send. See the current rates on the Smart OTP page.
Sending in bulk? For high OTP volumes our team offers discounted rates. Talk to our team or see OTP pricing.
How this example was tested
The React example was bundled with esbuild and run in headless Chrome with React 18.3.1 in <StrictMode>, against the Node.js backend and a test server that answers like Fast2SMS. All 3 checks passed: one widget per mount, onVerified gets the token, and the protected form submits a token the server accepts. No real SMS was sent, so do one live send with your own template before launch.
Frequently asked questions
Which backend should I use with React?
Any backend in the kit works, because they all speak the same JSON. If your API is already Node.js, use the Express backend. For a static React site, the Cloudflare Worker or Vercel/Netlify backend works.
Can I call the Fast2SMS OTP API straight from React?
No. That would put your API key in the browser, where anyone can copy it and spend your wallet. The component calls your backend, and the backend calls Fast2SMS.
Does it work with Next.js?
Use the separate Next.js example. It adds a route handler as the backend and a client component that loads the widget in the browser only.
How do I send OTP on WhatsApp instead of SMS?
Create a WhatsApp OTP template in Smart OTP and use its OTP ID. If the page needs a different OTP ID from the default, add it to the backend’s F2S_OTP_IDS and pass it as the otpId prop.
What happens when the user taps Resend?
The same code is sent again. Fast2SMS allows that within 10 minutes and up to 5 sends in total, and each resend is charged.
Can I change the button text?
Yes. Pass texts, for example { send: 'Get code' }. Texts are read once, so change the component’s key to apply new ones.
Related guides
- Smart OTP in Fast2SMS: set up OTP on SMS and WhatsApp with fallback
- Build a phone number login with OTP (Fast2SMS Smart OTP)
- OTP verification screen UX: input field, resend timer and error messages
- OTP fraud and SMS pumping: how to protect your OTP form and wallet
- Fast2SMS Verify OTP API: how OTP verification works (with code)