AssertInbox

Test that a magic link works once and expires

A magic link is a password you can click, so the rules around it matter: it should work once, it should stop working when it is replaced, and opening it a second time must never sign anyone in. Those rules are easy to get wrong and easy to test once you can read the email in code. This guide tests them in Playwright Test: follow the link and assert success, open the same link in a fresh browser context and assert rejection, and request a second email and assert that the first link is dead. The examples run unchanged against the practice app on this site, whose confirmation link is single use. It targets recent versions of @playwright/test and Node 18 or newer.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls and returns the newest matching message as JSON, including id, subject, otp and magic_link, or 408 if nothing arrived. The magic_link field holds the main call-to-action link found in the email, so the test does not parse HTML. Messages are deleted after 24 hours. When two emails arrive at one address, wait({ after: first.id }) returns only messages newer than the first.

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 keep 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.

Set up the fixture

Download assertinbox.mjs and playwright-fixture.mjs from the client files page into your tests folder. The fixture adds an inbox object to every test. Its tag is unique for each test, worker and retry, it waits with the 408 retry already built in, and it deletes the test's messages when the test ends.

curl -O https://assertinbox.com/sdk/assertinbox.mjs
curl -O https://assertinbox.com/sdk/playwright-fixture.mjs
npm install --save-dev @playwright/test

Test that a link works once

The practice app is a fake product. After sign-up it emails a code and a confirmation link, and its rules are fixed: the link works once, and a new email replaces the old link. Opening the link lands on a welcome page that says whether the link was accepted. The practice app has no login session, so the welcome page is the success signal here. In your own app, assert whatever proves the user is signed in, such as a menu with the account name. Save this as magic-link.spec.mjs and run npx playwright test magic-link.spec.mjs.

import { test, expect } from "./playwright-fixture.mjs";

const APP = process.env.APP_URL || "https://assertinbox.com";

async function signUp(page, email) {
  await page.goto(APP + "/practice/signup");
  await page.getByLabel("Email address").fill(email);
  await page.getByRole("button", { name: "Create account" }).click();
  await expect(page).toHaveURL(/\/practice\/verify/);
}

test("the link signs the user in the first time", async ({ page, inbox }) => {
  await signUp(page, inbox.address("once"));
  const mail = await inbox.wait();
  expect(mail.magic_link).toContain("/practice/api/confirm");

  await page.goto(mail.magic_link);
  await expect(page.getByRole("status")).toContainText("Your email is confirmed");
});

test("the same link is rejected the second time", async ({ page, browser, inbox }) => {
  await signUp(page, inbox.address("twice"));
  const mail = await inbox.wait();

  await page.goto(mail.magic_link);
  await expect(page.getByRole("status")).toContainText("Your email is confirmed");

  // A new context has no cookies or storage, so it is a different browser.
  const other = await browser.newContext();
  const second = await other.newPage();
  await second.goto(mail.magic_link);
  await expect(second.getByRole("status")).toContainText("invalid or already used");
  await other.close();
});

The second test opens the link in a fresh context on purpose. If the app only remembers "used" in a cookie, the same browser would be refused but another browser would get in, and a test in the first context would never notice. A new context proves the server is what refuses the link. Note too that the test asserts the rejection message and not only a missing success message: a link that fails with a server error would otherwise pass as "rejected".

Test that a newer link replaces the older one

Many apps promise that asking for a new link cancels the old one. The practice app does, and it allows a new email every 30 seconds. The test reads the first email, asks for a second, waits for it with after, and opens the older link first. If your app lets several links live at once, change the expectation and say so in the test name.

test("a newer link replaces the older one", async ({ page, inbox }) => {
  test.setTimeout(120_000);
  await signUp(page, inbox.address("replace"));
  const first = await inbox.wait();

  // Click until the app accepts the request instead of sleeping for a guessed time.
  await expect(async () => {
    await page.getByRole("button", { name: "Send a new code" }).click();
    await expect(page.getByRole("status")).toContainText("A new code was sent", { timeout: 3000 });
  }).toPass({ timeout: 60_000, intervals: [5000] });

  const second = await inbox.wait({ after: first.id });
  expect(second.magic_link).not.toBe(first.magic_link);

  await page.goto(first.magic_link);
  await expect(page.getByRole("status")).toContainText("invalid or already used");

  await page.goto(second.magic_link);
  await expect(page.getByRole("status")).toContainText("Your email is confirmed");
});

The order matters. The old link is opened first, so the test proves it is dead before it uses the new one. Opening the new link first would not prove anything about the old one if the app also invalidated links after a successful sign-in.

Test expiry and your own app

Expiry works as it does for codes: you need a test clock endpoint or a short lifetime setting in the test environment, because Playwright's own clock changes only the browser, not your server. The guide on OTP resend and expiry shows both approaches, and the same hooks apply to links. For your own application, replace the sign-up steps with yours and assert your signed-in state. If your link opens in an app on a phone, use the deep-link guides instead of a browser page.

One practical warning: some mail systems and security scanners open links automatically before the user does. A single-use link can be consumed by such a scanner. Your test mailbox does not open links, so a green test does not rule this out. If real users report that a fresh link is already used, look at link prefetching and consider a confirmation page that needs one click before the link is spent.

Run it in CI

Store ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE as secrets in your CI provider and pass them to the step that runs npx playwright test. Never print the key. The replacement test takes about 30 seconds because of the practice app's cooldown, so run it in its own project or tag it, and keep the single-use tests in the default run.

Troubleshooting

Next steps

For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.