Send Emails via API
This page explains how to send emails using the Console API. Before getting started, make sure to set up a project and familiarize yourself with the Console API.
Sending Emails
Endpoint:
POST /api/console/sends
Authorization:
Authorization: Bearer <your-api-key>Create an API key at Console → API with the sends.send scope enabled.
Request:
type Request = SendRequest;This is a SendRequest object, which is used to define the email you want to send.
interface SendRequest {
// The email address of the sender (required)
// the domain of the email address must be verified
from: Address;
// the recipient's email address(es) (required)
to: Address | Address[];
// optional, CC and BCC recipients
cc?: Address | Address[];
bcc?: Address | Address[];
// The subject of the email
subject?: string;
// The body of the email in HTML format
// required if body_text is not provided
body_html?: string;
// The body of the email in plain text format
// required if body_html is not provided
body_text?: string;
// additional headers
// see #limits for restrictions
headers?: Record<string, string>;
attachments?: Attachment[];
}
type Address = string | {
name?: string;
email: string;
};
type Attachment = {
content: string; // base64 encoded
name?: string;
content_type?: string; // MIME type
};{
// email address with a name
"from": {
"name": "HYVOR",
"email": "contact@hyvor.com"
},
// email address without a name
"to": "user@example.org",
// multiple recipients (direct email or mixed with names)
"cc": [
"cc1@example.org",
{
"name": "CC Recipient 2",
"email": "cc2@example.org"
}
],
"bcc": ["bcc1@example.org", "bcc2@example.org"],
"subject": "Welcome to HYVOR",
"body_html": "<h1>Welcome to HYVOR</h1><p>Thank you for signing up!</p>",
"body_text": "Welcome to HYVOR\n\nThank you for signing up!",
"headers": {
"X-Custom-Header": "Custom Value"
},
"attachments": [
{
"content": "SFlWT1IgUm9ja3Mh",
"name": "hello.txt",
"content_type": "text/plain"
}
]
}Retrying Failed Requests & Idempotency
Implement retrying
The HTTP request to send an email may fail due to network issues or other temporary problems. To prevent losing emails, we recommend configuring your application to retry automatically if the API returns a non-2xx status code with incremental backoff.
The recommended retry strategy is to send emails asynchronously and retry up to 3 times with an exponential backoff strategy (e.g., 30s, 2m, 5m). If the request fails after 3 retries, you can log the error and notify your team to investigate the issue.
If your setup sends emails synchronously (ex: within a web request), you can still implement retrying with smaller timeouts, such as 1s, 2s, and 5s.
Idempotency
When retrying it is possible that your request was already accepted and queued by the API, but
somehow your application was not able to correctly handle the response (or the response was not
received due to a network issue). In such cases, you may end up sending the same email multiple
times. To prevent that, use the X-Idempotency-Key header in your request. This header
should contain a unique idempotency key for each email you send.
Some idempotency key examples:
welcome-email-{userId}
(since the welcome email is sent only once to a user)order-confirmation-{orderId}
(since the order confirmation email is sent only once for an order). Note that you should use a new key if you have a "resend" option for an email.
Idempotency keys are saved for 24 hours. If you retry a request with the same idempotency key before the 24-hour period ends, the API will return the same response as the first request without actually processing it. If the idempotency key is not found, the API will process the request as usual and return a new response.
X-Idempotency-Short-Circuit header is set to true if the response was created
using a previously processed request with the same idempotency key.
Response Status Codes
Retry-After header value. If you are using an idempotency
key, you can retry with the same key.If the server returns a 5xx status code, idempotency keys are not saved, and retrying the request is recommended. For 4xx status codes, except 429, the idempotency key is saved, and retrying the request will not have any effect unless you change the request and use a new idempotency key.
Send Status
Send status, as in the Console API Send Object, can be one of the following:
- queued - Hyvor Relay has accepted the request and queued the email for sending. It can also mean that the email is re-queued after a temporary failure.
- accepted - The recipient's email server has accepted the email. The status
can still change to
bouncedorcomplainedlater. - bounced - The recipient's email server has rejected the email. The email will not be retried. If this was a hard bounce (5xx SMTP status code), the recipient's email will be added to the suppression list, and no further emails will be sent to that address.
- complained - The recipient has marked the email as spam or complained about it, usually after the email was delivered.
See Webhook Send Events for more information on each status.
Rate Limiting
The rate limit for the POST /api/console/sends endpoint is 10 requests per second (per project).
Learn more about Console API Rate Limiting.
Recipient Identification
Relay delivers a separate copy of the email to each recipient (TO, CC, and BCC), using one SMTP
transaction per recipient. Each copy has the same Message-ID but includes a unique X-Relay-Recipient header, which is the UUID of the recipient in Relay.
Some mailbox providers, such as Microsoft and Yahoo, redact the recipient's email address in bounces and spam complaints. Relay uses this header (returned in the original message of the report) to identify the recipient when the address is not available. This header is added after DKIM signing and is not included in the signed headers. It cannot be set as a custom header.
General Limits
- Recipients are limited to 20, including TO, CC, and BCC recipients. In Cloud, each recipient is billed as a separate email (ex: if you send an email to 3 recipients, you will be billed for 3 emails).
- Total email size is limited to 10MB.
(including headers, body, and attachments). - HTML body size is limited to 2MB.
- Plain text body size is limited to 2MB.
- Subject is limited to 998 characters.
- Attachments are limited to 10 per email. There is no size limit for each attachment, but the total email size must not exceed 10MB.
- Custom headers do not allow the following headers:
From,To,Cc,Bcc,Sender,Date,Subject,Content-Type,MIME-Version,DKIM-Signature,Return-Path,X-Mailer,X-Originating-IP,X-Relay-Recipient,Authentication-Results