Sending email
Send one email or a batch with the exx.sh API, schedule and cancel emails, retry safely with idempotency keys, and add attachments, tags, headers and streams.
POST /emails sends one email to up to 50 recipients (to, cc
and bcc together). It answers with the email's id once the message is accepted and
queued; delivery happens after, and each recipient gets its own attempts
(how delivery works).
A single email
import { Exx } from "@exx.sh/sdk";
const exx = new Exx(process.env.EXX_API_KEY);
const { data, error } = await exx.emails.send({
from: "Acme <hello@acme.com>",
to: ["delivered@example.com"],
subject: "Your sign-in link",
html: "<p>Click the link to sign in.</p>",
text: "Click the link to sign in.",
tags: [{ name: "category", value: "sign_in" }],
});
if (error) {
// error.name is the API's error name, e.g. "validation_error" or "rate_limit_exceeded".
console.error(`${error.name}: ${error.message}`);
process.exit(1);
}
console.log(data.id);The answer is the email's id:
{ "id": "01a1b246-b422-7615-a9b9-09d31765fc52" }| Field | Required | Value |
|---|---|---|
from | yes, unless the template has one | hello@acme.com or Acme <hello@acme.com>, on a verified domain of the project |
to | yes | One address or an array of addresses |
subject | yes, unless the template has one | The subject line |
html, text | one of them, or template | The body as HTML, as plain text, or both |
template | no | A published template and its variables, instead of html and text |
cc, bcc, reply_to | no | One address or an array |
headers | no | Your own headers, as an object |
attachments | no | Files, below |
tags | no | { name, value } pairs, below |
scheduled_at | no | When to send, below |
Give html, text or both. When you give html without text, exx.sh makes the
plain-text part from your HTML (links become numbered footnotes); give "text": "" to send
the HTML alone. A very large or very deeply nested HTML body is sent without a plain-text
part rather than refused. Mail sent over SMTP keeps the parts your client
wrote.
Every address in to, cc and bcc sees the others. To send the same message to many
people separately, send one email per recipient, or a batch.
Batches
POST /emails/batch takes up to 100 emails at once and counts as one
request against your rate limit. The batch is strict: if one email is
invalid, none is sent, and the error names the email and field ([2].to: ...). Batches
cannot carry attachments. The answer lists the ids in the order of the request:
{"data": [{"id": "..."}, ...]}.
import { Exx } from "@exx.sh/sdk";
const exx = new Exx(process.env.EXX_API_KEY);
// Up to 100 emails; one invalid email refuses the whole batch.
const { data, error } = await exx.batch.send([
{
from: "hello@acme.com",
to: "delivered@example.com",
subject: "Receipt for order 1001",
text: "Thanks!",
},
{
from: "hello@acme.com",
to: "delivered@example.com",
subject: "Receipt for order 1002",
text: "Thanks!",
},
]);
if (error) throw new Error(`${error.name}: ${error.message}`);
for (const sent of data.data) console.log(sent.id);Scheduling
scheduled_at takes an ISO 8601 time or a relative one: in <n> <seconds|minutes|hours|days>
(in 1 hour), at most 30 days ahead. Until it is sent,
PATCH /emails/{id} moves it and POST /emails/{id}/cancel cancels it. Other natural
language (tomorrow at 9am) is refused with validation_error, and scheduling is not
available over SMTP.
import { Exx } from "@exx.sh/sdk";
const exx = new Exx(process.env.EXX_API_KEY);
const sent = await exx.emails.send({
from: "hello@acme.com",
to: "delivered@example.com",
subject: "Your trial ends tomorrow",
text: "Pick a plan to keep sending.",
scheduledAt: "in 1 hour", // or an ISO 8601 time
});
if (sent.error) throw new Error(`${sent.error.name}: ${sent.error.message}`);
// Move it, then cancel it: both work until it is sent.
const later = new Date(Date.now() + 2 * 60 * 60 * 1000).toISOString();
const moved = await exx.emails.update({ id: sent.data.id, scheduledAt: later });
if (moved.error) throw new Error(moved.error.message);
const canceled = await exx.emails.cancel(sent.data.id);
if (canceled.error) throw new Error(canceled.error.message);
console.log(sent.data.id);Retries and idempotency
Send an Idempotency-Key header (1 to 256 characters) with POST /emails and
POST /emails/batch, and a retry within 24 hours gets the first answer back instead of a
second email. The same key with a different body answers 409 invalid_idempotent_request;
a retry while the first request still runs answers 409 concurrent_idempotent_requests.
The SDKs make a key for every send they may retry, so a timeout never sends twice.
Attachments
attachments[] items take content (base64, or the file's bytes as Resend's SDKs send a
byte buffer: an array of numbers or a Node.js Buffer) or path (an https URL exx.sh
downloads, at most 10 per email), plus filename, and optionally
content_type and content_id for inline images (<img src="cid:logo">). The whole
message, attachments included, may be 40 MiB after encoding. Executable files
such as .exe and .js are refused, and any other problem with an attachment answers
422 invalid_attachment. An array of numbers or a Buffer takes up to four bytes of JSON
for each byte of the file, so send files over about 14 MB as base64 or by path.
Attachments you sent
GET /emails/{id}/attachments lists the attachments of an email you sent, in Resend's
shape: id, filename, size (bytes), content_type (with its charset for text),
content_disposition (inline for an image sent with a content_id, else attachment),
content_id, and a download_url with its expires_at. It pages with limit (20 by default, at most 100), after and
before. GET /emails/{id}/attachments/{attachment_id} returns one of them. An
attachment's id stays the same every time you ask.
const { data } = await exx.emails.attachments.list({ emailId: "4ef9a417-…" });
const file = await fetch(data.data[0].download_url); // no API key neededThe download_url needs no API key, so you can hand it to a browser or a job, and it
works for 1 hour. After that it answers 410 exx_link_expired:
list the attachments again for a new link. A link that was changed in any way answers
403 exx_invalid_signature. Anyone who has a link can download the file until it expires,
so share it like the file itself. Downloads always arrive as a file to save, never as a page
the browser opens: HTML, SVG and XHTML files come as application/octet-stream. Any web
page may fetch a link (Access-Control-Allow-Origin: *), since it carries no cookie or key.
Downloads have their own rate per organization, so fetching many files never slows your
sends; past it they answer 429 with Retry-After.
Attachments last as long as the email's content: once your plan's retention removes it,
both endpoints and any link answer 404 not_found. The Resend SDKs'
emails.attachments.list and get work unchanged.
Tags and headers
tags are up to 10 { name, value } pairs (ASCII letters, digits, _
and -) that come back in webhook events, so you can tell which part of your application
sent an email. headers adds your own headers, such as X-Entity-Ref-ID or
List-Unsubscribe. Headers that exx.sh sets itself, such as From, To, Subject,
Message-ID and DKIM-Signature, cannot be set this way.
Streams
Mail is transactional unless you send the header X-Exx-Stream: distributional (newsletters
and other bulk mail). Distributional mail must carry List-Unsubscribe with
List-Unsubscribe-Post: List-Unsubscribe=One-Click (RFC 8058). Every plan, the free one
included, sends both streams. Distributional mail goes out from IP addresses of its own, so
a newsletter's complaints never weigh on your sign-in links.
Common questions
How fast is an email delivered?
exx.sh answers once the email is queued, and delivery starts right after. A recipient's server that refuses the message for now is retried on the schedule in how delivery works. Webhooks tell you when each recipient's result is known.
Can I cancel an email that was already sent?
No. Only a scheduled email can be cancelled, until its scheduled_at time. An email sent
without scheduled_at is queued at once and cannot be called back.