Create a Custom One-Time Password Delivery Apex Handler

Custom one-time password (OTP) delivery relies on an Apex handler that implements the Auth.CustomOneTimePasswordDeliveryHandler interface. Your custom OTP delivery handler calls out to your provider to send an OTP to the user via SMS. You can customize the content of the SMS message and the short code that the user sees.

Required Editions

Available in: both Salesforce Classic (not available in all orgs) and Lightning Experience
Available in: Professional, Enterprise, Unlimited, and Developer Editions
User Permissions Needed
To define, edit, delete, set security, and set version settings for Apex classes: Author Apex
To run Apex tests: View Setup and Configuration

The custom OTP delivery handler uses the sendOneTimePassword method to send an OTP to the user.

  1. From Setup, click Settings gear icon, and then select Developer Console.
  2. Click File, select New, and then select Apex Class.
  3. Enter a name for your handler, such as MyCustomOtpHandler.
  4. Develop an Apex handler that implements the Auth.CustomOneTimePasswordDeliveryHandler interface and returns an instance of the Auth.CustomOneTimePasswordDeliveryResult enum.
  5. To make sure that the handler can successfully call out to the provider to send an SMS message, test the Apex handler with hard-coded values for the userId, phoneNumber, oneTimePassword, defaultText, networkId, and experienceId. See Testing Apex.
  6. Save the handler.

Next, add the handler to your Experience Cloud site settings.