Sending OTP emails from Node.js: a checklist
18 November 2026 · 7 min read
One-time passcodes by email look simple: generate six digits, send them, check them. Most of the problems show up later: codes that arrive after a minute, users who get two codes, codes guessed by a script, emails in spam. This checklist covers both the security side and the delivery side, with Node.js examples.
1. Generate codes securely
Use a cryptographically secure generator, not Math.random():
import { randomInt } from "node:crypto";
const code = String(randomInt(0, 1_000_000)).padStart(6, "0");2. Store a hash, not the code
Treat the code like a short-lived password. Store a hash with an expiry, and compare in constant time:
import { createHash, timingSafeEqual } from "node:crypto";
const hash = (c) => createHash("sha256").update(`${userId}:${c}`).digest();
// save { userId, hash: hash(code), expiresAt: Date.now() + 10 * 60_000, attempts: 0 }
const ok = timingSafeEqual(saved.hash, hash(input)) && Date.now() < saved.expiresAt;3. Limit attempts and expiry
- Expire codes after 5 to 10 minutes.
- Allow 5 wrong attempts, then require a new code.
- Rate-limit sending: for example, one code per address per minute and a few per hour, so nobody can use your form to flood someone's inbox.
- Invalidate the code once it's used.
4. Send it with priority, and only once
The user is staring at the screen, so the email has to leave immediately and never twice. With TrivSend's Node.js SDK:
import { TrivSend } from "trivsend";
const trivsend = new TrivSend(process.env.TRIVSEND_API_KEY);
await trivsend.emails.send(
{
from: "Acme <noreply@notify.acme.com>",
to: user.email,
subject: `Your Acme code: ${code}`,
text: `Your sign-in code is ${code}. It expires in 10 minutes. If you didn't ask for it, ignore this email.`,
priority: "high",
},
{ idempotencyKey: `otp-${requestId}` },
);priority: "high"puts it ahead of newsletters and bulk sends.- The idempotency key means a retried request (a timeout, a double click) returns the first email instead of sending a second code.
5. Write it for the inbox
- Put the code in the subject ("Your Acme code: 482913"), so it's visible in notifications without opening the email.
- Send plain text, or very simple HTML. No images, no tracking pixels, no link shorteners.
- Don't include a login link in a code email unless that's the design. A code plus a link looks like phishing.
- Say what to do if they didn't ask for it.
6. Send from an authenticated domain
Use a subdomain such as notify.yoursite.com with SPF, DKIM and DMARC set up (how they work). Without them, Gmail and Outlook are much more likely to filter your codes.
7. Know when it didn't arrive
When a user says "I never got the code", you need to know whether the email was delivered, bounced or is still being retried. TrivSend shows this for every email, and a webhook can tell your app about bounces so you can suggest checking the address.
8. Test without sending
In CI, use test API keys and simulator addresses, so sign-up tests don't send real mail or count against your limits (testing).
Checklist
- Secure random codes, hashed with an expiry
- Attempt limits and send rate limits
- High priority and an idempotency key
- Code in the subject, plain content
- Authenticated sending domain
- Delivery status visible to support
Send OTP emails with TrivSend, free for 3,000 emails a month.