---
title: Production send pipeline
description: Build one production-ready email client with retries, a compatible fallback route, shared defaults, idempotency keys, observability, tests, and CLI verification.
sidebar:
  icon: "workflow"
---

This guide grows one `lib/email.ts` from a single adapter into a production send pipeline: retries on transient failures, a fallback route for outages, org-wide defaults, idempotency keys, secret-safe observability, a test that proves the routing, and CLI checks before the first live send.

It assumes you have finished the [quickstart](/v0.6.5/docs/getting-started/quickstart). None of the steps touch your application code, since `email.send(...)` stays the only call sites know about.

1. ### Start with the primary adapter

    One shared client, one adapter. These docs use Resend, but any [adapter](/v0.6.5/docs/adapters) slots in the same way.

    ```ts title="lib/email.ts"
    import { createEmailClient } from "@opencoredev/email-sdk";
    import { resend } from "@opencoredev/email-sdk/resend";

    export const email = createEmailClient({
      adapters: [resend({ apiKey: process.env.RESEND_API_KEY! })],
    });
    ```

2. ### Add retries for transient failures

    By default a send makes exactly one attempt. Give the client a retry budget for rate limits, 5xx responses, and network errors:

    ```ts title="lib/email.ts"
    export const email = createEmailClient({
      adapters: [resend({ apiKey: process.env.RESEND_API_KEY! })],
      retry: { retries: 2 },
    });
    ```

    Only [retryable errors](/v0.6.5/docs/reference/errors) are retried: HTTP 408, 409, 425, 429, 5xx, and network failures. Validation errors and hard rejections fail immediately. The default backoff is `min(100 * 2^(attempt - 1), 2000)` milliseconds, doubling from 100ms and capped at 2s. See [fallbacks and retries](/v0.6.5/docs/concepts/fallbacks-and-retries) for the full retry config, including custom `delay` and `shouldRetry`.

3. ### Add a compatible fallback route

    Retries cover blips. For a real outage you want a fallback adapter. Pick a backup that supports every field your messages use, checking [field support](/v0.6.5/docs/adapters/field-support) first. Postmark pairs well with Resend here: both carry CC, BCC, reply-to, headers, attachments, and tags.

    ```ts title="lib/email.ts"
    import { createEmailClient } from "@opencoredev/email-sdk";
    import { postmark } from "@opencoredev/email-sdk/postmark";
    import { resend } from "@opencoredev/email-sdk/resend";

    export const email = createEmailClient({
      adapters: [
        resend({ apiKey: process.env.RESEND_API_KEY! }),
        postmark({ serverToken: process.env.POSTMARK_SERVER_TOKEN! }),
      ],
      fallback: ["postmark"],
      retry: { retries: 2 },
    });
    ```

    Each send now tries Resend (with retries), then Postmark (with retries). A send that includes a field a route rejects, say `metadata` on the Resend side or a second tag on Postmark, throws an `EmailValidationError` instead of silently dropping data.

4. ### Apply org-wide defaults

    The [defaults plugin](/v0.6.5/docs/plugins/built-in/defaults) merges shared values into every message before validation, so individual call sites stay minimal:

    ```ts title="lib/email.ts"
    import { defaultsPlugin } from "@opencoredev/email-sdk/plugins/defaults";

    export const email = createEmailClient({
      adapters: [
        resend({ apiKey: process.env.RESEND_API_KEY! }),
        postmark({ serverToken: process.env.POSTMARK_SERVER_TOKEN! }),
      ],
      fallback: ["postmark"],
      retry: { retries: 2 },
      plugins: [
        defaultsPlugin({
          replyTo: "Acme Support <support@acme.com>",
          headers: { "X-Service": "checkout" },
          sendMetadata: { service: "checkout" },
        }),
      ],
    });
    ```

    Message values always win: a send that sets its own `replyTo` keeps it. `sendMetadata` attaches context for hooks and observability without touching the message. Use it instead of message `metadata` when a route (like Resend) cannot carry provider metadata.

5. ### Add idempotency keys to externally visible sends

    Retries and fallbacks mean one logical send can become several provider requests. Give every send a user could notice twice (receipts, password resets, invoices) a stable key derived from your domain:

    ```ts
    await email.send(
      {
        from: "Acme <receipts@acme.com>",
        to: "user@example.com",
        subject: "Your receipt",
        text: "Thanks for your order.",
      },
      { idempotencyKey: `receipt:${order.id}` },
    );
    ```

    The key reaches every adapter attempt through the provider context. Resend, JetEmail, Lettermint, and Primitive forward it as an `Idempotency-Key` header so the provider deduplicates. Custom adapters get the same key and can build their own dedupe logic on it. `defaultsPlugin` can add an `idempotencyKeyPrefix` to namespace keys per environment.

