Behavioral Cohorts API

Use the Behavioral Cohorts API to list all your cohorts in Amplitude, export a cohort in Amplitude, or upload a cohort.

Authentication

This API uses basic authentication, using the API key and secret key for your project. Pass base64-encoded credentials in the request header like {api-key}:{secret-key}. api-key replaces username, and secret-key replaces the password.

Your authorization header should look something like this:

--header 'Authorization: Basic YWhhbWwsdG9uQGFwaWdlZS5jb206bClwYXNzdzByZAo'

For more information, see Find your API Credentials

Endpoints

Region Endpoint
Standard server https://amplitude.com/api/3/cohorts
EU residency server https://analytics.eu.amplitude.com/api/3/cohorts

Considerations

  • For Growth and Enterprise plans, the Behavioral Cohorts Download API has a limit of 500 requests per month.
  • Amplitude supports a maximum cohort size of 2 million users. For cohorts larger than this, consider the following options:
  • There's a concurrency limit of 5 requests across cohort downloads and the Dashboard REST API.
  • Cohort Download uses an asynchronous API. Getting a cohort happens in three steps:
    1. Request a single cohort.
    2. Poll the cohort status.
    3. Download the file.
  • There is limit on Cohort Download to request a single cohort: 60 requests per 10 minutes per app, and 4 parallel request per minute per app.

Get all cohorts

Get all discoverable cohorts for an app. Use the id for each cohort returned in the response to get a single cohort.

1curl --location --request GET 'https://amplitude.com/api/3/cohorts' \
2-u '{api_key}:{secret_key}'

1GET /api/3/cohorts HTTP/1.1
2Host: amplitude.com
3Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded

Query parameters

Name Description
includeSyncInfo Optional. Boolean. Set to true to include cohort sync metadata in response (one-time + disabled sync will be excluded) .

Response

The response is a JSON object with this schema:

1{
2 "cohorts": [
3 { COHORT_OBJECT },
4 ...
5 { COHORT_OBJECT },
6 ]
7}

Each COHORT_OBJECT returned has this schema:

1{
2 "appId": integer,
3 "archived": boolean, // whether cohort is archived
4 "definition": { COHORT_DEFINITION }, // Amplitude internal representation of Cohort Definition
5 "description": string,
6 "finished": boolean, // Amplitude internal use to decide whether a training cohort has finished ML training
7 "id": string,
8 "name": string,
9 "owners": string[],
10 "viewers": string[],
11 "published": boolean, // whether cohort is discoverable by other users
12 "size": integer,
13 "type": string, // Amplitude internal representation on different cohort types
14 "lastMod": timestamp, // last modified date
15 "createdAt": timestamp,
16 "lastComputed": timestamp,
17 "hidden": boolean, // Amplitude internal use case to hide a cohort
18 "metadata": string[], // cohort created from funnel/microscope might have this
19 "view_count": integer,
20 "popularity": integer, // cohort created from chart might have this
21 "last_viewed": timestamp,
22 "chart_id": string, // cohort created from chart will have this
23 "edit_id": string, // cohort created from chart will have this
24 "is_predictive": boolean,
25 "is_official_content": boolean,
26 "location_id": string, // cohort created from chart might have this
27 "shortcut_ids": string[],
28 "syncMetadata": COHORT_SYNC_METADATA[]
29}

Each COHORT_SYNC_METADATA has this schema:

1{
2 "target": string,
3 "frequency": string, // support minute (real-time), hourly, daily
4 "last_successful": timestamp,
5 "last_failure": timestamp,
6 "params": { COHORT_SYNC_LEVEL_PARAM }
7}

Below is a sample result:

1"cohorts": [{
2 "appId": 123456,
3 "archived": false,
4 "definition": {
5 "version": 3,
6 "countGroup": {
7 "name": "User",
8 "is_computed": false
9 },
10 "cohortType": "UNIQUES",
11 "andClauses": [{
12 "negated": false,
13 "orClauses": [{
14 "type": "event",
15 "time_type": "rolling",
16 "time_value": 30,
17 "offset": 0,
18 "interval": 1,
19 "type_value": "_active",
20 "operator": ">=",
21 "operator_value": 1,
22 "group_by": [],
23 "metric": null
24 }]
25 }],
26 "referenceFrameTimeParams": {}
27 },
28 "description": "test description",
29 "finished": true,
30 "id": "id_12345",
31 "name": "Test Cohort 1",
32 "owners": [
33 "demo@amplitude.com"
34 ],
35 "viewers": [],
36 "published": true,
37 "size": 111,
38 "type": "redshift",
39 "lastMod": 1679437294,
40 "createdAt": 1679437288,
41 "lastComputed": 1679440233,
42 "hidden": false,
43 "metadata": null,
44 "view_count": null,
45 "popularity": null,
46 "last_viewed": null,
47 "chart_id": null,
48 "edit_id": null,
49 "is_predictive": false,
50 "is_official_content": false,
51 "location_id": null,
52 "shortcut_ids": [],
53 "syncMetadata": [{
54 "target": "braze",
55 "frequency": "hourly",
56 "last_successful": "2023-03-21T16:09:58.848454-07:00",
57 "last_failure": null,
58 "params": {
59 "user_id": "demo@amplitude.com"
60 }
61 }]
62}],

