---
title: Email
description: Send an occasional request or notice by email from an application's backend, see what the send accepted, and test the sends with a test double.
slug: /backend-packages/email
order: 11
visibility: public
redirects_from: []
---

# Email

Email lets your application's backend send an occasional request or notice, such as an order confirmation, a receipt, or an invitation, to up to five people at a time. Turn Zero Cloud sends each message from its own sending address under your application's name. Every application can declare email send.

The package provides your backend's side of that service. Its client sends one message under your application's [platform credential](/cloud/concepts/glossary/#platform-credential). It returns a message ID and the number of recipients the platform accepted. It throws a typed refusal that gives the platform's refusal name and, when a count is reached, the time your application may send again.

## Use it when

Use Email when your backend must write to a person: a customer whose order shipped, a member invited to a team, a user who asked for a receipt. Declare the service by adding `{"kind": "email"}` to the manifest's `services`.

Do not use it for newsletters, marketing, or any mail to a long list.

Your account is charged for each recipient the service accepts for delivery, at the rate on the [pricing page](https://dev.turnzero.ai/pricing). The service skips test end-user addresses before it counts or charges recipients. If the request passes its checks and every address is skipped, it returns a message ID and `recipients: 0` and queues nothing.

The platform limits an application's recipients to 100 a UTC hour and 500 a UTC day, and refuses a send past either limit. Development and production share these limits. For more volume, send through your own mail provider's API with your own key, as [Call an external API with an API key](/cloud/guides/call-an-external-api-with-an-api-key/) describes.

## What it provides

- **A client**, `createEmailClient`, which [The client](#the-client) describes. The module has no dependencies and keeps no state.
- **A send.** The method `send(message)` sends one message to the recipients it lists.
- **The response.** Each send returns `messageId` and the `recipients` the platform accepted and counted. An address named twice counts once.
- **A typed refusal.** Every other response throws an `EmailRefusal`. Its `refusal` member is the platform's refusal name, `members` contains the refusal's other values, and `resetsAt` gives the time from `resets_at`, or `null` if the response has no usable time.
- **A [test double](/cloud/concepts/glossary/#test-double)**, `createEmailDouble` at `@turnzero/email/testing`, which responds to a send as the platform does. [Test with the double](#test-with-the-double) shows it in use.

| Message member | Required | What it contains |
| --- | --- | --- |
| `to` | Yes | The recipients' addresses. |
| `cc` | No | More recipients, shown on the message as copied. A message has five recipients at most, `to` and `cc` together. |
| `subject` | Yes | One line, up to 1,000 characters. |
| `text` | Yes | The plain-text body. |
| `html` | No | An HTML body, sent beside the text. |
| `replyTo` | No | The one address a reply goes to. |

A message has no blind copy and no attachment. Its subject, text, and HTML together are 256 KiB (262,144 bytes) at most.

## Availability

Version 0.6.2 is the package's current version, and this page describes it. Until the library's next publish, `read_library` can return an earlier version, the one you can take, so this page can describe a change that version lacks. The entry includes the `testing` subpath. The entry contains the compiled modules, the package's [product statement and detailed contract](/cloud/concepts/glossary/#product-statement-and-detailed-contract), and its integration guide. Turn Zero Cloud runs the email service, and `read_usage` returns the month's recipients under `email_recipients`, with the hour's and the day's counts beside it.

## Related feature packages

Use [Logging](/cloud/backend-packages/logging/) to record a refused send. Your application’s log stream shows delivery outcomes for queued messages. A send addressed only to test end users queues nothing and has no delivery outcome in that stream. [Egress](/cloud/backend-packages/egress/) calls your own mail provider's API, for mail past the application's limits.

## The client

The package is published in the library as the npm package `@turnzero/email`, not on the npm registry; [Use the feature package library](/cloud/guides/use-the-library/#5-install-a-package-s-compiled-code) installs it with one line.

The function `createEmailClient(options)` takes either `api` and `token`, or `transport`. The table lists its options:

| Option | Meaning |
| --- | --- |
| `api`, `token` | The platform's origin and the application's platform credential, from `TURNZERO_CLOUD_API` and `TURNZERO_CLOUD_APP_TOKEN`. |
| `environment` | Either `development` or `production`: the environment header's value, where a credential needs one. The platform credential takes none, and the route refuses a value that contradicts it. |
| `fetch` | The fetch-shaped function the client sends through, the global `fetch` by default. |
| `transport` | In place of `api` and `token`: a function of your own, `(path, init)`, that adds the origin and the credential, such as the test double's `transport`. |

Give `transport`, or `api` and `token`, never both. At construction the client throws `EmailClientError`, code `client_options`, for both forms, a missing `token`, or an `api` that is not an http or https address. The method `send(message, { signal })` returns a promise of `{ messageId, recipients }`. A send the platform refuses rejects with `EmailRefusal`; [Refusals](#refusals) lists its members and names.

## Refusals

Branch on the refusal's name, never on its message. An `EmailRefusal` has `refusal`, `status`, `detail`, `members`, and `resetsAt`. These are the refusals a send meets most often; the [refusals page](/cloud/reference/refusals/) lists all of the platform's refusals, with the cause and remedy of each.

| Refusal | Status | Meaning |
| --- | --- | --- |
| `invalid_request` | 400 | A member of the message is malformed, a sixth recipient was named, or the message is past its size, and the detail identifies which, never an address. A member containing a NUL character (U+0000) or an unpaired UTF-16 surrogate is refused the same way. |
| `email_not_declared` | 409 | The application's manifest declares no `email` service. |
| `account_paused` | 429 | The application's account is [paused](/cloud/concepts/glossary/#pause) because its month's balance is used up. Nothing was queued. A paused account resumes when it has Free Allowance available again or credit it can use. The body gives the scheduled monthly reset in `resets_at`, which the client exposes as `resetsAt`. The `Retry-After` header gives the seconds until that reset. Check whether the account has resumed before sending again. |
| `email_limit_reached` | 429 | The send would exceed an applicable recipient limit. |
| `end_user_credential_not_admitted` | 403 | The send request used an end-user session. |
| `platform_credential_required` | 403 | The send request used a session or a minted token. |
| `email_answer_unreadable` | As received | The response does not confirm whether the message was queued. `read_usage` gives usage totals. It cannot confirm whether this message was queued. Leave automatic retries off to avoid sending the person a duplicate. |

A send refused `email_limit_reached` queued nothing. Its `members` give the `period`, the count at the time of the refusal, the limit, and the send's own recipients. When present, `resetsAt` gives the time the service returns in `resets_at`.

## What an application must do

Declare the service, and send from your backend under the platform credential, as [Send email from your application](/cloud/guides/send-email-from-your-application/) describes.

Send once for each event. The client retries nothing, because a send the platform accepted before its response was lost is already queued, and a second send would mail the person twice.

Keep a refused send from failing the request that made it. Record the refusal, and respond to the request. After `email_limit_reached`, hold the message until `resetsAt`, or send it through your own provider.

## Test with the double

The test double, `createEmailDouble(options)` from `@turnzero/email/testing`, responds using the limits for the application's size or plan and the clock you set in the test. Build the client with `createEmailClient({ transport: double.transport })`, or pass `double.fetch` as the client's `fetch`. Read every call in `double.calls` and every send the double accepted in `double.sends`.

For an application with a size, pass its quota key, such as `size: 'shared-1gb'`. The double applies only the `hour` and `day` limits for that size. It ignores `supported` and `month`, as the platform does. Omit `size` to test an application with no size, which reads `supported` and all three counts. Set `cells` to the limits your test needs. The defaults are sample values. The double does not fetch quotas from the service.

A test runs this sample over the double. It uses the size form with an explicit hourly limit:

```js
import assert from 'node:assert/strict';
import { createEmailClient, EmailRefusal } from '@turnzero/email';
import { createEmailDouble } from '@turnzero/email/testing';

// Two recipients an hour, and a clock fixed inside one hour.
const email = createEmailDouble({ size: 'shared-1gb', cells: { hour: 2 }, now: () => new Date('2026-10-12T09:30:00Z') });
const client = createEmailClient({ transport: email.transport });

const sent = await client.send({ to: ['customer@example.com', 'customer@example.com'], cc: ['orders@example.com'], subject: 'Your order shipped', text: 'Your order is on its way.' });
assert.equal(sent.recipients, 2);
assert.equal(email.sends.length, 1);
// The hour's two recipients are spent, so the next send is refused by name, with the time it may go again.
await assert.rejects(
  client.send({ to: ['customer@example.com'], subject: 'Again', text: 'One more.' }),
  (error) => error instanceof EmailRefusal && error.refusal === 'email_limit_reached' && error.resetsAt.toISOString() === '2026-10-12T10:00:00.000Z',
);
```

The double refuses with the email route's own refusal names, which [Refusals](#refusals) lists. Its setup gives these:

| Refusal | Cause | Remedy |
| --- | --- | --- |
| `email_not_on_plan` | A count the double reads is 0, or `supported` is 0 with no `size` given. A size ignores `supported` and `month`. | Set positive counts for the periods the test reads, and `supported: 1` when testing without a `size`. |
| `plan_quantity_unset` | A cell the double reads is `null`. With `size`, its `members.plan` names that quota key and the detail names the key and measure. | Give the cell a quantity, or assert this refusal when testing an Unset quantity. |
| `email_not_declared` | The double was created with `declared: false`. | Leave the service declared. |
| `email_limit_reached` | The sends passed a count the `cells` gave, read at the `now` clock. | Raise the count, move the clock, or `reset()` between tests. |
| `platform_credential_required`, `end_user_credential_not_admitted` | The double was created with `credential: 'session'`, `'minted'`, or `'end_user'`. | Leave the credential at the default, `'platform'`. |
| A scripted refusal | `refuseNext(name)` named a refusal. | Check that the refusal has the name you supplied. |
| A transport error | `failNext(error)` makes the transport reject. With no argument, the error is a `TypeError` with the message `fetch failed`. | Check that the send rejects with the error. |
