# Extract OTP Codes from Email — Skip the Inbox

Every OTP interrupts your flow. You get a login prompt, switch to your email client, wait for the message, find the 6-digit code buried in a template, type it in, switch back. That's 30-60 seconds of context switching per authentication event.

For developers running integration tests or E2E flows, it's worse. You need the code programmatically, which means writing custom email polling logic for each provider — Gmail API, Microsoft Graph, IMAP idle — before you can even get to the actual test.

[Nylas CLI](https://cli.nylas.com) solves both problems with one command: `nylas otp get`.

## Install

```bash
# macOS / Linux
brew install nylas/nylas-cli/nylas

# Windows PowerShell
irm https://cli.nylas.com/install.ps1 | iex
```

New to the CLI? The [getting started guide](https://cli.nylas.com/guides/getting-started) covers setup in under a minute.

## Authenticate

```bash
nylas auth login
nylas auth whoami
# => Authenticated as you@company.com (Google Workspace)
```

Works with Gmail, Outlook, Exchange, Yahoo, iCloud, and any IMAP server. No provider-specific config needed.

## Extract the latest OTP

```bash
nylas otp get
# => ✓ Found code: 847291 (from noreply@github.com, 23s ago)
# => Copied to clipboard
```

That's it. The CLI scans your recent emails for verification codes, extracts the digits, and copies to clipboard.

## Scripting and CI/CD

Use `--raw` to output just the code — no formatting, no clipboard:

```bash
CODE=$(nylas otp get --raw)
echo "OTP is: $CODE"
```

This makes it easy to pipe into [Playwright tests](https://cli.nylas.com/guides/e2e-email-testing), Cypress, or any CI/CD pipeline:

```bash
# In a GitHub Actions workflow
- name: Get verification code
  run: |
    CODE=$(nylas otp get --raw --timeout 30)
    echo "OTP=$CODE" >> $GITHUB_ENV
```

For PowerShell environments, see the [CI/CD email testing guide](https://cli.nylas.com/guides/powershell-email-cicd) which covers GitHub Actions and Azure DevOps with Pester assertions.

## Watch mode — poll for incoming codes

When you trigger a signup or password reset and need to wait for the code to arrive:

```bash
nylas otp watch --interval 5
# Polls every 5 seconds, prints each new code as it arrives
```

This is useful for automated [E2E email testing](https://cli.nylas.com/guides/e2e-email-testing) where you trigger a password reset flow in Playwright, then wait for the OTP to land.

## Filter by sender

If you're getting codes from multiple services simultaneously:

```bash
nylas otp get --from "noreply@github.com"
nylas otp get --from "verify@stripe.com"
```

## How it works under the hood

The CLI fetches your last 10 unread emails via the Nylas API, scans subject lines and bodies for common OTP patterns (6-digit codes, "verification code", "security code", etc.), and returns the most recent match. No regex to maintain, no IMAP polling logic to write.

For a deeper look at how email listing works, see:

*   [List Gmail Emails from the Command Line](https://cli.nylas.com/guides/list-gmail-emails)
    
*   [List Outlook Emails from Terminal](https://cli.nylas.com/guides/list-outlook-emails)
    
*   [Read and Search Email in PowerShell](https://cli.nylas.com/guides/read-email-powershell)
    

## Use case: AI agent account signups

AI coding agents like Claude Code and Cursor often need to sign up for services on your behalf. They hit the email verification step and get stuck. With [MCP email access](https://cli.nylas.com/guides/give-ai-agent-email-address), your agent can extract OTPs directly:

```bash
nylas mcp install --assistant claude-code
```

Now Claude Code can call `nylas otp get --raw` as a tool to complete signups autonomously. See the full [AI agent email setup guide](https://cli.nylas.com/guides/ai-agent-email-mcp) for MCP configuration.

For more on why AI agents need email access: [Why AI Agents Need Email](https://cli.nylas.com/guides/why-ai-agents-need-email)

## Compared to rolling your own

| Feature | Gmail API + regex | IMAP polling | Nylas CLI |
| --- | --- | --- | --- |
| Setup time | Hours (OAuth, scopes, parsing) | Hours (IMAP config, TLS) | 1 minute |
| Multi-provider | No (Gmail only) | Partial | Yes (6 providers) |
| OTP extraction | Manual regex | Manual regex | Built-in |
| Clipboard copy | Manual | Manual | Automatic |
| CI/CD friendly | Complex | Complex | `--raw` flag |

For a full comparison of CLI email tools, see [Best CLI Email Tools Compared](https://cli.nylas.com/guides/best-cli-email-tools-compared).

* * *

Full guide with watch mode, filtering, and automation examples: [Extract OTP Codes from Email](https://cli.nylas.com/guides/extract-otp-codes-from-email)

Related guides:

*   [E2E Email Testing with Playwright](https://cli.nylas.com/guides/e2e-email-testing)
    
*   [Send Email from the Command Line](https://cli.nylas.com/guides/send-email-from-terminal)
    
*   [Give Your AI Agent an Email Address](https://cli.nylas.com/guides/give-ai-agent-email-address)
    
*   [Build an AI Email Triage Agent](https://cli.nylas.com/guides/build-ai-email-triage-agent)
    
*   [Monitor Your Inbox with PowerShell](https://cli.nylas.com/guides/powershell-email-monitoring)
    
*   [Secure Email Handling for CLI](https://cli.nylas.com/guides/secure-email-handling-cli)
    
*   [Manage Calendar from the Terminal](https://cli.nylas.com/guides/manage-calendar-from-terminal)
    

All guides: [cli.nylas.com/guides](https://cli.nylas.com/guides)
