# 2FA authentication automation using Playwright with Java

Humanless works from Java over plain HTTP: create an identity with the `email` and `totp` channels, set its `totp_secret` on your test user (a seed script, admin API or an enrollment field that accepts a key), and at the 2FA prompt call `GET /v1/identities/:id/totp`, which returns `{ "code": "159026", "seconds_remaining": 14 }`. For emailed codes, call `GET /v1/identities/:id/wait` instead. Today, Humanless generates the TOTP secret and doesn't import one your app created, so if your app always generates the secret, read it from the enrollment page and compute codes in Java with a TOTP library.

```java TotpStep.java
HttpClient http = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder(URI.create(
        "https://api.usehumanless.com/v1/identities/" + identityId + "/totp"))
    .header("Authorization", "Bearer " + System.getenv("HUMANLESS_API_KEY"))
    .build();
JsonNode totp = new ObjectMapper().readTree(http.send(req, HttpResponse.BodyHandlers.ofString()).body());
if (totp.get("seconds_remaining").asInt() <= 3) { /* wait for the next 30-second window, then call again */ }
page.getByLabel("Authentication code").fill(totp.get("code").asText());
```

## Other options

- **A Java TOTP library**, such as `com.eatthepath:java-otp` or GoogleAuth (`com.warrenstrange:googleauth`), with the secret captured at enrollment and stored as a CI secret.
- **Seed users with 2FA already enabled** and keep one test that enrolls through the UI.
- **Save the logged-in state** with `context.storageState()` so only one test logs in through 2FA.

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

More: [How to test TOTP / authenticator 2FA in E2E tests](/guides/totp-e2e-testing) · [All answers](/answers/)

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/2fa-authentication-automation-using-playwright-with-java

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