Rest API
Create or update an audience
const url = 'https://api.unless.com/api/v1/audiences';const options = { method: 'POST', headers: { 'x-api-key': 'example', 'x-user-email': 'example', 'x-account-id': 'example', 'x-website-id': 'example', 'Content-Type': 'application/json' }, body: '{"ruleId":"example","name":"example","description":"example","rule":"example","state":"draft","duration":"session","durationType":"minutes","audienceTags":["example"]}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url https://api.unless.com/api/v1/audiences \ --header 'Content-Type: application/json' \ --header 'x-account-id: example' \ --header 'x-api-key: example' \ --header 'x-user-email: example' \ --header 'x-website-id: example' \ --data '{ "ruleId": "example", "name": "example", "description": "example", "rule": "example", "state": "draft", "duration": "session", "durationType": "minutes", "audienceTags": [ "example" ] }'Create a new audience, or update an existing one by passing its ruleId. An audience is a reusable visitor segment defined by a targeting rule, used to scope personalizations and AI behaviour.
Use cases
- Segmentation: Create audiences for visitor groups you want to target, e.g. returning visitors or visitors from a specific campaign
- Account setup: Provision standard audiences when configuring a new workspace
- Maintenance: Update the rule or name of an existing audience by passing its
ruleId
Notes
- Omit
ruleIdto create a new audience, pass it to update that audience - The
rulefield is a JSON string containing the targeting rule expression - Use the get audiences endpoint to inspect existing audiences and their rule format first
- Every field is overwritten on update except
description, which is kept when omitted
Parameters
Section titled “Parameters”Header Parameters
Section titled “Header Parameters”Your API key from account settings.
The email address of your user.
The ID of your account. You can find this in the account settings.
The ID of the workspace you want to use.
Request Bodyrequired
Section titled “Request Bodyrequired”object
ID of an existing audience to update. Omit to create a new audience.
Display name of the audience.
Who this audience is, in plain words. Omit it to keep the current description, send null or an empty string to clear it.
JSON string containing the targeting rule expression.
State of the audience.
Unit for the duration field.
Tags used to categorise and filter audiences.
Responses
Section titled “Responses”The created or updated audience.
object
object
The account ID that owns this audience.
The website this audience belongs to.
Unique identifier for this audience.
Display name of the audience.
Who this audience is, in plain words. Use it to choose the right audience, for example when restricting content.
Current state of the audience.
JSON string containing the targeting rule expression.
Unit for the duration field.
Unix timestamp (ms) when the audience was created.
Unix timestamp (ms) when the audience was last updated.
Whether the audience is disabled. Disabled audiences are excluded from evaluation.
Whether this audience can be modified.
Tags used to categorise and filter audiences.
Example
{ "audience": { "accountId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "websiteId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "ruleId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "name": "Returning visitors", "description": "Visitors who came back within 30 days.", "state": "live", "rule": "{\"and\":[{\"field\":\"visits\",\"operator\":\"greaterThan\",\"value\":\"1\"}]}", "duration": 30, "durationType": "days", "audienceTags": [ "lifecycle" ] }}Bad request - missing or invalid body.
Unauthorized - invalid API key.