2.1.1.1.4 OTP Method
Overview
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
- Initialize VerifySpeed: Follow the initialize instructions.
Verifying Phone Numbers with OTP
Use verifyPhoneNumberWithOtp
to send the OTP to the user's phone number with these parameters:
phoneNumber
: The user's phone number (including country code).verificationKey
: Unique key provided by your backend to ensure verification integrity.
VerifySpeed.verifyPhoneNumberWithOtp(
"PHONE_NUMBER" // e.g., '+1234567890',
"YOUR_VERIFICATON_KEY",
)
This dependency 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 library like libphonenumber
for efficient phone number validation.
Validating OTP
After the user receives the OTP, call validateOTP
with these parameters:
otpCode
: The OTP received from the user.verificationKey
: Unique key provided by your backend to ensure verification integrity.callBackListener
: Callback interface to handle success and failure cases:onSuccess
: Triggered on successful verification, returning atoken
to retrieve the user's phone number.onFail
: Triggered if verification fails, providingerror
details including type and message.
- Kotlin
- Java
VerifySpeed.validateOTP(
"OTP_CODE", // OTP received from the user
"YOUR_VERIFICATON_KEY", // unique key provided by your backend
object : VerifySpeedListener { // callback listener for success and failure cases
override fun onSuccess(token: String) {
Log.d("VerifySpeed", "Token: $token")
}
override fun onFail(error: VerifySpeedError) {
Log.d("VerifySpeed", "Error: ${error.message}")
}
}
)
VerifySpeed.validateOTP(
"OTP_CODE", // OTP received from the user
"YOUR_VERIFICATON_KEY", // unique key provided by your backend
new VerifySpeedListener() { // callback listener for success and failure cases
@Override
public void onSuccess(String token) {
Log.d("VerifySpeed", "Token: " + token);
}
@Override
public void onFail(VerifySpeedError error) {
Log.d("VerifySpeed", "Error: " + error.getMessage());
}
}
);
Example
For complete implementation examples, see:
Jetpack Compose:
Android Views:
When implementing OTP Verification, look for code comments with the keyword TIP
in the example code for guidance on implementation details.
When testing, you'll need to integrate with your backend using these recommended request/response structures (which you can modify as needed).