-
🚀 Auto-Reading OTP in React Native with react-native-otp-auto-verify
One of the small but painful UX problems in mobile apps is OTP verification. Traditionally, users have to manually type a 4–6 digit code from an SMS. That friction can lead to abandoned flows and frustrated users.
I built react-native-otp-auto-verify — a library that automatically listens to incoming SMS messages on Android and extracts the OTP when it arrives. No manual input needed ✨
This article walks through why this matters, how it works, and how to integrate it into your React Native app.
🔍 What It Does
react-native-otp-auto-verify uses Android’s SMS Retriever API to:
- Automatically listen for incoming SMS with your OTP.
- Extract the OTP code (e.g., 4–6 digits) in the app without user interaction.
- Provide easy hooks and imperative APIs for listeners.
This means you can build a seamless login/verification experience where users never have to tap to paste their code — the app just picks it up. (GitHub)
Note: this is Android-only because iOS doesn’t allow arbitrary SMS reads for security/privacy reasons.
🛠️ How It Works — Under the Hood
Android offers the SMS Retriever API, which can “watch” for a specially formatted SMS that contains your app’s hash.
- Generate App Hash The library exposes a method to generate your app’s hash:
import { getHash } from "react-native-otp-auto-verify";
const [hash] = await getHash();
You send this hash along with your OTP SMS payload from your backend.
- Format SMS Correctly Your SMS must include the hash at the end:
<#> Your verification code is 123456
AbCdEfGhIjK
SMS Retriever will only deliver messages that include this hash. (GitHub)
- Listen & Extract the Code Use the provided hook or imperative listener:
import { useOtpVerification } from "react-native-otp-auto-verify";
export function OtpScreen() {
const { hashCode, otp, timeoutError, startListening, stopListening } =
useOtpVerification({ numberOfDigits: 6 });
useEffect(() => {
startListening();
return () => stopListening();
}, []);
return (
<View>
<Text>{otp ?? "Waiting for SMS…"}</Text>
</View>
);
}
You’ll receive the OTP directly once the SMS arrives. (GitHub)
📌 API at a Glance
| API | What it does | |
|---|---|---|
getHash() |
Returns Android app hash to embed in SMS | |
useOtpVerification() |
Hook to start/stop listener and get OTP | |
activateOtpListener(handler) |
Imperative listener with callback | |
removeListener() |
Manually remove listener | |
extractOtp(sms, numberOfDigits) |
Utility to parse OTP from text | (GitHub) |
đź§© Why This Matters
There are a few alternatives out there (e.g., react-native-otp-verify on npm), but many are outdated or don’t support newer React Native architectures. (npm)
With this library:
✅ Works with React Native ≥ 0.60+ and autolinking
âś… Built with TurboModules support
âś… Cleaner, hook-friendly API
âś… Helps developers ship smoother OTP flows
đź§Ş Common Pitfalls
- The SMS must include the app hash — otherwise the platform won’t deliver it.
- The listener times out after ~5 minutes — handle retries in your UI. (GitHub)
Two Verification Methods
This library provides two different approaches for SMS OTP verification:
Method User Action SMS Format Use Case
SMS Retriever None (Automatic) Requires app hash Best for your own backend
SMS User Consent User taps "Allow" Any format Works with any SMS sender
📦 Try It Yourself
Install via:
npm install react-native-otp-auto-verify
Then format your backend’s SMS to include the app hash. That’s all you need for near-effortless OTP flows.
Check out the full code and docs on GitHub:
➡️ [react‑native‑otp‑auto‑verify on GitHub](https://github.com/kailas-rathod/react-native-otp-auto-verify?tab=readme-ov-file)
If you want, I can also help you write a demo app using this package or create a tutorial with screenshots/code sandbox!
Top comments (0)