This Flutter example gives you a ready OTP login screen: the user types a mobile number, gets a code by SMS or WhatsApp through Fast2SMS Smart OTP, types it or taps the code the keyboard suggests, and your app gets a signed “verified” token. The app talks only to your own backend, which holds the API key. It needs nothing but the http package.
Download the Flutter OTP widget example (zip)Download .zip
Download the complete OTP widget kit (zip)Download .zip
--dart-define, or in a config file shipped with the app. APK and IPA files can be unpacked. The key lives only in the backend’s environment, and the app only knows your backend URL.What is in the Flutter example
| File | What it does |
|---|---|
lib/f2s_otp_client.dart |
F2sOtpClient: send, verify and resend against your endpoint, plus cleanMobile. |
lib/f2s_otp_screen.dart |
F2sOtpScreen: phone field with +91, Send, code field with SMS autofill (AutofillHints.oneTimeCode), resend countdown, errors and onVerified(token, mobile). |
lib/main.dart |
A small app that shows the screen and where to send the token. |
pubspec.yaml |
Dart SDK 3 and the http package. |
If you do not need the demo app, copy the two f2s_otp_* files into your own project. Every app example needs one of the kit’s backends to talk to (see the OTP widget overview).
Before you start
- A Fast2SMS account with wallet balance.
- An approved DLT OTP template, or a WhatsApp Authentication or Utility template.
- Flutter with Dart 3, and a server for one of the backends.
Set up OTP in Flutter
-
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. The app talks to this backend over HTTPS; use a URL such ashttps://www.example.com/f2s-otp.
The key goes into your backend settings only. -
Set the endpoint and run the app
Unzip the Flutter example. In
lib/main.dart, setkOtpEndpointto your backend URL, then run:flutter pub get flutter runTo test on the Android emulator with a backend on your computer, use
http://10.0.2.2:3000/f2s-otp. Plainhttpneedsandroid:usesCleartextTraffic="true"(or a network security config) in debug builds only. Never ship that in a release build.
Use the screen in your own app
import 'package:http/http.dart' as http;
import 'f2s_otp_client.dart';
import 'f2s_otp_screen.dart';
const kOtpEndpoint = 'https://www.example.com/f2s-otp'; // your backend, never fast2sms.com
F2sOtpScreen(
client: F2sOtpClient(kOtpEndpoint),
onVerified: (token, mobile) async {
// Send the token to YOUR login API. Your server checks it, then starts the session.
await http.post(Uri.parse('https://www.example.com/api/login'), body: {'f2s_token': token});
},
)
To use a second OTP ID, for example a WhatsApp-only template, pass F2sOtpClient(kOtpEndpoint, otpId: 'YOUR_OTP_ID'). The backend accepts it only if it is listed in F2S_OTP_IDS.
What the app sends to your backend
| Request body | Success answer |
|---|---|
{"action":"send","mobile":"9999999999"} |
ok, masked, resend_after, length, expires_in, ticket |
{"action":"verify","mobile":"9999999999","otp":"123456","ticket":"..."} |
ok, masked, token, expires_in (600) |
{"action":"resend","mobile":"9999999999","ticket":"..."} |
same as send |
Every call is a POST to your endpoint with the headers Content-Type: application/json and X-F2S-Widget: 1. Errors come back as {"ok":false,"reason":"...","message":"..."}, and the screen shows message, which is plain English. The ticket from send is kept in memory only; verify and resend need it.
Check the token on your server
The onVerified callback in the app proves nothing to your server on its own. Send the token to your own log-in or sign-up API, check it there with the backend kit’s helper, and only then create the session:
// 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.
The token lasts 10 minutes by default and works once. Use the number the helper returns, not a number the app sends separately.
How the backend protects an app
A browser sends an Origin header, and the backend uses it to block other websites. Mobile apps send no Origin header, so that check does not apply to them. What protects your wallet against app requests is:
- the resend wait (30 seconds by default),
- the per-number and per-IP limits (5 and 20 sends per hour by default),
- the limit on wrong codes (5 by default),
- an optional captcha (Cloudflare Turnstile or reCAPTCHA, off by default).
The example screen does not include a captcha. If you turn F2S_CAPTCHA on, your app must send a captcha token in the captcha field of the send call. For high volume, also check Google Play Integrity on Android or App Attest on iOS on the backend before it sends a code, so only your real app can ask for codes.
Do not set an Origin header in the app. If you do and it is not in the backend’s allowed list, the backend answers “This page is not allowed to use this OTP service.”
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. |
Fast2SMS also has its own limits in Smart OTP > OTP Settings: Max Verify OTP Limit (Per SMS) and SMS Resend / Time Limit (Seconds). The backend’s own limits apply first.

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.
Test status
Common problems and fixes
| What you see | Why | Fix |
|---|---|---|
| “Could not reach the server…” | Wrong endpoint, backend not running, or plain http blocked. |
Check kOtpEndpoint. On the emulator use 10.0.2.2, not localhost. |
| “We could not send the code right now…” | A backend setup problem: wrong OTP ID, low balance, key refused. | Read the backend log. It names the real reason. |
| “Please wait N seconds…” | The resend wait. | Wait for the countdown. |
| Code sent but nothing arrives | Sent means Fast2SMS accepted it, not that the phone got it. | Check Smart OTP > Delivery Report. |
| The keyboard does not suggest the code | Autofill depends on the phone and keyboard. | The user can still type it. |
Frequently asked questions
Can I call the Fast2SMS OTP API directly from Flutter?
You should not. The API key would sit inside the app, and anyone can pull it out of an APK or IPA. The app calls your backend, and only the backend calls Fast2SMS.
Which backend should I use with a Flutter app?
Any backend in the kit. If you already have a PHP, Node.js or Python server, use that one. With no server, a Cloudflare Worker or a Vercel/Netlify function works.
Does the code fill in automatically?
The code field uses AutofillHints.oneTimeCode, so many keyboards suggest the code from the SMS. It depends on the phone and keyboard, and iOS does not suggest codes that arrive on WhatsApp.
How do I log the user in after OTP?
Post the token from onVerified to your own login API. The server checks it with the kit’s helper, finds the user by the verified number and starts the session.
Is the token safe to keep in the app?
Use it right away. It lasts 10 minutes by default and works only once, so there is no reason to store it.
Does Resend send a new code?
No. Resend sends the same code again, within 10 minutes and up to 5 sends in total. Each resend is charged.
Related guides
- Flutter OTP verification with Fast2SMS (and React Native): keep the API key on your server
- OTP autofill on Android, iPhone and websites: SMS Retriever, app hash and DLT
- OTP for website login vs app login: what changes and what stays the same
- Smart OTP in Fast2SMS: set up OTP on SMS and WhatsApp with fallback
- OTP fraud and SMS pumping: how to protect your OTP form and wallet