# How to communicate OTP from email to IntelliJ using Cucumber

With Humanless, the Cucumber step that needs the OTP fetches it from an email API at runtime, so nothing goes through IntelliJ and the scenario runs the same in the IDE and in CI. A `Given` step creates a disposable inbox over REST and stores its id in the scenario context, a `When` step submits the address, and the step that needs the code calls `GET /v1/identities/:id/wait`, which returns the parsed OTP as soon as the email arrives. Put the API key in an environment variable (`HUMANLESS_API_KEY`) in your IntelliJ run configuration and as a CI secret.

```java OtpSteps.java
// World is your per-scenario context (cucumber-picocontainer injects one instance per scenario)
public class OtpSteps {
  private static final String API = "https://api.usehumanless.com/v1/identities";
  private static final String AUTH = "Bearer " + System.getenv("HUMANLESS_API_KEY");
  private final HttpClient http = HttpClient.newHttpClient();
  private final ObjectMapper json = new ObjectMapper();
  private final World world;

  public OtpSteps(World world) { this.world = world; }

  @Given("a new user with a disposable inbox")
  public void newInbox() throws Exception {
    JsonNode inbox = json.readTree(send(HttpRequest.newBuilder(URI.create(API))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString("{\"channels\":[\"email\"]}"))).body());
    world.identityId = inbox.get("identity_id").asText();
    world.email = inbox.get("email").asText();
  }

  @When("they request a login code")
  public void requestCode() {
    world.driver.findElement(By.name("email")).sendKeys(world.email);
    world.since = System.currentTimeMillis();
    world.driver.findElement(By.id("send-code")).click();
  }

  @Then("they can sign in with the emailed code")
  public void enterCode() throws Exception {
    URI wait = URI.create(API + "/" + world.identityId + "/wait?channel=email&timeout=30&since=" + world.since);
    long deadline = System.currentTimeMillis() + 120_000;
    HttpResponse<String> res;
    do { res = send(HttpRequest.newBuilder(wait)); }   // 408: nothing arrived yet, wait again
    while (res.statusCode() == 408 && System.currentTimeMillis() < deadline);
    assertEquals(200, res.statusCode(), res.body());
    world.driver.findElement(By.name("otp")).sendKeys(json.readTree(res.body()).get("code").asText());
  }

  @After
  public void deleteInbox() throws Exception {
    if (world.identityId != null) send(HttpRequest.newBuilder(URI.create(API + "/" + world.identityId)).DELETE());
  }

  private HttpResponse<String> send(HttpRequest.Builder req) throws Exception {
    return http.send(req.header("Authorization", AUTH).build(), HttpResponse.BodyHandlers.ofString());
  }
}
```

## Other options

- **JavaMail / Jakarta Mail** reading a test mailbox over IMAP inside the step definition.
- **A test hook in your app** that exposes the last OTP for a test user, enabled only in test environments.
- **Mailpit** for local runs, called with the same `HttpClient`.

> **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/how-to-communicate-otp-from-email-to-intellij-using-cucumber

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