To add OTP verification to a Vue 3 app, use the <F2sOtp> component from the Fast2SMS OTP widget kit. It shows the mobile field, the code boxes and the resend timer, and emits verified with a signed token once the number is verified through Smart OTP by SMS or WhatsApp. The component calls your own backend, which holds the API key.
Download the Vue OTP widget example (zip)Download .zip
Download the complete OTP widget kit (zip)Download .zip
What you get
| File | What it is |
|---|---|
src/F2sOtp.vue |
The component. Props: endpoint, otpId, form, texts. Events: verified, sent, error. |
src/LoginExample.vue |
Log in with OTP, then post the token to /login. |
src/f2s-otp-widget.js |
The browser widget the component wraps. |
It works with Vite and with Nuxt on the client side. The component 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.
- A Vue 3 app and a place to run one of the backends.
Set up OTP in Vue
-
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. During development, proxy/f2s-otpfrom the Vite dev server to the backend (server.proxyinvite.config.js).
The key goes into your backend settings only. -
Add the component
Copy
F2sOtp.vueandf2s-otp-widget.jsintosrc/:<script setup> import F2sOtp from './F2sOtp.vue'; async function onVerified({ token }) { await fetch('/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ f2s_token: token }), }); } </script> <template> <F2sOtp endpoint="/f2s-otp" @verified="onVerified" /> </template>
After Send OTP: six code boxes and a Resend countdown. -
Or protect a form
Pass a template ref of the form. The form can be submitted only after verification, and the widget adds a hidden
f2s_tokenfield.<form ref="signup" action="/signup" method="post" data-mobile-field="phone"> <input name="name" required> <input name="phone" type="tel" required> <button type="submit">Create account</button> </form> <F2sOtp v-if="signup" :form="signup" />
Check the token on your server
The verified event alone proves nothing to your server. Your /login or /signup route must check the token with the backend kit’s helper:
// 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
- Nuxt: render the component only on the client, inside
<ClientOnly>. The widget needs a browser. - Keep the key out of Vue. Never put the Fast2SMS API key in
VITE_*variables or front-end code. They end up in the browser bundle. - Theming: CSS variables such as
--f2s-primaryand--f2s-radiuson.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.
Test status
Common problems and fixes
| What you see | Why | Fix |
|---|---|---|
| Nothing shows in Nuxt | The component rendered on the server only. | Wrap it in <ClientOnly>. |
| “Could not reach the server.” | The dev proxy or the endpoint URL is wrong. | Check server.proxy and the endpoint prop. |
| “This page is not allowed to use this OTP service.” | The app is on another domain than the backend. | Add the app’s origin to F2S_ALLOWED_ORIGINS. |
| “We could not send the code right now.” | Key, OTP ID, balance or template problem. | Read the backend log. |
Frequently asked questions
Does the OTP component work with Vue 2?
The component is written for Vue 3 with <script setup>. The widget inside it is plain JavaScript, so you can mount it in Vue 2 with Fast2smsOtp.mount() yourself.
Can I use it in Nuxt?
Yes, on the client side. Put it inside <ClientOnly> and run the backend as a separate service or server route that holds the API key.
Which backend is best for a Vue app?
Any of them. Use the one that matches your server: PHP, Node.js, Python, Laravel, Django, or a Cloudflare Worker or Vercel/Netlify function for a static Vue site.
Is Resend charged?
Yes. Each send and each resend is charged; verify is free. Resend sends the same code again within 10 minutes, up to 5 sends in total.
Can I send the OTP on WhatsApp?
Yes. The OTP ID decides the channel. Use a WhatsApp OTP template, or an SMS template with a WhatsApp fallback.
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
- Smart OTP settings: resend wait time and max verify attempts