AssertInbox

Local SMTP catcher or real test inbox?

There are two common ways to test code that sends email. A local SMTP catcher is a small server on your machine or in a container that accepts any message your app sends and lets the test read it. A real test inbox is a service with real addresses on the internet: your app sends to it as it would to any user, and the test reads the message through an API. Neither is always better. This guide lists the trade-offs without favouring a product, says when to use each, and ends with code that lets one test suite use both by changing an environment variable.

What a local catcher is good at

What a local catcher cannot tell you

What a real test inbox is good at

What a real test inbox costs you

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including id, subject, otp and magic_link, or 408 if nothing arrived. Messages are deleted after 24 hours. The tag is the isolation boundary: every test builds its own tag, so tests never read each other's mail. The rest of this guide uses it for the real-inbox side.

Get an API key

Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so store it in your secret manager or CI secrets.

curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}

Export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.

When to use each

Using both: switch the address source

A simple way to keep one suite for both is a function that returns an address and a way to fetch its message. An environment variable chooses the implementation. Tests call getMessage and never know which one runs. This sketch assumes a catcher that exposes an HTTP API at CATCHER_URL returning a JSON list of messages for a recipient; adapt the two catcher lines to the catcher you use, since APIs differ.

const MODE = process.env.MAIL_MODE || "catcher";
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const CATCHER_URL = process.env.CATCHER_URL || "http://localhost:8025";

let counter = 0;

function newAddress(label) {
  const tag = (label + "-" + Date.now().toString(36) + "-" + ++counter).toLowerCase();
  if (MODE === "inbox") return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
  return { tag, email: tag + "@example.test" };
}

async function fromCatcher(address) {
  for (let i = 0; i < 20; i++) {
    const res = await fetch(CATCHER_URL + "/messages?to=" + encodeURIComponent(address.email));
    const list = await res.json();
    if (list.length) {
      const body = list[0].body || "";
      const otp = (body.match(/\b[0-9]{6}\b/) || [null])[0];
      const link = (body.match(/https?:\/\/[^\s"<]+/) || [null])[0];
      return { subject: list[0].subject, otp, magic_link: link };
    }
    await new Promise((resolve) => setTimeout(resolve, 500));
  }
  throw new Error("No message for " + address.email + " in the catcher");
}

async function fromInbox(address) {
  const params = new URLSearchParams({ tag: address.tag, timeout: "25" });
  for (let attempt = 0; attempt < 3; attempt++) {
    const res = await fetch(API + "/v1/wait?" + params, { headers: { Authorization: "Bearer " + KEY } });
    if (res.status === 200) return res.json();
    if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status + " for tag " + address.tag);
  }
  throw new Error("No email arrived for tag " + address.tag);
}

function getMessage(address) {
  return MODE === "inbox" ? fromInbox(address) : fromCatcher(address);
}

module.exports = { newAddress, getMessage };

In a unit-test job, run with MAIL_MODE=catcher (the default). In an end-to-end or staging job, run with MAIL_MODE=inbox and the two AssertInbox variables set. The catcher path above extracts a six-digit code and the first link with simple patterns; the real inbox returns otp and magic_link already. If your tests rely on edge cases of the extraction, check them in the real-inbox mode only.

A test that works in both modes

const { test } = require("node:test");
const assert = require("node:assert");
const { newAddress, getMessage } = require("./mail-source");

test("sign-up sends a verification code", async () => {
  const address = newAddress("signup");

  const res = await fetch((process.env.APP_URL || "http://localhost:3000") + "/api/signup", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ email: address.email }),
  });
  assert.equal(res.status, 201);

  const mail = await getMessage(address);
  assert.match(mail.otp, /^[0-9]{4,8}$/);
});

A practical split

Keep most tests on the catcher and a handful on the real inbox: the sign-up, the password reset and any flow where a failed email means a lost customer. The real-inbox set runs on every merge to main or before each release, not on every commit, which keeps daily usage low. See Reliable email tests for unique tags, timeouts and cleanup.

Troubleshooting

Next steps

Read Reliable email tests for unique tags, retries and cleanup. Related guides: sign-up email tests in Node, GitHub Actions and password reset emails. The wait filters are described in the docs, and the client files wrap the helper for Node.js and Python.

Free beta, run by AI agents

This service is in free beta and is operated by AI agents under human supervision. Messages are deleted after 24 hours.