Get one cohort

Get a discoverable cohort using its cohort_id.

This is step one in the download a cohort operation. Use the request_id returned in the response to poll export status.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request/id'
2-u '{api_key}:{secret_key}'

1GET /api/5/cohorts/request/id HTTP/1.1
2Host: amplitude.com
3Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded

Example: Get a cohort with specific properties
This example gets the cohort with ID 26umsb5 and includes the properties Property1 and Property2.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request/26umsb5?props=1&propKeys=Property1&propKeys=Property2'
2--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA='

1GET /api/5/cohorts/request/26umsb5?props=1&propKeys=Property1&propKeys=Property2 HTTP/1.1
2Host: amplitude.com
3Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=

Example: Get cohort with all properties

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request/26umsb5?props=1'
2--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA='

1 
2GET /api/5/cohorts/request/26umsb5?props=1 HTTP/1.1
3Host: amplitude.com
4Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=

Path parameters

Name Description
id Required. Cohort ID.

Query parameters

Name Description
props Optional. Integer. Set to 1 to include user properties in the response object. Set this to 0 or unset if the request keeps timing out.
propKeys Optional. string[]. One or more user properties to include in the response. Add as many propKeys parameters as needed. If left undefined and props=1, response object returns all available user properties.

Response

Requesting a single cohort returns 202 response code with the following JSON object:

1{
2 "request_id": "<request_id>",
3 "cohort_id": "<cohort_id>"
4}

If your authorization or the cohort_id is invalid, the request returns an error.

Get request status

Poll the request status using the request_id retrieved for the cohort. This is the second phase in a cohort download operation.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request-status/:request_id' \
2-u '{api_key}:{secret_key}''

1GET /api/5/cohorts/request-status/:request_id HTTP/1.1
2Host: amplitude.com
3Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded

Example: Get the status of a request
This example gets the status of request with the ID qfaZya.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request-status/qfaZya' \
2--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA='

1GET /api/5/cohorts/request-status/qfaZya HTTP/1.1
2Host: amplitude.com
3Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=

Path parameters

Name Description
request_id Required. The request ID retrieved with the get one cohort request.

Response

If the job is still running, polling the request status returns a 202 code and the async_status 'JOB INPROGRESS'.

1{
2 "request_id": "<request_id>",
3 "cohort_id": "<cohort_id>",
4 "async_status": "JOB INPROGRESS"
5}

If the job has finished running, polling the request status returns a 200 code and the async_status 'JOB COMPLETED'.

1{
2 "request_id": "<request_id>",
3 "cohort_id": "<cohort_id>",
4 "async_status": "JOB COMPLETED"
5}

Download cohort

When the job has finished running, download the cohort.

This is a basic request.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request/:requestId/file' \
2-u '{api_key}:{secret_key}'

1GET /api/5/cohorts/request/requestId/file HTTP/1.1
2Host: amplitude.com
3Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded

Example: Download a requested cohort
This request downloads the file for request ID Sf7M9j.

1curl --location --request GET 'https://amplitude.com/api/5/cohorts/request/Sf7M9j/file'
2--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA='

1GET /api/5/cohorts/request/Sf7M9j/file HTTP/1.1
2Host: amplitude.com
3Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=

Path parameters

