Skip to content
muchobienPublic

About

Receive one-time login codes on disposable addresses and read them from the command line. Self-hosted on Cloudflare.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

otp

Receive one-time login codes on disposable email addresses and read them from the command line. Built for automated tests and coding agents that need to sign in with an emailed code.

addr=$(otp new)                 # otp+k3f9x2m7qa@agents.example.com
# …sign up or log in with $addr…
code=$(otp wait "$addr")        # 4829

otp runs as one Cloudflare Worker in your own account. Cloudflare Email Routing hands incoming mail to the Worker, which keeps the recipient, sender, subject and the code it found in D1 for 24 hours. The CLI talks to the Worker over HTTPS with a bearer token. Nothing is sent: otp only receives mail.

Use the CLI

Install a release binary with mise:

mise use --global github:muchobien/otp

Releases ship standalone binaries for macOS and Linux on arm64 and x64. Or install from source with Node.js 24 or newer:

npm install --global github:muchobien/otp

Then save your Worker's URL and its OTP_TOKEN secret in ~/.config/otp/config.json (or $XDG_CONFIG_HOME/otp/config.json), readable only by you:

{ "url": "https://otp.YOUR_SUBDOMAIN.workers.dev", "token": "…" }

The OTP_URL and OTP_TOKEN environment variables override the file.

Command Prints Exit code
otp new A fresh address on your domain 0
otp wait <address> The code from the newest email to that address 0, or 1 on timeout or when the email has no code
otp wait <address> --json {to, from, subject, code, receivedAt} Same as above

otp wait polls every 2 seconds for up to --timeout seconds (default 60). It returns the newest email to the address, including one that arrived before the command started. When a flow sends a second code to the same address, pass --since with the time you requested it (epoch milliseconds or ISO 8601) to ignore older emails. otp new --json includes createdAt for this.

The code is the first standalone run of 4 to 8 digits in the subject, or else in the text body. Use --json to see the subject when a code is missing or wrong.

Deploy your own

You need a domain on Cloudflare. Email Routing only allows a catch-all rule on an apex domain, so otp uses one address on a subdomain plus subaddressing: every otp+<id>@agents.example.com reaches the Worker, and the rest of your domain's mail is untouched.

  1. In the Cloudflare dashboard, go to Email Service > Email Routing for your domain. Under Settings, add the subdomain (for example agents.example.com) and turn on Subaddressing. Cloudflare adds the subdomain's MX and SPF records.

  2. Create the deployment config. wrangler.deploy.jsonc is ignored by Git:

    pnpm install --frozen-lockfile
    cp wrangler.jsonc wrangler.deploy.jsonc

    In it, set account_id, set vars.ADDRESS to otp+{id}@agents.example.com, and add "addresses": ["otp@agents.example.com"] so wrangler deploy creates the routing rule.

  3. Create the database, deploy and set the token:

    pnpm exec wrangler d1 create otp --binding DB --update-config --config wrangler.deploy.jsonc
    pnpm db:migrate:remote
    pnpm run deploy
    openssl rand -base64 32 | tr -d '\n' >| otp-token
    pnpm exec wrangler secret put OTP_TOKEN --config wrangler.deploy.jsonc < otp-token

    Keep the token somewhere safe and delete otp-token.

  4. Check it: curl https://otp.YOUR_SUBDOMAIN.workers.dev/api/health returns {"ok":true,"name":"otp"}. Then run otp new, send an email with a code to that address and run otp wait.

Anyone can send mail to the subdomain, and the Worker does not check senders. Addresses are random, so a code only reaches the test that created the address. Don't use otp for accounts that matter.

API

All routes except /api/health need Authorization: Bearer <OTP_TOKEN>. Every response allows any origin (CORS), so browser extensions can call the API; the token is still required.

Route Response
GET /api/health {"ok": true, "name": "otp"}
POST /api/addresses {"address", "createdAt"}
GET /api/messages/latest?to=<address>&since=<epoch ms> {"message": {to, from, subject, code, receivedAt}}, or {"message": null}

Develop

pnpm install --frozen-lockfile
cp .dev.vars.example .dev.vars
pnpm typegen
pnpm db:migrate
pnpm dev

wrangler dev accepts test emails at POST /cdn-cgi/handler/email?from=<sender>&to=<recipient> with a raw MIME message as the body. Point the CLI at it with OTP_URL=http://localhost:8787.

Run pnpm check, pnpm typecheck and git diff --check before committing.

License

MIT · Made by mucho bien.

About

Receive one-time login codes on disposable addresses and read them from the command line. Self-hosted on Cloudflare.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages