Skip to content
OnCharge.io
View as Markdown
Developer docs

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#

HTTP
POST /emails
FieldTypeRequiredDescription
fromstringYesSender, as "Name <orders@example.com>" or a bare address. The domain must be verified.
tostring or array of stringsYesRecipients. Up to 50 addresses in total across to, cc and bcc.
ccstring or array of stringsNoCopy recipients.
bccstring or array of stringsNoBlind copy recipients.
reply_tostring or array of stringsNoWhere replies go.
subjectstringYes, unless the template has oneSubject line.
htmlstringOne of html, text or templateHTML body.
textstringOne of html, text or templatePlain-text body. Send it with html too: some readers and filters prefer it.
templateobjectOne of html, text or template{ "id": "<template id>", "variables": { "KEY": "value" } }. See Templates.
attachmentsarray of objectsNoFiles to attach. See Attachments.
tagsarray of objectsNo[{ "name": "...", "value": "..." }], returned on the email and in webhooks. See Tags.
headersobjectNoExtra headers, for example { "X-Entity-Ref-ID": "1001" }.
scheduled_atstringNoSend later: an ISO 8601 time or plain words such as "in 1 hour", up to 30 days ahead. See Scheduled sends.
streamstringNotransactional (default) or marketing. See Streams.
Request
{
  "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" }
}
Response 200
{ "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:

HTTP
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 409 with idempotency_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#

JSON
"attachments": [
  { "filename": "invoice-1001.pdf", "content": "JVBERi0xLjcKJ...", "content_type": "application/pdf" },
  { "filename": "logo.png", "path": "https://example.com/email/logo.png", "content_id": "logo" }
]
FieldDescription
filenameThe file name the recipient sees.
contentThe file, base64-encoded. Give either content or path.
pathAn https URL OnCharge downloads the file from.
content_typeOptional MIME type; worked out from the file name when left out.
content_idOptional. 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.

Shell
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:

Shell
curl -X POST https://app.oncharge.io/api/v1/email-templates/{id}/publish \
  -H "Authorization: Bearer $ONCHARGE_MESSAGING_KEY"
POST /emails
{
  "from": "Example Store <orders@example.com>",
  "to": "shopper@example.net",
  "template": { "id": "<template id>", "variables": { "ORDER_NUMBER": "1001", "FIRST_NAME": "Alex" } }
}
EndpointDoes
POST /email-templatesCreates a draft: name, and optionally subject, html, text and variables (key, type of string or number, optional fallback).
GET /email-templatesLists templates.
GET /email-templates/{id}Reads one.
PATCH /email-templates/{id}Changes the draft.
POST /email-templates/{id}/publishPublishes the draft, so sends use it.
POST /email-templates/{id}/duplicateCopies 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:

JSON
{ "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. Its last_event becomes canceled.

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:

Shell
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." }
  ]'
Response 200
{ "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:

ValueBehaviour
strict (default)One invalid email fails the whole batch, and nothing is sent.
permissiveValid emails are sent; the response also lists the others: { "data": [...], "errors": [{ "index": 3, "message": "..." }] }.

Retrieve and list emails#

HTTP
GET /emails/{id}
Response 200
{
  "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:

ValueMeaning
queuedAccepted and waiting to be sent.
scheduledWaiting for its scheduled_at time.
sentHanded to the recipient's mail server.
deliveredThe recipient's mail server accepted it.
delivery_delayedThe recipient's server is deferring it; OnCharge keeps trying.
bouncedThe recipient's server refused it.
complainedThe recipient marked it as spam.
openedOpened (approximate: some mail apps open every message automatically).
clickedA link in it was clicked.
failedIt could not be sent, for example because of an invalid attachment.
suppressedNot sent, because the address is on your suppression list.
canceledA scheduled email you canceled.

List the log, newest first:

HTTP
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.

EndpointDoes
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.