> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Test emails with Resend

> Test the emails your app sends and receives with the Resend SDK.

You can use the [Resend SDK](https://resend.com/docs/send-with-nodejs) in your tests
to read the emails your app sends through [Resend](https://resend.com).

In this guide, we create a unique test email address and check for the email using the Resend SDK.

## Connect your account

You need a full-access API key to read emails with the Resend SDK. So before running the tests,
create a full-access [Resend API key](https://resend.com/api-keys) and set it as `RESEND_API_KEY` environment variable.

## Installation

To install the SDK, run:

<CodeGroup>
```bash npm
npm install --save-dev resend
```

```bash pnpm
pnpm add -D resend
```

```bash bun
bun add -d resend
```

</CodeGroup>

## Configure e2e

Next, we register the API key with `registerProviderKey` so e2e redacts it
from reports, traces, and model input. Then pass it to your app with `inheritEnv`:

```ts title="e2e.config.ts"

registerProviderKey('resend', process.env.RESEND_API_KEY);

export default defineConfig({
  projectId: 'dev.example.signup',
  targets: [
    {
      name: 'web',
      engine: web(),
      app: {
        url: 'http://127.0.0.1:0',
        command: {
          executable: 'node',
          args: ['app/server.mjs'],
          inheritEnv: ['RESEND_API_KEY'],
          env: { PORT: '{port}' },
        },
      },
    },
  ],
});
```

## Read a sent email

Now we can write a test that signs up with a unique
[Resend test address](https://resend.com/docs/dashboard/emails/send-test-emails)
like `delivered+<id>@resend.dev`. Our app still sends from its
[verified domain](https://resend.com/docs/dashboard/domains/introduction),
but the email doesn't reach a real inbox. Since delivery takes a moment, we
wait for the email in `toPass()` and then read it with `emails.get()`:

```ts title="tests/signup.e2e.ts"

const resend = new Resend(process.env.RESEND_API_KEY);

test('signing up sends a welcome email', async ({ app, screen }) => {
  const address = `delivered+${randomUUID()}@resend.dev`;

  await app.open('/');
  await screen.getByRole('textbox', { name: 'Email' }).fill(address);
  await screen.getByRole('button', { name: 'Sign up' }).click();

  let id = '';

  await expect(async () => {
    const { data, error } = await resend.emails.list({ limit: 100 });

    expect(error).toBeNull();

    const sent = data?.data.find((email) => email.to.includes(address));

    expect(sent?.last_event).toBe('delivered');
    id = sent?.id ?? '';
  }).toPass();

  const { data: email, error } = await resend.emails.get(id);

  expect(error).toBeNull();
  expect(email?.subject).toMatch(/welcome/i);
  expect(email?.html).toContain('Confirm your email');
});
```

Here we check `error` after each Resend call because we get `{ data, error }`
back instead of a thrown error.

To test how our app handles a bounce or a spam report, we can sign up with
`bounced+<id>@resend.dev` or `complained+<id>@resend.dev` instead. Then we
expect `last_event` to be `'bounced'` or `'complained'` instead of
`'delivered'`.

<Note>
  Before we verify a domain, our app can still send to these test addresses from
  `onboarding@resend.dev`.
</Note>

## Receive real email

If we want to test an email as a recipient sees it, we can send it to an
address on our team's `<id>.resend.app` domain or another domain that
[receives email through Resend](https://resend.com/docs/dashboard/receiving/introduction).
Our app needs to send from its verified domain here, since
`onboarding@resend.dev` only sends to the test addresses. Then we wait for the
email to arrive and read it with `emails.receiving`:

```ts
const address = `${randomUUID()}@<id>.resend.app`;

// Sign up with the address, then:

let id = '';

await expect(async () => {
  const { data, error } = await resend.emails.receiving.list({ to: [address] });

  expect(error).toBeNull();
  expect(data?.data).toHaveLength(1);
  id = data?.data[0]?.id ?? '';
}).toPass();

const { data: email, error } = await resend.emails.receiving.get(id);

expect(error).toBeNull();
expect(email?.subject).toMatch(/welcome/i);
expect(email?.html).toContain('Confirm your email');
```

## Limits

- We can only get 100 emails per page from `emails.list`, and we can't filter
  by recipient. We should run our tests against a dedicated Resend team so
  other emails don't push our test email off the first page.
- All API keys on our team share Resend's
  [rate limit](https://resend.com/docs/api-reference/rate-limit), including
  the key our app uses. While a test waits for an email, `toPass()` checks
  about four times in the first second and then once a second, so our app can
  hit `429` when sending if we run many email tests in parallel. If that
  happens, we can lower `workers` or run fewer email tests.
- Both test emails and received emails count toward our Resend quota.

## Next

<CardGroup cols={2}>
  <Card title="Retrying a block" href="/reference/expect#retrying-a-block">
    How `toPass()` retries assertions until they pass or the test times out.
  </Card>
  <Card title="Config reference" href="/reference/config#command">
    How to pass environment variables to your app with `inheritEnv` and `env` on
    `app.command`.
  </Card>
</CardGroup>