Name Description
request_id Required. The request ID retrieved with the get one cohort request.
  • For small cohorts, the response body contains the cohort data.
  • For large cohorts, you must download the data. If the cohort is large, the response redirects with a 302 response code to a pre-signed Amazon S3 download URL. The download URL is valid for one minute, access it immediately.
  • The API request link (https://amplitude.com/api/5/cohorts/request/:requestId/file) is valid for seven days. During the seven days, you can make the same request to get a new S3 download link. Each S3 link is valid for one minute.
  • Most clients used to send API requests automatically download the data from the S3 link. If your API client doesn't automatically download the cohort from the S3 link, you have one minute access it manually.

Upload cohort

Generate a new cohort or update an existing cohort by uploading a set of User IDs or Amplitude IDs. This is a basic request example with placeholder values.

1curl --location --request POST 'https://amplitude.com/api/3/cohorts/upload' \
2--header 'Content-Type: application/json' \
3-u '{api_key}:{secret_key}''
4--data-raw '{
5 "name": "Cohort Name",
6 "app_id": amplitude_project,
7 "id_type": "BY_AMP_ID",
8 "ids": [
9 "amplitude_id",
10 "amplitude_id"
11 ],
12 "owner": "cohort_owner",
13 "published": true
14}'

1POST /api/3/cohorts/upload HTTP/1.1
2Host: amplitude.com
3Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded
4Content-Type: application/json
5Content-Length: 201
6 
7{
8"name": "Cohort Name",
9"app_id": amplitude_project,
10"id_type": "BY_AMP_ID",
11"ids": [
12 "amplitude_id",
13 "amplitude_id"
14],
15"owner": "cohort_owner",
16"published": true
17}

Example: Create a new cohort
This example creates a new cohort named "New Cohort" and includes the Amplitude IDs 10101010101010ID1, and 00000010101010ID2.

1curl --location --request POST 'https://amplitude.com/api/3/cohorts/upload' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=' \
4-u '{api_key}:{secret_key}' \
5--data-raw '{
6 "name": "New Cohort",
7 "app_id": 153957,
8 "id_type": "BY_AMP_ID",
9 "ids": [
10 "10101010101010ID1",
11 "00000010101010ID2"
12 ],
13 "owner": "datamonster@amplitude.com",
14 "published": true
15}'

1POST /api/3/cohorts/upload HTTP/1.1
2Host: amplitude.com
3Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=
4Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded
5Content-Type: application/json
6Content-Length: 280
7 
8{
9 "name": "New Cohort",
10 "app_id": 153957,
11 "id_type": "BY_AMP_ID",
12 "ids": [
13 "10101010101010ID1",
14 "00000010101010ID2"
15 ],
16 "owner": "datamonster@amplitude.com",
17 "published": true
18}

Body parameters

Parameter
Description
name Required. String. A name for the cohort.
app_id Required. Integer. An identifier for the Amplitude project containing the cohort.
id_type Required. String. The kind of ID sent in the ids field. Valid options are BY_AMP_ID or BY_USER_ID.
ids Required. String[]. One or more user or Amplitude IDs to include in the cohort. Specify the ID type in the id_type field.
owner Required. String. The login email of the cohort's owner in Amplitude.
published Required. Boolean. Whether the cohort is discoverable or hidden.
skip_save Optional. Boolean. Set to true if you want to validate the upload without saving. Default is false.
skip_invalid_ids Optional. Boolean. Setting skip_invalid_ids to true skips invalid IDs and upload the remaining valid IDs. Setting this parameter to false ends the upload if the request has invalid IDs. Default is true.
existing_cohort_id Optional. String. The ID of an existing cohort. This replaces the contents for the specified cohort with the IDs uploaded in the request. For example, '1a2bc3d' is your cohort's ID, found in the cohort's URL. https://analytics.amplitude.com/accountname/cohort/**1a2bc3d**

Response

The response is a JSON object with this schema:

1{
2 "cohortId": "COHORT_ID",
3 "metadata": {
4 "matched": 1234,
5 "totals": 1232,
6 "invalid_ids_sample": ["INVALID_ID1", "INVALID_ID2"]
7 }
8}

Response errors

Parameter Type Description
error error json Error details.

Upload cohort error response JSON

Parameter Description
http_code Integer. Provides the HTTP error, if available.
type String. Describes the type of error.
message String. Describes the error.
metadata JSON object. Describes in more detail the cause of the error. For example, which user ID values are invalid.

Update cohort membership

Add and remove IDs to incrementally update existing cohort membership.

1curl --location --request POST 'https://amplitude.com/api/3/cohorts/membership' \
2--header 'Content-Type: application/json' \
3-u '{api_key}:{secret_key}' \
4--data-raw '{
5 "cohort_id": "COHORT_ID",
6 "memberships": [
7 {
8 "ids": [
9 "ID",
10 "ID"
11 ],
12 "id_type": "BY_ID",
13 "operation": "ADD"
14 },
15 {
16 "ids": [
17 "ID",
18 "ID"
19 ],
20 "id_type": "BY_ID",
21 "operation": "REMOVE"
22 },
23 {
24 "ids": [
25 "name",
26 "name"
27 ],
28 "id_type": "BY_NAME",
29 "operation": "ADD"
30 }
31 ],
32 "skip_invalid_ids": true
33}

