Get started
SDKs
Install the official Ternwire client for Node.js, Python, Ruby, Go, PHP, .NET, or Rust and send your first email. Java is coming soon.
On this page
Ternwire publishes official clients for seven languages. Each one wraps the same /v1 API, calls https://ternwire.com by default, and includes a webhook verification helper. Use whichever matches your server, or call the HTTP API directly.
Java is coming soon. The Java client isn't on Maven Central yet. Until it is, call the HTTP API from Java directly.
Install
npm install ternwireSend an email
Every client takes the request body and an idempotency key. Derive the key from a stable application operation, such as order-123-receipt, and reuse it with the identical body when you retry. Ternwire remembers a key for 7 days after it accepts the email.
import { TernwireClient } from "ternwire";
const client = new TernwireClient({ apiKey: process.env.TERNWIRE_API_KEY });
const message = await client.submitEmail(
{
from: "notifications@your-verified-domain.com",
to: ["recipient@example.com"],
subject: "Your receipt",
text: "Thanks for your order.",
},
"order-123-receipt",
);Configuration
SDKs do not read environment variables on their own. Pass the key to the client yourself. To target another deployment, such as a local API, pass its origin without /v1. The origin must use HTTPS unless it is localhost or a loopback address.
| SDK | Default client | Origin override |
|---|---|---|
| Node.js | new TernwireClient({ apiKey }) | baseUrl option |
| Python | TernwireClient(api_key) | base_url argument |
| Ruby | Ternwire::Client.new(api_key:) | base_url: keyword |
| Go | ternwire.NewClient(ternwire.Config{APIKey: key}) | Config.BaseURL |
| PHP | new TernwireClient($apiKey) | $baseUrl argument |
| .NET | new TernwireClient(apiKey) | baseUrl argument |
| Rust | Client::from_api_key(api_key) | Client::new(api_key, base_url) |
Timeouts default to 30 seconds and can be changed in each client's constructor. Redirects are not followed.
Errors and retries
The Node.js SDK retries for you. After a 429, 502, 503 or 504, a timeout, or a connection error, it repeats a request up to twice. It waits as long as Retry-After asks, or backs off exponentially with jitter when the header is missing. A pause longer than a minute is left to your application. Only requests that are safe to repeat are retried: reads, deletes, replacements, and every request that carries an idempotency key, such as submitEmail. Set maxRetries to change the number, or to 0 to turn retries off. timeoutMs limits each attempt.
The other clients do not run retry loops for you. Keep Retry-After on 429 responses and use bounded backoff when your application chooses to retry, with the same idempotency key and body.
On an API error, each client raises one error type with the HTTP status and the parsed body, which holds the error code, message and field errors. Errors and retry decisions shows the body and how to catch it in Node.js, Python and Ruby. Network failures stay native transport errors, separate from API errors. In Node.js, describeNetworkError(error) names the cause, such as DNS, a refused connection, TLS, or a timeout.
Responses from a newer API
The API adds fields and states over time. The Node.js SDK keeps fields it does not know and returns a state it does not know as sent, so treat state lists as open and keep a default branch when you switch on them.
Listing every page
List methods return one page. In Node.js, paginate walks every page and listAll collects them into an array:
import { listAll, paginate } from "ternwire";
for await (const message of paginate((cursor) => client.listMessages({ state: "failed", cursor }))) {
console.log(message.id);
}
const audiences = await listAll((cursor) => client.listAudiences({ cursor }), { maxItems: 1000 });CommonJS and other runtimes
The Node.js package loads with both import and require, so NestJS, Jest and ts-node projects that compile to CommonJS can use it. The main entry needs no Node.js built-in until you use a Node.js-only feature, so it also loads in edge runtimes that have fetch. The ternwire/verify helper and uploadContactImportFile still need Node.js. The client refuses to run in a web page, where anyone could read the API key. Test suites that use jsdom or happy-dom on Node.js are not affected.
A 202 acceptance means queued, not delivered. Fetch the message or consume webhooks for the eventual outcome.
Method names
Node.js and PHP use the OpenAPI operation ID directly, such as submitEmail and createSendingDomain. Python, Ruby, and Rust use submit_email and create_sending_domain. Go uses SubmitEmail, and .NET uses SubmitEmailAsync.
The Node.js SDK covers all public operations, including marketing audiences and broadcasts. The other SDKs cover transactional email, domains, templates, attachments, and webhooks. Source code for each lives in the ternwire GitHub organization.