OTP & verification

OTP Autofill on iOS, Android, and the Web

How to make SMS codes fill themselves in: autocomplete="one-time-code" on iOS, the WebOTP API and origin-bound codes on the web, and the SMS Retriever API on Android.

Updated · 3 min read

Every second a user spends switching to their messages app and copying a code is a chance to lose them. Autofill removes that step: the code appears as a suggestion above the keyboard, or fills itself in. It also improves security, because a correctly bound code only fills on your real domain, not on a phishing page.

The message format that makes it work

Add an origin-bound line as the last line of your SMS:

482615 is your Acme verification code.

@acme.com #482615

The last line has your domain after @ and the code after #. Browsers and operating systems use it to offer the code only on acme.com. Keep the rest of the message short; the line counts towards your character limit.

iOS and Safari

On iOS, the keyboard suggests codes from incoming SMS when the input field is marked correctly:

<input
  autocomplete="one-time-code"
  inputmode="numeric"
  pattern="[0-9]*"
/>
  • autocomplete="one-time-code" tells iOS this field expects a code.
  • inputmode="numeric" shows the number pad.
  • In native apps, set the text field's content type to one-time code.

iOS can detect codes without the origin-bound line, but with it Safari only suggests the code on the matching domain.

The WebOTP API (Chrome and Android browsers)

On Android, Chrome supports the WebOTP API, which can read the code from an SMS with the origin-bound format and fill it after the user taps "Allow":

if ('OTPCredential' in window) {
  const controller = new AbortController()
  navigator.credentials
    .get({ otp: { transport: ['sms'] }, signal: controller.signal })
    .then((otp) => {
      input.value = otp.code
      form.requestSubmit()
    })
    .catch(() => { /* user dismissed or timed out: keep manual entry */ })
}

Always keep manual entry and paste working; autofill is an enhancement, not the only path.

Native Android apps: SMS Retriever API

Native Android apps can use Google's SMS Retriever API, which reads a matching SMS without asking for SMS permissions. The message must end with an 11-character hash that identifies your app, generated from your app's signing key. Your SMS template therefore needs a variant for Android app users that includes the hash.

Design the input for autofill

  • Use one input field (or a single real input behind visual boxes) so the whole code fills at once.
  • Auto-submit when all digits are present.
  • Accept paste of codes with spaces, such as "482 615".
  • Do not block the field while the code is being read.

Check it on real devices

Autofill depends on OS versions, browsers, and message format. Test on a recent iPhone in Safari, an Android phone in Chrome, and your native apps, with your real sender ID and template. If you also send codes by WhatsApp, use the copy-code and one-tap buttons that WhatsApp authentication templates support.

For the rest of the flow, see OTP best practices and designing the OTP resend flow.