Sending email
Everything about POST /emails: the fields, idempotency, attachments, tags, templates, scheduled sends, batches, reading the log and suppressions. Requests go to https://app.oncharge.io/api/v1 with Authorization: Bearer oc_msg_live_... (see the quickstart).
Send one email#
POST /emails| Field | Type | Required | Description |
|---|---|---|---|
from | string | Yes | Sender, as "Name <orders@example.com>" or a bare address. The domain must be verified. |
to | string or array of strings | Yes | Recipients. Up to 50 addresses in total across to, cc and bcc. |
cc | string or array of strings | No | Copy recipients. |
bcc | string or array of strings | No | Blind copy recipients. |
reply_to | string or array of strings | No | Where replies go. |
subject | string | Yes, unless the template has one | Subject line. |
html | string | One of html, text or template | HTML body. |
text | string | One of html, text or template | Plain-text body. Send it with html too: some readers and filters prefer it. |
template | object | One of html, text or template | { "id": "<template id>", "variables": { "KEY": "value" } }. See Templates. |
attachments | array of objects | No | Files to attach. See Attachments. |
tags | array of objects | No | [{ "name": "...", "value": "..." }], returned on the email and in webhooks. See Tags. |
headers | object | No | Extra headers, for example { "X-Entity-Ref-ID": "1001" }. |
scheduled_at | string | No | Send later: an ISO 8601 time or plain words such as "in 1 hour", up to 30 days ahead. See Scheduled sends. |
stream | string | No | transactional (default) or marketing. See Streams. |
{
"from": "Example Store <orders@example.com>",
"to": ["shopper@example.net"],
"reply_to": "help@example.com",
"subject": "Your order #1001 has shipped",
"html": "<p>Your order is on its way. Track it <a href=\"https://example.com/track/1001\">here</a>.</p>",
"text": "Your order is on its way. Track it at https://example.com/track/1001",
"tags": [{ "name": "type", "value": "shipping" }, { "name": "order", "value": "1001" }],
"headers": { "X-Entity-Ref-ID": "1001" }
}{ "id": "0b6f2c1e-7d4a-4e8b-9f3c-2a1d5e6b7c80" }A 200 means OnCharge accepted the email and queued it. Whether it was delivered comes later, as the email's last_event and as webhooks.
Before anything is sent, every email passes OnCharge's checks: your API key and domain, the recipient's suppression status, and the compliance check that reads the content for your store's category. A refused email returns an error such as domain_not_verified, recipient_suppressed or content_blocked; see Limits & errors.
Idempotency#
Send an Idempotency-Key header (up to 256 characters) so a retry never sends the same email twice:
Idempotency-Key: order-1001-shipped- OnCharge keeps each key for 24 hours.
- The same key with the same body returns the first response, without sending again.
- The same key with a different body returns
409withidempotency_conflict.
Use something that identifies the message in your system, such as the order and the kind of email. Idempotency keys work on POST /emails and POST /emails/batch.
Attachments#
"attachments": [
{ "filename": "invoice-1001.pdf", "content": "JVBERi0xLjcKJ...", "content_type": "application/pdf" },
{ "filename": "logo.png", "path": "https://example.com/email/logo.png", "content_id": "logo" }
]| Field | Description |
|---|---|
filename | The file name the recipient sees. |
content | The file, base64-encoded. Give either content or path. |
path | An https URL OnCharge downloads the file from. |
content_type | Optional MIME type; worked out from the file name when left out. |
content_id | Optional. Set it to show an image inline with <img src="cid:logo"> in the HTML. |
The whole email, attachments included, can be up to 40 MB after encoding. Batches cannot carry attachments.
Tags#
Tags label an email so you can find it in the log and route its webhooks: the order, the kind of email, a campaign.
- Each tag is
{ "name": "...", "value": "..." }. - Names and values use ASCII letters, numbers,
_and-, up to 256 characters each. - Tags come back on the email object and on every webhook about it.
Templates#
Store an email once and send it with different values. Variables use triple braces, {{{KEY}}}, in the subject, HTML and text.
curl -X POST https://app.oncharge.io/api/v1/email-templates \
-H "Authorization: Bearer $ONCHARGE_MESSAGING_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Order shipped",
"subject": "Order {{{ORDER_NUMBER}}} has shipped",
"html": "<p>Hi {{{FIRST_NAME}}}, your order is on its way.</p>",
"variables": [
{ "key": "ORDER_NUMBER", "type": "string" },
{ "key": "FIRST_NAME", "type": "string", "fallback": "there" }
]
}'Publish it, then send it by id:
curl -X POST https://app.oncharge.io/api/v1/email-templates/{id}/publish \
-H "Authorization: Bearer $ONCHARGE_MESSAGING_KEY"{
"from": "Example Store <orders@example.com>",
"to": "shopper@example.net",
"template": { "id": "<template id>", "variables": { "ORDER_NUMBER": "1001", "FIRST_NAME": "Alex" } }
}| Endpoint | Does |
|---|---|
POST /email-templates | Creates a draft: name, and optionally subject, html, text and variables (key, type of string or number, optional fallback). |
GET /email-templates | Lists templates. |
GET /email-templates/{id} | Reads one. |
PATCH /email-templates/{id} | Changes the draft. |
POST /email-templates/{id}/publish | Publishes the draft, so sends use it. |
POST /email-templates/{id}/duplicate | Copies a template. |
DELETE /email-templates/{id} | Deletes it. |
A variable with no value and no fallback is an error. The same templates are available to the marketing editor in the dashboard.
Scheduled sends#
Set scheduled_at to send later, up to 30 days ahead:
{ "scheduled_at": "2027-01-15T09:00:00-05:00" }Plain words work too, such as "in 1 hour" or "tomorrow at 9am". A scheduled email's last_event is scheduled until it goes out.
- Move it:
PATCH /emails/{id}with a new{ "scheduled_at": "..." }. - Cancel it:
POST /emails/{id}/cancel. Itslast_eventbecomescanceled.
Both work on scheduled emails only. Batches cannot be scheduled.
Batches#
Send up to 1,000 emails in one request. The body is an array of email objects, each with the fields above except attachments and scheduled_at:
curl -X POST https://app.oncharge.io/api/v1/emails/batch \
-H "Authorization: Bearer $ONCHARGE_MESSAGING_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: shipping-run-2027-01-15" \
-d '[
{ "from": "Example Store <orders@example.com>", "to": "a@example.net", "subject": "Order 1001 shipped", "text": "On its way." },
{ "from": "Example Store <orders@example.com>", "to": "b@example.net", "subject": "Order 1002 shipped", "text": "On its way." }
]'{ "data": [{ "id": "0b6f2c1e-7d4a-4e8b-9f3c-2a1d5e6b7c80" }, { "id": "5c2e9a7d-1f3b-4d6e-8a0c-9b7f1e2d3c41" }] }The x-batch-validation header decides what happens when some emails are invalid:
| Value | Behaviour |
|---|---|
strict (default) | One invalid email fails the whole batch, and nothing is sent. |
permissive | Valid emails are sent; the response also lists the others: { "data": [...], "errors": [{ "index": 3, "message": "..." }] }. |
Retrieve and list emails#
GET /emails/{id}{
"id": "0b6f2c1e-7d4a-4e8b-9f3c-2a1d5e6b7c80",
"from": "Example Store <orders@example.com>",
"to": ["shopper@example.net"],
"cc": [],
"bcc": [],
"reply_to": ["help@example.com"],
"subject": "Your order #1001 has shipped",
"html": "<p>Your order is on its way.</p>",
"text": "Your order is on its way.",
"tags": [{ "name": "type", "value": "shipping" }],
"created_at": "2027-01-15T14:02:11.000Z",
"scheduled_at": null,
"last_event": "delivered",
"stream": "transactional"
}last_event is one of:
| Value | Meaning |
|---|---|
queued | Accepted and waiting to be sent. |
scheduled | Waiting for its scheduled_at time. |
sent | Handed to the recipient's mail server. |
delivered | The recipient's mail server accepted it. |
delivery_delayed | The recipient's server is deferring it; OnCharge keeps trying. |
bounced | The recipient's server refused it. |
complained | The recipient marked it as spam. |
opened | Opened (approximate: some mail apps open every message automatically). |
clicked | A link in it was clicked. |
failed | It could not be sent, for example because of an invalid attachment. |
suppressed | Not sent, because the address is on your suppression list. |
canceled | A scheduled email you canceled. |
List the log, newest first:
GET /emails?limit=20&after=<id>limit is 1 to 100 (default 20). Page with after (older than that email) or before (newer). The response is { "object": "list", "has_more": true, "data": [ ... ] }. Emails stay in the log for 90 days.
Streams#
Every email travels in one of two streams:
transactional(the default): receipts, shipping notices, password resets, login codes. Mail people expect because of something they did.marketing: newsletters, promotions and anything else people opted in to. Marketing mail carries one-click unsubscribe headers, and an unsubscribe adds the address to your suppression list.
The streams are kept apart. If your marketing stream is paused, for example because complaints rose, receipts and login codes keep going out.
Suppressions#
OnCharge does not send to an address on your suppression list. Addresses are added:
- after a hard bounce (the mailbox does not exist);
- after repeated soft bounces;
- when the recipient marks your mail as spam;
- when the recipient unsubscribes from marketing mail.
A send to a suppressed address is not delivered: the request returns recipient_suppressed, or the email's last_event becomes suppressed and an email.suppressed webhook is sent.
| Endpoint | Does |
|---|---|
GET /email-suppressions?email= | Lists suppressions, or looks up one address. |
DELETE /email-suppressions/{email} | Removes a manual or complaint suppression. A hard bounce cannot be removed for 14 days. |
Remove a suppression only when the person has asked to hear from you again.