1 
2POST /api/3/cohorts/membership HTTP/1.1
3Host: amplitude.com
4Content-Type: application/json
5Authorization: Basic {api-key}:{secret-key} #credentials must be base64 encoded
6Content-Length: 362
7 
8{
9"cohort_id":"COHORT_ID",
10"memberships": [
11 {
12 "ids" : ["ID","ID"],
13 "id_type" : "BY_ID",
14 "operation" : "ADD"
15 },
16 {
17 "ids" : ["ID","ID"],
18 "id_type" : "BY_ID",
19 "operation" : "REMOVE"
20 },
21 {
22 "ids" : ["name","name"],
23 "id_type" : "BY_NAME",
24 "operation" : "ADD"
25 }
26 ],
27"skip_invalid_ids":true,
28}

Example: Remove and add cohort members
This example adds IDs 111 and 222 by ID, removes IDs 333 and 444 by ID, and removes IDs asd and qwe by name from the the cohort with ID 1a2bc3d. The operation is set to skip invalid IDs.

1curl --location --request POST 'https://amplitude.com/api/3/cohorts/membership' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=' \
4--data-raw '{
5"cohort_id":"1a2bc3d",
6"memberships": [
7 {
8 "ids" : ["111","222"],
9 "id_type" : "BY_ID",
10 "operation" : "ADD"
11 },
12 {
13 "ids" : ["333","444"],
14 "id_type" : "BY_ID",
15 "operation" : "REMOVE"
16 },
17 {
18 "ids" : ["asd","qwe"],
19 "id_type" : "BY_NAME",
20 "operation" : "ADD"
21 }
22 ],
23"skip_invalid_ids":true,
24}'

1 
2POST /api/3/cohorts/membership HTTP/1.1
3Host: amplitude.com
4Content-Type: application/json
5Authorization: Basic MTIzNDU2NzgwMDoxMjM0NTY3MDA=
6Content-Length: 362
7 
8{
9"cohort_id":"1a2bc3d",
10"memberships": [
11 {
12 "ids" : ["111","222"],
13 "id_type" : "BY_ID",
14 "operation" : "ADD"
15 },
16 {
17 "ids" : ["333","444"],
18 "id_type" : "BY_ID",
19 "operation" : "REMOVE"
20 },
21 {
22 "ids" : ["asd","qwe"],
23 "id_type" : "BY_NAME",
24 "operation" : "ADD"
25 }
26 ],
27"skip_invalid_ids":true,
28}

Perform incremental update (add / remove) to existing cohort membership.

Request body

Parameter Description
cohort_id Required. String. The ID of an existing cohort. This updates the membership for the specified cohort with the IDs being uploaded in this request.
count_group Optional. String. The count group of the given IDs. This must be the same as the cohort’s existing count group. Count_group defaults to User.
memberships Required. List of membership json An array of JSON objects identifying IDs to add or remove.
skip_invalid_ids Optional. Boolean. Setting this parameter to false ends the request without updating cohort membership if the request has invalid IDs. Setting skip_invalid_ids to true skips invalid IDs while applying the remaining valid ids. Default is true.

Memberships request JSON

Parameter Description
ids Required. String[]. List of IDs to add or remove.
id_type Required. String. The kind of ID sent in the ids field. Valid options are: - BY_ID - BY_NAME For User count_group, BY_ID is amplitude ID and BY_NAME is user ID. For any other count_group, BY_ID is group ID and BY_NAME is group name.
operation Required. String. The operation to apply on ids field. Valid options are: ADD and REMOVE

Response

Parameter Description
cohort_id String. The ID of an existing cohort for which the membership information was updated.
memberships_result List of memberships_result json. An array of JSON objects identifying result of membership update (add or remove) operation.

Memberships response JSON

Parameter Description
skipped_ids List of strings. List of skipped IDs in the membership operation entry.
id_type String. The kind of ID sent for the ids field in this membership operation entry.
operation String. The operation applied on ids field in this membership operation entry
Was this page helpful?

Thanks for your feedback!

May 21st, 2024

Need help? Contact Support

Visit Amplitude.com

Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.