post https://onesignal.com/api/v1/apps/:/segments
Create segments visible and usable in the dashboard and API
Requires Growth Plan or higher
The Create Segment method is used when you want your server to programmatically create a segment instead of using the OneSignal Dashboard UI. Just like creating Segments from the dashboard you can pass in filters
with multiple "AND"
or "OR"
operator
's.
Does Not Update Segments
This endpoint will only create segments, it does not edit or update currently created Segments. You will need to use the Delete Segments endpoint and re-create it with this endpoint to edit.
Example Code - Create Segments
curl --include --request POST \
--header "Content-Type: application/json; charset=utf-8" \
--header "Authorization: Basic YOUR_REST_API_KEY" \
--data-binary "{\"name\": \"SEGMENT_NAME\", \"filters\": [{\"field\": \"session_count\", \"relation\": \">\", \"value\": \"1\"},{\"operator\": \"OR\"},{\"field\": \"tag\", \"key\": \"TAG_KEY\", \"relation\": \"exists\"},{\"operator\": \"OR\"},{\"field\": \"last_session\", \"relation\": \"<\", \"value\": \"1\"}]}" \
https://onesignal.com/api/v1/apps/YOUR_APP_ID/segments
// single line example
curl --include --request POST --header "Content-Type: application/json; charset=utf-8" --header "Authorization: Basic YOUR_REST_API_KEY" --data-binary "{\"name\": \"SEGMENT_NAME\", \"filters\": [{\"field\": \"tag\", \"key\": \"TAG_KEY\", \"relation\": \"=\", \"value\": \"true\"},{\"operator\": \"AND\"},{\"field\": \"tag\", \"key\": \"TAG_KEY2\", \"relation\": \"exists\"}]}" https://onesignal.com/api/v1/apps/YOUR_APP_ID/segments
{
"name": "Segment 2",
"filters": [
{"field": "session_count", "relation": ">", "value": "1"},
{"operator": "AND"},
{"field": "tag", "relation": "!=", "key": "tag_key", "value": "1"},
{"operator": "OR"},
{"field": "last_session", "relation": "<", "hours_ago": "30"}
]
}
//Another Example
{
"name": "iOS, Android, Web",
"filters": [
{"field": "device_type", "relation": "=", "value": "iOS"},
{"operator": "OR"},
{"field": "device_type", "relation": "=", "value": "Android"},
{"operator": "OR"},
{"field": "device_type", "relation": "=", "value": "Web Push (Browser)"}
]
}
Result Format - Create Segments
// Always true, "id" is uuid of created segment
{"success": true, "id": "7ed2887d-bd24-4a81-8220-4b256a08ab19"}
{"success": false, "errors": ["Segment with the given id already exists."]}
//Always false, error will explain why request failed
{"success": false, "errors": ["name is required"]}