POST
/
api
/
marketing
/
sync-users
Sync Users
curl --request POST \
  --url https://onlyautomator.com/api/marketing/sync-users \
  --header 'Content-Type: application/json' \
  --data '{
  "platform": "mailchimp",
  "list_id": "abc123def456",
  "sync_type": "all",
  "user_ids": [
    "user1",
    "user2",
    "user3"
  ],
  "tags": [
    "onlyautomator-user",
    "active"
  ]
}'
{
  "success": true,
  "sync_id": "sync_01ABC123DEF456",
  "stats": {
    "total": 523,
    "added": 34,
    "updated": 489,
    "removed": 0,
    "errors": 0
  }
}

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.
Authorization
string
required
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
audienceName
string
The name for the new Resend audience. Defaults to “Audience [YYYY-MM-DD]”.
audienceDescription
string
An optional description for the new Resend audience.
filterActive
boolean
If true, only includes users considered active within the activeDays period.
activeDays
number
If filterActive is true, specifies the number of days to consider for recent activity (default: 30).

POST Response

success
boolean
Indicates if the audience creation and contact addition were successful.
audience
object
Details of the newly created Resend audience (e.g., id, name).
contacts_added
number
The number of contacts successfully added to the audience.
message
string
A summary message of the operation.

GET Request (Preview Sync)

Checks how many users match the filters without performing the sync.
Authorization
string
required
Bearer token containing the secret marketing API key. Format: Bearer YOUR_MARKETING_API_SECRET
filterActive
boolean
If true, only counts users considered active within the activeDays period.
activeDays
number
If filterActive is true, specifies the number of days to consider for recent activity (default: 30).

GET Response

success
boolean
Indicates if the count was successful.
total_users
number
The total number of users matching the specified filters.
sample
array
An array containing a sample of the first 5 users matching the filters (includes email, full_name, first_name, last_name).

Error Codes

Status CodeDescription
401Invalid or missing marketing API key.
500Internal 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.

Body

application/json
platform
string
required

The marketing platform to sync with

Example:

"mailchimp"

list_id
string

Optional list or audience ID for platforms that support multiple lists

Example:

"abc123def456"

sync_type
enum<string>
default:all

The type of sync to perform

Available options:
all,
new,
specific
Example:

"all"

user_ids
string[]

Array of user IDs to sync (required if sync_type is "specific")

Example:
["user1", "user2", "user3"]
tags
string[]

Array of tags to apply to the synced users

Example:
["onlyautomator-user", "active"]

Response

Sync completed successfully

success
boolean
Example:

true

sync_id
string
Example:

"sync_01ABC123DEF456"

stats
object