AssertInbox

Run email tests in GitLab CI

Email tests are easy to run on a laptop and easy to break in CI: the key leaks into a log, two pipelines read each other's messages, or a parallel job waits for a mail that another job consumed. This guide sets up Playwright Test in GitLab CI with the AssertInbox API key stored as a masked CI/CD variable, a tag that is unique for every pipeline and every parallel job, and a job that fails with a readable message. It uses a complete .gitlab-ci.yml, a helper and a spec, and works the same on GitLab.com and a self-managed instance.

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. Messages are deleted after 24 hours. The tag is the isolation boundary: if every job builds its tags from the pipeline id and its own job index, no two jobs can read the same mail.

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.

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

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.

Store the key as a masked variable

In your project, open Settings, then CI/CD, then Variables. Add a variable named ASSERTINBOX_KEY with the api_key value. Turn on Mask variable so that GitLab hides its value in job logs, and turn on Protect variable if only protected branches and tags should be able to read it. The namespace is not secret, since it is only the first part of an address, so add it as a plain variable named ASSERTINBOX_NAMESPACE. Add APP_URL too if your tests run against a staging URL. The masking rules are in the GitLab documentation: a value must be on one line and use only certain characters, and the key format qualifies.

A protected variable is not available to pipelines for merge requests from unprotected branches. Decide whether those pipelines should skip the email tests; do not work around it by printing the key.

The pipeline

Save this as .gitlab-ci.yml. It uses the Playwright image, so no browser install step is needed; keep the image version in line with the @playwright/test version in your package.json. parallel: 4 starts four jobs, and Playwright's --shard option gives each one a quarter of the tests. RUN_TAG combines the pipeline id with the job index, so a retried pipeline and every parallel job get their own tags.

stages:
  - test

e2e:
  stage: test
  image: mcr.microsoft.com/playwright:v1.49.0-jammy
  timeout: 15 minutes
  parallel: 4
  variables:
    RUN_TAG: "gl-$CI_PIPELINE_ID-$CI_NODE_INDEX"
  script:
    - npm ci
    - npx playwright test --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL
  artifacts:
    when: on_failure
    expire_in: 3 days
    paths:
      - playwright-report/
      - test-results/
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

The job reads ASSERTINBOX_KEY, ASSERTINBOX_NAMESPACE and APP_URL from the project variables. The artifacts contain the Playwright report and traces, which show the page state when a test fails. They do not contain the key, as long as your tests never print it.

Keeping the key out of the logs

The helper and the test

Save the helper as tests/inbox.js. It needs Node 18 or newer for the built-in fetch, which the Playwright image provides. The tag combines RUN_TAG with a counter and a random suffix.

const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;

if (!KEY || !NAMESPACE) {
  throw new Error("Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE");
}

let counter = 0;

function newInbox(label) {
  const base = process.env.RUN_TAG || "local-" + Date.now();
  const tag = (base + "-" + label + "-" + ++counter + "-" + Math.random().toString(36).slice(2, 6)).toLowerCase();
  return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}

async function waitForEmail(tag, filters = {}) {
  const params = new URLSearchParams({ tag, timeout: "25", ...filters });
  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 " + tag);
  }
  throw new Error("No matching email arrived for tag " + tag);
}

async function deleteEmails(tag) {
  await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
    method: "DELETE",
    headers: { Authorization: "Bearer " + KEY },
  });
}

module.exports = { newInbox, waitForEmail, deleteEmails };

Save this as tests/signup.spec.js. Replace the page, labels and subject with those of your app.

const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");

const APP = process.env.APP_URL || "http://localhost:3000";

test("sign-up with an emailed code", async ({ page }) => {
  const inbox = newInbox("signup");
  try {
    await page.goto(APP + "/signup");
    await page.fill("input[name=email]", inbox.email);
    await page.click("button[type=submit]");

    const mail = await waitForEmail(inbox.tag, { subject: "verify" });
    expect(mail.otp).toMatch(/^[0-9]{4,8}$/);

    await page.fill("input[name=code]", mail.otp);
    await page.click("button[type=submit]");
    await expect(page.getByText("Email verified")).toBeVisible();
  } finally {
    await deleteEmails(inbox.tag);
  }
});

Parallel jobs and the daily allowance

Each key has 2,000 API requests per UTC day, and a wait counts as one request however long it polls. Four parallel jobs that each run a few dozen email tests stay far below that, but a pipeline that runs on every push of a busy repository can add up. If you want to separate pipelines from local runs, create a second key for CI. If a job fails with HTTP 429, the Retry-After header gives the seconds until 00:00 UTC.

Troubleshooting

Next steps

Read Reliable email tests for unique tags, retries and cleanup. Related guides: email tests in GitHub Actions and Playwright email verification. 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.