Are you ready to start sending emails with the SendLayer API?
The SendLayer REST API is easy to integrate with, fully featured, and allows you to programmatically send transactional emails.
In this tutorial, we’ll show you how to start sending emails with the SendLayer API.
Before getting started, be sure to:
Note: Check out SendLayer’s API reference for details on the HTTP requests available through the SendLayer REST API.
Base URL
All API calls need to start with the following base API URL:
https://console.sendlayer.com/api/v1/
Authentication
All API calls require a Bearer Token authorization header with a valid API key:
"Authorization": "Bearer XYZ" (replacing XYZ with your API key)
Note: Not sure where to find your SendLayer API key? Check out our tutorial on managing API keys for all the details.
Sending a Single Email
In the following sections, we’ll cover how to send a single email with the SendLayer API and what to expect when your message is sent.
Note: The API allows for a larger total email size, including both content and attachments, with a limit of 25 MB. In contrast, SMTP only allows a maximum of 10 MB.
Endpoint
To send a single email with the SendLayer API, you’ll need to execute an HTTP POST request using the /email
endpoint:
POST https://console.sendlayer.com/api/v1/email
JSON Message Format
SendLayer sends emails through the API based on JSON input. The JSON properties, such as Subject
, HTMLContent
, and PlainContent
, are converted to a valid message when sent to the recipient.
Below you’ll find an example JSON payload that can be used to send an email in an API platform like Postman. Be sure to replace the example email addresses with valid email addresses in the From
and To
objects.
Note: The “From” email address must contain your sending domain, which is the domain you’ve registered in SendLayer. For example, if you’ve registered example.com you’ll need to use [email protected].
Example JSON payload:
{
"From": {
"name": "Paulie Paloma",
"email": "[email protected]"
},
"To": [
{
"name": "Pattie Paloma",
"email": "[email protected]"
}
],
"Subject": "This is the email subject",
"ContentType": "HTML",
"HTMLContent": "<html><body><p>This is a test email sent with the <a href=\"https://sendlayer.com\">SendLayer</a> API!</p></body></html>",
"PlainContent": "This is the plain text email.",
"Tags": [
"tag-name",
"daily"
],
"Headers": {
"X-Mailer": "Test mailer",
"X-Test": "test header"
}
}
Sending a Request With cURL
If you have cURL installed on your machine, you can send a request using the command line. You’ll need to replace XYZ
in the authorization header with a valid API key. Also, be sure to replace the example email addresses with valid email addresses in the From
and To
objects.
curl --location --request POST 'https://console.sendlayer.com/api/v1/email' \
--header 'Authorization: Bearer XYZ' \
--header 'Content-Type: application/json' \
--data-raw '{
"From": {
"name": "Paulie Paloma",
"email": "[email protected]"
},
"To": [
{
"name": "Pattie Paloma",
"email": "[email protected]"
}
],
"Subject": "This is the email subject",
"ContentType": "HTML",
"HTMLContent": "<html><body><p>This is a test email sent with the <a href=\"https://sendlayer.com\">SendLayer</a> API!</p></body></html>",
"PlainContent": "This is the plain text email.",
"Tags": [
"tag-name",
"daily"
],
"Headers": {
"X-Mailer": "Test mailer",
"X-Test": "test header"
}
Success Message
When you successfully send an API email, you’ll receive a JSON message with the MessageID
.
Example success message:
{
"MessageID": "ab800253-4dc4-47a6-9ad6-1daa643d3d8b"
}
Error Codes
If your API call is unsuccessful, an error code and message will be returned in JSON. Below is a list of the error codes followed by an example error:
Code | Message |
---|---|
1 | Missing SenderAPIKey |
2 | Missing FromName |
3 | Missing FromEmail |
4 | Missing ReplyToName |
5 | Missing ReplyToEmail |
6 | Missing ToName |
7 | Missing ToEmail |
8 | Missing Subject |
9 | Missing ContentType |
10 | Missing HTMLContent |
11 | Missing PlainContent |
12 | Invalid user |
13 | Invalid sender API key |
14 | Recipient email is suppressed |
15 | Invalid TemplateID |
16 | Invalid TargetListID |
17 | Email quota reached |
18 | Recipient name or email address is missing |
19 | Recipient email address is invalid |
20 | There is no recipient set for the email OR The number of recipients exceeds the allowed recipient email address count |
21 | CC name or email address is missing |
22 | CC email address is invalid |
23 | Invalid from email address format |
24 | BCC email addresses must be set as an array |
25 | BCC name or email address is missing |
26 | BCC email address is invalid |
27 | The number of BCC email addresses exceed the allowed email address count |
28 | Reply-To email addresses must be set as an array |
29 | Reply-To name or email address is missing |
30 | Reply-To email address is invalid |
31 | The number of Reply-To email addresses exceed the allowed email address count |
32 | Domain is not activated |
429 | Too many requests |
Example error response:
{
"Errors": [
{
"Code": 13,
"Message": "Invalid SenderAPIKey"
}
]
}
Recipient Limits for Single Emails
When sending emails, you can address multiple recipients by adding additional recipient objects in the “To,” “Cc,” and “Bcc” fields. Ensure you do not exceed 10 addresses for “To” and “Cc” and 5 for “Bcc” to avoid exceeding SendLayer’s limits.
That’s it! Now you know how to send an email with the SendLayer API.
Next, would you like to learn how to send SendLayer data to 3rd party apps? Check out our tutorial on managing webhooks for more information.