6. ### Wire up observability

    The [observability plugin](/v0.6.5/docs/plugins/built-in/observability) emits `email.sent`, `email.retry`, and `email.error` events with a redacted message: counts, tag names, and the subject, without bodies or recipients. The complete client:

    ```ts title="lib/email.ts"
    import { createEmailClient } from "@opencoredev/email-sdk";
    import { defaultsPlugin } from "@opencoredev/email-sdk/plugins/defaults";
    import { observabilityPlugin } from "@opencoredev/email-sdk/plugins/observability";
    import { postmark } from "@opencoredev/email-sdk/postmark";
    import { resend } from "@opencoredev/email-sdk/resend";

    export const email = createEmailClient({
      adapters: [
        resend({ apiKey: process.env.RESEND_API_KEY! }),
        postmark({ serverToken: process.env.POSTMARK_SERVER_TOKEN! }),
      ],
      fallback: ["postmark"],
      retry: { retries: 2 },
      plugins: [
        defaultsPlugin({
          replyTo: "Acme Support <support@acme.com>",
          headers: { "X-Service": "checkout" },
          sendMetadata: { service: "checkout" },
        }),
        observabilityPlugin({
          log(event) {
            console.log(event.type, event.provider, event.attempt, event.metadata);
          },
          metric(event) {
            // increment counters: emails_sent_total{provider}, emails_error_total{provider}, ...
          },
        }),
      ],
    });
    ```

    A spike of `email.retry` followed by `email.sent` on `postmark` tells you the fallback route earned its keep. Observer exceptions are swallowed, so monitoring cannot break a send.

7. ### Prove the routing with a test

    Rebuild the same topology with [testing adapters](/v0.6.5/docs/guides/test-email-behavior): a failing primary, a memory backup, and the [capture plugin](/v0.6.5/docs/plugins/built-in/capture) recording lifecycle events.

    ```ts title="lib/email.test.ts"
    import { expect, test } from "bun:test";
    import { createEmailClient } from "@opencoredev/email-sdk";
    import { capturePlugin } from "@opencoredev/email-sdk/plugins/capture";
    import { failingProvider, memoryProvider } from "@opencoredev/email-sdk/testing";

    test("falls back to the backup route when the primary fails", async () => {
      const backup = memoryProvider("backup");
      const email = createEmailClient({
        adapters: [failingProvider("primary"), backup],
        fallback: ["backup"],
        retry: { retries: 2, delay: () => 0 },
        plugins: [capturePlugin()],
      });

      const response = await email.send({
        from: "Acme <receipts@acme.com>",
        to: "user@example.com",
        subject: "Your receipt",
        text: "Thanks for your order.",
      });

      expect(response.provider).toBe("backup");
      expect(backup.raw.sent).toHaveLength(1);

      const types = email.capture.events.map((event) => event.type);
      expect(types).toContain("error"); // primary route failed
      expect(types).toContain("afterSend"); // backup route delivered
    });
    ```

    Run it with `bun test`. Use the message shape production actually sends, because that is what catches a fallback route that cannot carry one of your fields.

8. ### Verify from the CLI

    Check the environment for both routes, dry-run a representative message, then make one real smoke send:

    ```bash
    npx email-sdk doctor --adapter resend
    npx email-sdk doctor --adapter postmark
    ```

    ```bash
    npx email-sdk send \
      --adapter resend \
      --from "Acme <receipts@acme.com>" \
      --to "user@example.com" \
      --subject "Pipeline check" \
      --text "It works" \
      --dry-run
    ```

    `--dry-run` validates message shape and adapter field support without a provider request. Then drop the flag once per route, from the environment that will send production email. That live send is the only check that proves the account, sender domain, and recipient policy are ready.

## Next

**[Fallbacks and retries](/v0.6.5/docs/concepts/fallbacks-and-retries)**

Route resolution, backoff tuning, error classification, and per-send overrides.

**[Test email behavior](/v0.6.5/docs/guides/test-email-behavior)**

More recipes: assert fields, retries, and shared capture stores.

**[Built-in plugins](/v0.6.5/docs/plugins)**

Everything defaultsPlugin, observabilityPlugin, and capturePlugin can do.

**[CLI reference](/v0.6.5/docs/reference/cli)**

All doctor, send, and dry-run flags.
