Skip to main content
Version: 1.0

2.1.1.3 OTP Method

OTP Method

The OTP Verification process allows users to verify their phone numbers by receiving a one-time password (OTP) via SMS, and then entering the OTP in your app to complete the verification.

Get started

Initializing VerifySpeed

To start, initialize an instance of OtpProcessor:

dart
  final otpProcessor = VerifySpeed.instance.initializeOtpProcessor(); 

Verifying Phone Numbers with OTP

Use verifyPhoneNumberWithOtp to send the OTP to the user's phone number with these parameters:

  • phoneNumber: phoneNumber: The user’s phone number (including country code).
  • verificationKey: Unique key provided by your backend to ensure verification integrity.
dart
  await otpProcessor.verifyPhoneNumberWithOtp(
phoneNumber: 'PHONE_NUMBER', // e.g., '+1234567890'
verificationKey: 'VERIFICATION_KEY', // unique key provided by your backend
);
warning

This package does not include phone number validation. Since validation occurs server-side, it’s best to handle initial validation on your end to reduce user wait times. Use a package like libphonenumber for efficient phone number validation.

Validating OTP

After the user receives the OTP, call validateOtp with these parameters:

  • otp: The OTP received from the user.
  • verificationKey: Unique key provided by your backend to ensure verification integrity.
  • onSuccess: Callback triggered on successful verification, returning a token to retrieve the user’s phone number.
  • onFailure: Callback triggered if verification fails, providing error details including type and message.
dart
    await otpProcessor.validateOtp(
otpCode: 'OTP', // OTP received from the user
verificationKey: 'VERIFICATION_KEY', // unique key provided by your backend
onSuccess: (token) {
log('token: $token');
},
onFailure: (error) {
log('error: ${error.message} type: ${error.type}');
},
);

Example

You can find the complete example in the example project. See phone number page and otp page for more details.

tip

When testing the example project, you'll need to integrate with your backend using these recommended request/response structures (which you can modify as needed).

tip

Search for keywords TIP if you want to know how to implement OTP Verification.