Send Marketing Email
Sends an email campaign via Resend. Allows targeting either a specific Resend audience ID or a dynamic list of users based on activity filters. Supports personalization and test mode.
Authentication
This endpoint requires a secret API key (MARKETING_API_SECRET environment variable) passed as a Bearer token in the Authorization header. This is not the standard user JWT token.
Request
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
The subject line of the email.
The HTML content of the email. Supports personalization tags: {{first_name}}, {{last_name}}, {{full_name}}, {{email}}.
The email address to send from.
The name to display as the sender.
The email address for replies (defaults to fromEmail).
Option 1: The ID of the Resend audience to send the email to. If provided, user filters (filterActive, activeDays) are ignored.
Option 2: If audienceId is not provided, set to true to filter users based on recent activity.
Option 2: If filterActive is true, specifies the number of days to consider for recent activity (default: 30).
If true, only sends emails to the addresses specified in testEmails (useful for testing filters and content).
An array of email addresses to target when testMode is true.
Response
Indicates if the overall process was initiated successfully.
A summary message of the operation.
(Only when sending to audienceId) The direct response object from the Resend API.
(Only when sending to filtered users) Total number of users targeted.
(Only when sending to filtered users) Number of emails sent successfully.
(Only when sending to filtered users) Number of emails that failed to send.
(Only when sending to filtered users) An array containing details of the first few send failures (if any).
Indicates if the operation was run in test mode.
Error Codes
| Status Code | Description |
|---|
| 400 | Missing required fields (subject, content, fromEmail) or invalid testEmails format. |
| 401 | Invalid or missing marketing API key. |
| 500 | Internal server error (e.g., fetching users failed, Resend API error). |
Code Examples
// Send to a specific Resend Audience
const sendToAudience = async (audienceId, emailData, apiKey) => {
const response = await fetch(`https://onlyautomator.com/api/marketing/send-email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({ ...emailData, audienceId })
});
return await response.json();
};
// Send to active users (last 30 days)
const sendToActiveUsers = async (emailData, apiKey) => {
const response = await fetch(`https://onlyautomator.com/api/marketing/send-email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({ ...emailData, filterActive: true, activeDays: 30 })
});
return await response.json();
};
// Example Usage
const emailContent = {
subject: "Special Offer Inside!",
content: "<p>Hi {{first_name}}, check out our new products!</p>",
fromEmail: "[email protected]",
fromName: "OnlyAutomator Team"
};
const marketingApiKey = process.env.MARKETING_API_SECRET;
sendToAudience('resend_audience_id_123', emailContent, marketingApiKey)
.then(result => console.log('Audience send result:', result))
.catch(err => console.error(err));
sendToActiveUsers(emailContent, marketingApiKey)
.then(result => console.log('Active users send result:', result))
.catch(err => console.error(err));
Notes
- Authorization: Uses a dedicated marketing API key, NOT user JWTs.
- Targeting: Can target a Resend audience directly OR filter users from the database.
- Batching: When sending to filtered users, emails are sent in batches of 50 to prevent timeouts.
- Personalization: Supports
{{first_name}}, {{last_name}}, {{full_name}}, {{email}} tags in the content.
- Test Mode: Allows safe testing by targeting specific email addresses.
The email address to send the test to
The template to use for the email
Available options:
login,
welcome,
setup,
feedback
The user's first name to insert in the template