Skip to content
You're viewing documentation for v0.6.3. It may be out of date.Go to latest
Email SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

Create an adapter

Implement the EmailProvider contract for a new provider — payload mapping, fail-fast field validation, retryable error mapping, and tests with injected fetch.

This guide builds a complete adapter for a fictional provider, Acme Mail, the same way the SDK’s own adapters are built: a factory that returns an EmailProvider, fail-fast validation of unsupported fields, error mapping with retryability, a plugin wrapper for distribution, and tests that never touch the network.

The contract is one object: a name used for routing and a send(message, context) that returns a normalized response or throws a typed error. Exact types are in the adapter contract reference.

Scaffold the factory

Accept credentials plus two options every adapter should have: baseUrl for proxies and fetch for tests. Pass context.signal into the request so callers can abort sends.

The response shape is { provider, id?, messageId?, accepted?, rejected?, raw? }. Always set provider and keep the untouched API body in raw. The context also carries idempotencyKey and attempt — forward the key when the provider supports it (Resend sends it as an Idempotency-Key header).

Reject unsupported fields before the request

Acme Mail’s API accepts addresses, subject, body, and tags — no headers, attachments, or metadata. Do not drop those fields: throw an EmailValidationError listing them, matching the SDK’s own adapters. This is what makes the adapter safe to use as a fallback route.

Core validation (missing from, no recipient, neither html nor text) already ran before your adapter is called — only check what is specific to your provider.

Map the payload

Keep the mapping in one pure function so it is trivially testable. EmailAddress is string | { email, name? } and recipient fields accept one value or an array, so normalize both:

Map errors with retryability

Throw EmailProviderError with status, the parsed body in details, and an honest retryable flag. The client’s retry loop and isRetryableEmailError are driven by that flag — the SDK’s adapters treat 408, 409, 425, 429, and 5xx as retryable:

Network failures thrown by fetch need no handling — the client wraps them as retryable provider errors automatically.

Wrap it as a plugin

Apps can mount the adapter directly via adapters: [acmeMail(...)]. For a shareable package, also export a plugin wrapper so consumers get a one-line install in plugins:

Test with injected fetch

The injected fetch makes the adapter fully testable without mocks or network. Cover the three behaviors that matter: payload mapping, error retryability, and unsupported-field rejection.

Run bun test — three green tests and zero network calls.

Checklist

  • One stable adapter name; document it as the routing name.
  • provider, id or messageId, and raw on every response.
  • context.signal forwarded into provider requests; context.idempotencyKey forwarded when supported.
  • Unsupported fields throw EmailValidationError — never dropped.
  • Provider failures throw EmailProviderError with status and an honest retryable.
  • Injected fetch accepted and documented.

Next

Last updated on