# Automate the OTP functionality

Humanless automates the OTP step in your own app by letting the test receive the code the way a user does, then type it. For emailed OTPs, it gives each test a disposable inbox and a wait call that returns the parsed code as soon as the email arrives: `POST /v1/identities` for the address, `GET /v1/identities/:id/wait` for the code. For authenticator-app OTPs, add the `totp` channel and read the current code from `GET /v1/identities/:id/totp`. Avoid fixed sleeps and shared inboxes, which are the two usual causes of flaky OTP tests. SMS numbers are not available yet (waitlist only).

```python test_totp.py
# Emailed codes: see /answers/how-to-read-otp-from-email-using-selenium-webdriver
import os, time, requests
from selenium.webdriver.common.by import By

API = "https://api.usehumanless.com/v1"
H = {"Authorization": f"Bearer {os.environ['HUMANLESS_API_KEY']}"}

ident = requests.post(f"{API}/identities", headers=H, json={"channels": ["email", "totp"]}).json()
# Enroll ident["totp_secret"] on the test user (seed script, admin API or an "enter a key" field).
# Today Humanless generates the secret, so your setup has to accept a supplied key.

def current_totp():
    t = requests.get(f"{API}/identities/{ident['identity_id']}/totp", headers=H).json()
    if t["seconds_remaining"] <= 3:  # too close to the 30-second rollover: use the next window
        time.sleep(t["seconds_remaining"] + 1)
        t = requests.get(f"{API}/identities/{ident['identity_id']}/totp", headers=H).json()
    return t["code"]

driver.find_element(By.NAME, "totp").send_keys(current_totp())
requests.delete(f"{API}/identities/{ident['identity_id']}", headers=H)
```

## Other options

- **A test-only OTP**: a fixed code accepted for one seeded test user in non-production environments. Simple, but it skips email delivery, so keep one real test.
- **Read the code from your database or logs** in the test environment.
- **IMAP or Mailpit** to receive the email yourself; **pyotp** or otplib for TOTP codes.

> **Use policy:** Only for apps you own or are authorized to test. See the [Terms](/terms).

More: [Humanless REST API reference](/docs/api) · [All answers](/answers/)

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/automate-the-otp-functionality

---
More: https://usehumanless.com/llms.txt
