Effortlessly retrieve OTPs from SMS in your Capacitor apps.
To install the sms-retriever
plugin, run the following commands:
npm install sms-retriever
npx cap sync
Defines the interface for the SmsReaderPlugin.
readSMS(options: ReadSMSOptions) => Promise<ReadSMSResult>
Reads the OTP from SMS messages based on the provided search string.
Param | Type | Description |
---|---|---|
options |
ReadSMSOptions |
- An object containing the search string. |
Returns: Promise<ReadSMSResult>
startWatching(options: ReadSMSOptions) => Promise<ReadSMSResult>
Starts watching for SMS messages to read the OTP based on the provided search string.
Param | Type | Description |
---|---|---|
options |
ReadSMSOptions |
- An object containing the search string. |
Returns: Promise<ReadSMSResult>
stopWatching() => Promise<void>
Stops watching for SMS messages.
Represents the result of reading SMS.
Prop | Type | Description |
---|---|---|
otp |
string |
The OTP extracted from the SMS. |
Represents the options for reading SMS.
Prop | Type | Description |
---|---|---|
searchString |
string |
The search string used to find the OTP in the SMS. |
Here's a quick example of how to use the sms-retriever
plugin in your Capacitor app:
import { SmsReader } from 'sms-retriever';
async function watchForOTP() {
try {
const result = await SmsReader.startWatching({ searchString: "OTP: " });
console.log('Received OTP:', result.otp);
// Optionally stop watching after receiving OTP
await stopWatchingForOTP();
} catch (error) {
console.error('Error watching for OTP:', error);
}
}
async function stopWatchingForOTP() {
try {
await SmsReader.stopWatching();
console.log('Stopped watching for OTP');
} catch (error) {
console.error('Error stopping SMS watcher:', error);
}
}
// Start watching for OTP
watchForOTP();
With this setup, the plugin will start watching for SMS messages that contain the specified search string, extract the OTP, and stop watching once the OTP is retrieved.
This project is licensed under the MIT License. See the LICENSE file for more details.