Utilize our Headless SDK for ultimate flexibility. This guide provides detailed instructions on integrating custom UI elements with OTPLESS’s backend authentication functions.
This is the new Headless authentication SDK that is significantly faster and more robust than the previous version. This upgrade enhances performance, reliability, and security, ensuring a seamless authentication experience, along with a seamless integration process. We strongly recommend migrating to the new SDK for improved efficiency and better support. To migrate from the old SDK, remove the previous SDK dependency and integration and follow the below mentioned steps.
Conform to OtplessResponseDelegate in your signup/sign in file to receive callbacks from OtplessBM.
func onResponse(_ response: OtplessBM.OtplessResponse) { Otpless.shared.commitOtplessResponse(response) switch response.responseType { case .SDK_READY: // Notify that SDK has been initialized successfully print("SDK has been initialized successfully, you may enable your continue button or proceed with user authentication.") case .FAILED: // Notify that the initialization has failed if response.statusCode == 5003 { print("SDK initialization failed, please try to initialize the SDK again") } case .INITIATE: // Notify that authentication has been initiated if response.statusCode == 200 { print("Authentication initiated") let authType = response.response?["authType"] as? String // This is the authentication type if authType == "OTP" { // Take user to OTP verification screen } else if authType == "SILENT_AUTH" { // Handle Silent Authentication initiation by showing loading status for SNA flow. } } else { handleInitiateError(response) } case .VERIFY: // Notify that verification has failed if response.response?["authType"] as? String == "SILENT_AUTH" { if response.statusCode == 9106 { // Silent Authentication and all fallback authentication methods in SmartAuth have failed. // The transaction cannot proceed further. // Handle the scenario to gracefully exit the authentication flow } else { // Silent Authentication failed. If SmartAuth is enabled, // the INITIATE response will include the next available authentication // method configured in the dashboard. } } else if response.response?["authType"] as? String == "OTP" { handleVerifyError(response) } case .FALLBACK_TRIGGERED: // A fallback occurs when an OTP delivery attempt on one channel fails, // and the system automatically retries via the subsequent channel selected on Otpless Dashboard. // For example, if a merchant opts for SmartAuth with primary channal as WhatsApp and secondary channel as SMS, // in that case, if OTP delivery on WhatsApp fails, the system will automatically retry via SMS. // The response will contain the deliveryChannel to which the OTP has been sent. let newDeliveryChannel = response.response?["deliveryChannel"] as? String // This is the deliveryChannel to which the OTP has been sent print("Fallback authentication triggered") case .DELIVERY_STATUS: // This function is called when delivery is successful for your authType. let authType = response.response?["authType"] as? String // It is the authentication type (OTP, MAGICLINK, OTP_LINK) for which the delivery status is being sent let deliveryChannel = response.response?["deliveryChannel"] as? String // It is the delivery channel (SMS, WHATSAPP, etc) on which the authType has been delivered case .ONETAP: // Final response with token if response.statusCode == 200 { if let data = response.response?["data"] as? [String: Any], let token = data["token"] as? String, !token.isEmpty { // Process token and proceed to verify the token on your backend. print("Received token: \(token)") } else { print("Token not received") } } else { print("Token verification failed") } }}
Initiate the authentication process based on the user’s selected method by using the initiate method of the SDK.
Phone Authentication 📱
Phone authentication allows users to verify their identity using their phone number. Merchants can choose from various authentication methods:
Silent Authentication (SNA) – Automatically verifies the user without requiring OTP or MAGICLINK.
OTP on Desired Channel – Sends a one-time password (OTP) via SMS, WhatsApp, or another preferred channel.
Magic Link – Sends a link that users can click to authenticate.
SNA + OTP – Uses silent authentication first and falls back to OTP if needed.
OTP + Magic Link – Sends both an OTP and a magic link, allowing users to authenticate via either method.
To verify the OTP entered by the user, use the verify method with the necessary parameters. Verifying OTP is required only in case of OTP authentication. No need to verify OTP in case of MAGICLINK.
Phone Authentication 📱
Phone authentication allows users to verify their identity using their phone number. Merchants can choose from various authentication methods:
Silent Authentication (SNA) – Automatically verifies the user without requiring OTP or MAGICLINK.
OTP on Desired Channel – Sends a one-time password (OTP) via SMS, WhatsApp, or another preferred channel.
Magic Link – Sends a link that users can click to authenticate.
SNA + OTP – Uses silent authentication first and falls back to OTP if needed.
OTP + Magic Link – Sends both an OTP and a magic link, allowing users to authenticate via either method.
To verify the OTP entered by the user, use the verify method with the necessary parameters. Verifying OTP is required only in case of OTP authentication. No need to verify OTP in case of MAGICLINK.
To verify the OTP entered by the user, use the verify method with the necessary parameters. Verifying OTP is required only in case of OTP authentication. No need to verify OTP in case of MAGICLINK.
Replace YOUR_APP_ID with your actual App
ID provided in
your OTPLESS dashboard.
OAuth Authentication 🔑
OAuth allows users to authenticate using third-party services like Google, GitHub, or WhatsApp. Instead of entering credentials manually, users can log in using their existing accounts, streamlining the authentication process.