Guide

Extract OTP Codes from Email

Stop switching to your inbox every time a 2FA code arrives. nylas otp get scans recent emails for verification codes and copies the result to your clipboard in under a second. Works with Gmail, Outlook, Exchange, Yahoo, iCloud, and IMAP — no browser required.

By Nick Barraclough

The problem with OTP codes

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, then switch back. That's 30–60 seconds of context switching per authentication event.

For developers running integration tests or end-to-end 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.

According to the FIDO Alliance 2024 Online Authentication Barometer, 58% of users abandon signups when verification is slow or inconvenient. Brittle OTP extraction code is one of the top causes of flaky E2E tests.

The Nylas CLI solves both problems with one command: nylas otp get.

1. Install

Pick the install method for your platform:

# macOS / Linux (Homebrew)
brew install nylas/nylas-cli/nylas

# macOS / Linux / WSL (shell script)
curl -fsSL https://cli.nylas.com/install.sh | bash

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

Verify the install:

nylas --version

2. Authenticate

Connect your email account using an API key from the Nylas Dashboard:

nylas auth config --api-key YOUR_API_KEY

This stores credentials locally. The CLI works with Gmail, Outlook, Exchange (EWS), Yahoo Mail, iCloud Mail, and any IMAP server — you don't need to configure each provider separately.

3. Get the latest OTP code

Pull the most recent OTP from your inbox:

nylas otp get
✓ OTP found

  Code:    847291
  From:    noreply@github.com
  Subject: Your GitHub verification code
  Copied to clipboard

The command scans your recent emails, finds the verification code, and copies it to your clipboard automatically.

For scripting — where you don't want clipboard side effects — use --raw:

nylas otp get --raw
847291

To skip the clipboard copy in interactive sessions:

nylas otp get --no-copy

4. Watch for incoming OTP codes

When you need to wait for a code that hasn't arrived yet, use nylas otp watch. It polls your inbox and prints each new OTP as it arrives:

nylas otp watch
Watching for OTP codes... (Ctrl+C to stop)

  [14:23:05] 847291  — noreply@github.com
  [14:31:18] 193047  — security@google.com

Control the polling interval (in seconds):

nylas otp watch --interval 5

To watch without copying codes to the clipboard:

nylas otp watch --no-copy --interval 10

Press Ctrl+C to stop watching.

5. List configured accounts

If you have multiple email accounts connected, list them:

nylas otp list
  ACCOUNT                        PROVIDER
  user@gmail.com                 Gmail
  work@company.com               Outlook
  personal@icloud.com            iCloud

Target a specific account when fetching:

nylas otp get user@gmail.com

6. Use OTP codes in scripts

The --raw flag makes OTP extraction composable. Here's a complete bash flow that triggers a password reset, waits for the code, and submits it:

#!/usr/bin/env bash
set -euo pipefail

EMAIL="user@example.com"
API_BASE="https://app.example.com/api"

# Trigger the OTP email
curl -s -X POST "$API_BASE/auth/send-otp" \
  -H "Content-Type: application/json" \
  -d "{"email": "$EMAIL"}"

# Wait for delivery, then grab the code
sleep 3
CODE=$(nylas otp get --raw)

echo "Got code: $CODE"

# Submit the code
curl -s -X POST "$API_BASE/auth/verify-otp" \
  -H "Content-Type: application/json" \
  -d "{"email": "$EMAIL", "code": "$CODE"}"

echo "Verified."

In CI/CD, store your API key as a secret and set it before running:

# GitHub Actions example
- name: Authenticate Nylas CLI
  run: nylas auth config --api-key ${{ secrets.NYLAS_API_KEY }}

- name: Run OTP flow test
  run: ./scripts/test-otp-flow.sh

The CLI reads NYLAS_API_KEY from the environment, so you can skip the nylas auth config step entirely in ephemeral CI environments:

# Set env var — CLI picks it up automatically
export NYLAS_API_KEY=your_key
CODE=$(nylas otp get --raw)

Next steps