Sync Users to Marketing Audience
This endpoint fetches users from the database based on activity filters, creates a new audience in Resend, and adds the filtered users as contacts to that audience.
There is also a GET method on this endpoint to preview the number of users that would be synced based on the filters, without actually creating an audience or syncing.
Authentication
Both POST and GET methods require 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.
POST Request (Sync Users)
Creates a Resend audience and adds users.
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
The name for the new Resend audience. Defaults to “Audience [YYYY-MM-DD]”.
An optional description for the new Resend audience.
If true, only includes users considered active within the activeDays period.
If filterActive is true, specifies the number of days to consider for recent activity (default: 30).
POST Response
Indicates if the audience creation and contact addition were successful.
Details of the newly created Resend audience (e.g., id, name).
The number of contacts successfully added to the audience.
A summary message of the operation.
GET Request (Preview Sync)
Checks how many users match the filters without performing the sync.
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
If true, only counts users considered active within the activeDays period.
If filterActive is true, specifies the number of days to consider for recent activity (default: 30).
GET Response
Indicates if the count was successful.
The total number of users matching the specified filters.
An array containing a sample of the first 5 users matching the filters (includes email, full_name, first_name, last_name).
Error Codes
| Status Code | Description |
|---|
| 401 | Invalid or missing marketing API key. |
| 500 | Internal server error (e.g., fetching users failed, Resend API error). |
Code Examples
// Sync active users (last 60 days) to a new audience
const syncActiveUsers = async (audienceDetails, apiKey) => {
const response = await fetch(`https://onlyautomator.com/api/marketing/sync-users`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({ ...audienceDetails, filterActive: true, activeDays: 60 })
});
return await response.json();
};
// Preview how many users have been active in the last 7 days
const previewActiveUsers = async (apiKey) => {
const url = new URL(`https://onlyautomator.com/api/marketing/sync-users`);
url.searchParams.append('filterActive', 'true');
url.searchParams.append('activeDays', '7');
const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
return await response.json();
};
// Example Usage
const marketingApiKey = process.env.MARKETING_API_SECRET;
const audience = { audienceName: "Active Users - Last 60d" };
// syncActiveUsers(audience, marketingApiKey)
// .then(result => console.log('Sync result:', result))
// .catch(err => console.error(err));
previewActiveUsers(marketingApiKey)
.then(result => console.log('Preview result:', result))
.catch(err => console.error(err));
Notes
- Authorization: Uses a dedicated marketing API key, NOT user JWTs.
- POST: Creates a new audience each time it’s called and adds users.
- GET: Useful for checking the scope of a potential sync before executing it.
- Data Format: User data is formatted to match Resend’s contact requirements.
The marketing platform to sync with
Optional list or audience ID for platforms that support multiple lists
The type of sync to perform
Available options:
all,
new,
specific
Array of user IDs to sync (required if sync_type is "specific")
Example:["user1", "user2", "user3"]
Array of tags to apply to the synced users
Example:["onlyautomator-user", "active"]
Sync completed successfully