Skip to main content

Node.js

No SDK required. Node has fetch built in from version 18, and the API is JSON over HTTPS.

Send

const response = await fetch('https://api.epostix.com/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.EPOSTIX_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
subject: 'It works',
html: '<p>Sent from Node.</p>',
}),
});

const email = await response.json();
console.log(email.id);

Run it against a sandbox address first and nothing is delivered or charged:

Handle failures properly

response.ok is not enough. Read the error envelope and branch on type.

if (!response.ok) {
const error = await response.json();

switch (error.type) {
case 'rate_limit_exceeded':
case 'daily_quota_exceeded':
await sleep(Number(response.headers.get('Retry-After')) * 1000);
return send();

case 'validation_error':
throw new BadRequest(error.details);

default:
throw new Error(`${error.type}: ${error.message} (${error.request_id})`);
}
}

Always keep a default branch. New error types are added without a version bump, so an exhaustive switch will break. See Versioning.

Make retries safe

Anything you retry should carry an idempotency key derived from your own identifiers, not from the moment of the call.

headers: {
'Idempotency-Key': `order-${order.id}-confirmation`,
}

Retry with the same key and a request that already succeeded returns its original result instead of sending a second email. See Idempotency.

Keep the key on the server

An API key can send mail as your domain. It belongs in an environment variable on a server you control, never in code that reaches a browser or a mobile app.

Next

  • Test mode to build without touching production
  • Webhooks to learn what happened after we accepted the message
  • Errors for the full catalogue