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

Client

API reference for createEmailClient — client options, send and sendBatch, adapter helpers, and the normalized response.

Complete reference for the email client: constructor options, every client method, per-send options, and the normalized response shape. For how routing behaves at runtime, see fallbacks and retries.

createEmailClient(options)

PropType
adapters?EmailProvider[]

Adapters to register, keyed by their routing names.

TypeEmailProvider[]
Default[]
defaultAdapter?string

Routing name used when a send sets no adapter.

Typestring
Defaultfirst registered adapter
fallback?string[]

Routing names tried in order after the selected adapter finally fails.

Typestring[]
Default[]
retry?EmailRetryConfig

Per-adapter retry budget, backoff delay, and retry predicate.

TypeEmailRetryConfig
Defaultno retries
hooks?EmailHooks

Observe-only callbacks: beforeSend, afterSend, onError, onRetry.

TypeEmailHooks
plugins?EmailPlugin[]

Plugins that add adapters, hooks, middleware, or client extensions.

TypeEmailPlugin[]
Default[]

retry options are documented in fallbacks and retries, hooks in hooks, and plugins in the plugin API. providers and defaultProvider are accepted aliases.

Construction fails fast with an EmailValidationError for a duplicate adapter routing name (Duplicate email adapter "x".) or plugin id (Duplicate email plugin "x".), and for an empty client (createEmailClient requires a default adapter.). A defaultAdapter that is not registered throws EmailProviderNotFoundError.

email.send(message, options?)

Validates the message, builds the route [adapter, ...fallbackAdapters] (duplicates removed), retries each adapter per the retry config, and resolves with the first successful response. One failed route rethrows the adapter’s error as-is; multiple failed routes throw EmailSdkError with code all_providers_failed — see errors.

SendOptions

PropType
adapter?string

Routing name for this send. Overrides defaultAdapter.

Typestring
fallbackAdapters?string[]

Replaces the client-level fallback list for this send. Pass [] to disable fallback.

Typestring[]
retries?number

Replaces the client-level retry count for this send.

Typenumber
signal?AbortSignal

AbortSignal forwarded to the provider's fetch call.

TypeAbortSignal
idempotencyKey?string

Stable send identity passed to the adapter on every attempt. Falls back to message.idempotencyKey.

Typestring
metadata?Record<string, unknown>

Send-scope metadata visible to hooks and middleware. Never sent in the message.

TypeRecord<string, unknown>

provider and fallbackProviders are accepted aliases. Send-options metadata is unrelated to message.metadata — the message field goes to the provider, the send option only flows to hooks.

email.sendBatch(messages, options?)

Sends sequentially, one message at a time in array order, and always resolves with one result per item — a failed item never throws or stops the batch:

Each item is an EmailMessage plus optional adapter and fallbackAdapters that override the call-level options for that item:

email.adapter(name)

Returns the registered adapter or throws EmailProviderNotFoundError. Useful for adapter escape hatches like email.adapter("memory").raw.sent in tests.

email.withAdapter(name)

Returns a client pinned to one route — send and sendBatch behave as if { adapter: name } were passed every time. The name is checked immediately: an unregistered name throws EmailProviderNotFoundError at withAdapter time, not at send time.

Properties

PropType
adapters?ReadonlyMap<string, EmailProvider>

All registered adapters, keyed by routing name. Read-only.

TypeReadonlyMap<string, EmailProvider>
defaultAdapter?string

The resolved default routing name.

Typestring

Response

Every successful send resolves with a normalized EmailProviderResponse. provider is always set — it names the adapter that actually delivered, which matters when a fallback route handled the send.

PropType
providerstring

Routing name of the adapter that delivered the message.

Typestring
id?string

Provider-assigned id for the send, when the API returns one.

Typestring
messageId?string

Provider message id, when distinct from id.

Typestring
accepted?string[]

Recipients the provider accepted, when reported.

Typestring[]
rejected?string[]

Recipients the provider rejected, when reported.

Typestring[]
raw?unknown

Untouched provider response body for debugging and provider-specific fields.

Typeunknown

Aliases

adapter and provider mean the same thing everywhere: providers, defaultProvider, fallbackProviders, email.provider(name), email.providers, and email.withProvider(name) are compatibility aliases for their adapter spellings. Prefer adapter in new code — see naming aliases.

Last updated on