Important Note: Throughout this guide, the example domain "YOUR-DOMAIN.COM" is used as a placeholder. When implementing the API, you'll need to replace this with your actual domain where MailLayer is hosted.
After setting up MailLayer for marketing campaigns, I discovered one of its most powerful features: the transactional API. This feature lets you programmatically send personalized emails directly from your applications. Here's what I learned while implementing it for our user onboarding flow.
What Are Transactional Emails?
Unlike marketing campaigns sent to many subscribers at once, transactional emails are triggered by specific user actions:
- Welcome emails
- Password resets
- Order confirmations
- Shipping notifications
- Account updates
These emails typically have higher open rates because they contain information the user specifically needs or requested.
MailLayer's Transactional API Basics
MailLayer makes sending transactional emails surprisingly simple. The API endpoint is clean and straightforward:
https://YOUR-DOMAIN.COM/api/transactional/send
Note: Replace "YOUR-DOMAIN.COM" with your actual MailLayer domain. In the examples below, I'll use "send.maillayer.com" as a placeholder, but you should use your own domain.
Each request requires just a few parameters:
Parameter | Type | Required | Description |
---|---|---|---|
apiKey | String | Yes | Your template API key |
to | String | Yes | Recipient email address |
variables | Object | No | Variables to replace in the template |
Creating Email Templates
Before using the API, you'll need to create email templates in MailLayer:
- Go to the Transactional section in MailLayer
- Create a new template (e.g., "Welcome Email" or "Password Reset")
- Design your email using the drag-and-drop editor
- Add variable placeholders like
{{userName}}
or{{resetLink}}
- Save and publish your template
Each template gets its own unique API key that looks something like:
txn_67f21b6001f510a2a8c87574_m958x7up
This key is what you'll use when sending emails with this specific template.
Code Implementation
Here's a simple cURL example to send a transactional email:
curl -X POST "https://YOUR-DOMAIN.COM/api/transactional/send" \
-H "Content-Type: application/json" \
-d '{
"apiKey": "txn_67f21b6001f510a2a8c87574_m958x7up",
"to": "recipient@example.com",
"variables": {}
}'
For a welcome email with personalization:
curl -X POST "https://YOUR-DOMAIN.COM/api/transactional/send" \
-H "Content-Type: application/json" \
-d '{
"apiKey": "txn_67f21b6001f510a2a8c87574_m958x7up",
"to": "new-user@example.com",
"variables": {
"firstName": "John",
"accountType": "Premium",
"loginLink": "https://app.example.com/login"
}
}'