Skip to main content
POST
/
api
/
marketing
/
send-email
Send Marketing Email
curl --request POST \
  --url https://onlyautomator.com/api/marketing/send-email \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data @- <<EOF
{
  "subject": "Our Latest Newsletter!",
  "content": "<p>Hello {{first_name}}, here's your update!</p>",
  "fromEmail": "[email protected]",
  "fromName": "OnlyAutomator Team",
  "replyTo": "[email protected]",
  "audienceId": "resend_audience_123",
  "filterActive": true,
  "activeDays": 30,
  "testMode": false,
  "testEmails": [
    "[email protected]",
    "[email protected]"
  ]
}
EOF
{
  "success": true,
  "message": "Email campaign processed.",
  "result": {
    "id": "re_12345",
    "from": "[email protected]",
    "to": [
      "audience_123"
    ],
    "created_at": "2023-10-27T10:00:00Z"
  },
  "total": 150,
  "successful": 148,
  "failed": 2,
  "errors": [
    {
      "email": "[email protected]",
      "reason": "Invalid address"
    }
  ],
  "test_mode": false
}

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

Authorization
string
required
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
subject
string
required
The subject line of the email.
content
string
required
The HTML content of the email. Supports personalization tags: {{first_name}}, {{last_name}}, {{full_name}}, {{email}}.
fromEmail
string
required
The email address to send from.
fromName
string
The name to display as the sender.
replyTo
string
The email address for replies (defaults to fromEmail).
audienceId
string
Option 1: The ID of the Resend audience to send the email to. If provided, user filters (filterActive, activeDays) are ignored.
filterActive
boolean
Option 2: If audienceId is not provided, set to true to filter users based on recent activity.
activeDays
number
Option 2: If filterActive is true, specifies the number of days to consider for recent activity (default: 30).
testMode
boolean
If true, only sends emails to the addresses specified in testEmails (useful for testing filters and content).
testEmails
array[string]
An array of email addresses to target when testMode is true.

Response

success
boolean
Indicates if the overall process was initiated successfully.
message
string
A summary message of the operation.
result
object
(Only when sending to audienceId) The direct response object from the Resend API.
total
number
(Only when sending to filtered users) Total number of users targeted.
successful
number
(Only when sending to filtered users) Number of emails sent successfully.
failed
number
(Only when sending to filtered users) Number of emails that failed to send.
errors
array
(Only when sending to filtered users) An array containing details of the first few send failures (if any).
test_mode
boolean
Indicates if the operation was run in test mode.

Error Codes

Status CodeDescription
400Missing required fields (subject, content, fromEmail) or invalid testEmails format.
401Invalid or missing marketing API key.
500Internal 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.

Authorizations

Authorization
string
header
required

Secret API key for Marketing endpoints (MARKETING_API_SECRET). Prefix with 'Bearer '.

Body

application/json
subject
string
required

The subject line of the email.

Example:

"Our Latest Newsletter!"

content
string
required

The HTML content of the email. Supports personalization tags like {{first_name}}.

Example:

"<p>Hello {{first_name}}, here's your update!</p>"

fromEmail
string
required

The email address to send from.

fromName
string

The name to display as the sender.

Example:

"OnlyAutomator Team"

replyTo
string

The email address for replies (defaults to fromEmail).

audienceId
string

Option 1: The ID of the Resend audience to send the email to. If provided, user filters are ignored.

Example:

"resend_audience_123"

filterActive
boolean

Option 2: If audienceId is not provided, set to true to filter users based on recent activity.

Example:

true

activeDays
number
default:30

Option 2: If filterActive is true, specifies the number of days to consider for recent activity.

Example:

30

testMode
boolean

If true, only sends emails to the addresses specified in testEmails.

Example:

false

testEmails
string<email>[]

An array of email addresses to target when testMode is true.

Response

Email campaign processed. See response body for details.

success
boolean
Example:

true

message
string
Example:

"Email campaign processed."

result
object

Direct response from Resend API if audienceId was used.

Example:
{
"id": "re_12345",
"from": "[email protected]",
"to": ["audience_123"],
"created_at": "2023-10-27T10:00:00Z"
}
total
integer | null

Total number of users targeted (if using filters).

Example:

150

successful
integer | null

Number of emails sent successfully (if using filters).

Example:

148

failed
integer | null

Number of emails that failed to send (if using filters).

Example:

2

errors
object[] | null

Details of send failures (if using filters).

Example:
[
{
"email": "[email protected]",
"reason": "Invalid address"
}
]
test_mode
boolean

Indicates if the operation was run in test mode.

Example:

false