Contexts are people, services, machines, or other resources that encounter feature flags in your product. Contexts are identified by their kind
, which describes the type of resources encountering flags, and by their key
. Each unique combination of one or more contexts that have encountered a feature flag in your product is called a context instance.
When you use the LaunchDarkly SDK to evaluate a flag, you provide a context to that call. LaunchDarkly records the key and attributes of each context instance. You can view these in the LaunchDarkly user interface from the Contexts list, or use the Context APIs. To learn more, read Contexts.
LaunchDarkly provides APIs for you to:
To learn more about context kinds, read Context kinds.
Contexts are always scoped within a project and an environment. Each environment has its own set of context instance records.
Several of the endpoints in the contexts API require a context instance ID or application ID. Both of these IDs are returned as part of the Search for context instances response. The context instance ID is the id
field of each element in the items
array. The application ID is the applicationId
field of each element in the items
array. By default, the application ID is set to the SDK you are using. In the LaunchDarkly UI, the application ID and application version appear on the context details page in the "From source" field. You can change the application ID as part of your SDK configuration. To learn more, read Application metadata configuration.
When you search for contexts or context instances, you can filter on certain fields using the filter
parameter either as a query parameter or as a request body parameter.
The filter
parameter supports the following operators: after
, anyOf
, before
, contains
, equals
, exists
, notEquals
, startsWith
.
Returns contexts or context instances if any of the values in a field, which should be dates, are after the provided time. For example:
myField after "2022-09-21T19:03:15+00:00"
Returns contexts or context instances if any of the values in a field match any of the values in the match value. For example:
myField anyOf [44]
myField anyOf ["phone","tablet"]
myField anyOf [true]"
Returns contexts or context instances if any of the values in a field, which should be dates, are before the provided time. For example:
myField before "2022-09-21T19:03:15+00:00"
Returns contexts or context instances if all the match values are found in the list of values in this field. For example:
myListField contains 44
myListField contains ["phone","tablet"]
myListField contains true
Returns contexts or context instances if there is an exact match on the entire field. For example:
myField equals 44
myField equals "device"
myField equals true
myField equals [1,2,3,4]
myField equals ["hello","goodbye"]
Returns contexts or context instances if the field matches the specified existence. For example:
myField exists true
myField exists false
*.name exists true
Returns contexts or context instances if there is not an exact match on the entire field. For example:
myField notEquals 44
myField notEquals "device"
myField notEquals true
myField notEquals [1,2,3,4]
myField notEquals ["hello","goodbye"]
Returns contexts or context instances if the value in a field, which should be a singular string, begins with the provided substring. For example:
myField startsWith "do"
You can also combine filters in the following ways:
,
) as an AND operator|
) as an OR operator()
to group filtersFor example:
myField notEquals 0, myField notEquals 1
returns contexts or context instances where myField
is not 0 and is not 1myFirstField equals "device",(mySecondField equals "iPhone"|mySecondField equals "iPad")
returns contexts or context instances where myFirstField
is equal to "device" and mySecondField
is equal to either "iPhone" or "iPad"You can only filter certain fields in contexts and context instances when using the filter
parameter. Additionally, you can only filter some fields with certain operators.
When you search for contexts, the filter
parameter supports the following fields and operators:
Field |
Description | Supported operators |
---|---|---|
applicationId |
An identifier representing the application where the LaunchDarkly SDK is running. | equals , notEquals , anyOf |
id |
Unique identifier for the context. | equals , notEquals , anyOf |
key |
The context key. | equals , notEquals , anyOf , startsWith |
kind |
The context kind. | equals , notEquals , anyOf |
kinds |
A list of all kinds found in the context. Supply a list of strings to the operator. | equals , anyOf , contains |
kindKey |
The kind and key for the context. They are joined with : , for example, user:user-key-abc123 . |
equals , notEquals , anyOf |
kindKeys |
A list of all kinds and keys found in the context. The kind and key are joined with : , for example, user:user-key-abc123 . Supply a list of strings to the operator. |
equals , anyOf , contains |
q |
A "fuzzy" search across context attribute values and the context key. Supply a string or list of strings to the operator. | equals |
name |
The name for the context. | equals , notEquals , exists , anyOf , startsWith |
<a kind>.<an attribute name> |
A kind and the name of any attribute that appears in a context of that kind, for example, user.email . To filter all kinds, use * in place of the kind, for example, *.email . You can use either a literal attribute name or a JSON path to specify the attribute. If you use a JSON path, then you must escape the / character, using ~1 , and the ~ character, using ~0 . For example, use user.job/title or user./job~1title to filter the /job/title field in a user context kind. If the field or value includes whitespace, it should be enclosed in double quotes. |
equals , notEquals , exists , startsWith , before , after . |
When searching for context instances, the filter
parameter supports the following fields and operators
Field |
Description | Supported operators |
---|---|---|
applicationId |
An identifier representing the application where the LaunchDarkly SDK is running. | equals , notEquals , anyOf |
id |
Unique identifier for the context instance. | equals , notEquals , anyOf |
kinds |
A list of all kinds found in the context instance. Supply a list of strings to the operator. | equals , anyOf , contains |
kindKeys |
A list of all kinds and keys found in the context instance. The kind and key are joined with : , for example, user:user-key-abc123 . Supply a list of strings to the operator. |
equals , anyOf , contains |
Get all context kinds for a given project.
Context kinds collection response
Invalid access token
Forbidden
Invalid resource identifier
{- "items": [
- {
- "key": "organization-key-123abc",
- "name": "Organization",
- "description": "An example context kind, to enable targeting based on organization",
- "version": 4,
- "creationDate": 0,
- "lastModified": 0,
- "lastSeen": 0,
- "createdFrom": "string",
- "hideInTargeting": false,
- "archived": false,
- "_links": {
- "property1": {
- "href": "string",
- "type": "string"
}, - "property2": {
- "href": "string",
- "type": "string"
}
}
}
], - "_links": {
- "property1": {
- "href": "string",
- "type": "string"
}, - "property2": {
- "href": "string",
- "type": "string"
}
}
}
Create or update a context kind by key. Only the included fields will be updated.
Context kind upsert response
Invalid request
Invalid access token
Forbidden
Invalid resource identifier
{- "name": "organization",
- "description": "An example context kind for organizations",
- "hideInTargeting": false,
- "archived": false,
- "version": 1
}
{- "status": "success",
- "_links": {
- "property1": {
- "href": "string",
- "type": "string"
}, - "property2": {
- "href": "string",
- "type": "string"
}
}
}
Get context attribute names.
filter | string <string> A comma-separated list of context filters. This endpoint only accepts |
limit | integer <int64> Specifies the maximum number of items in the collection to return (max: 100, default: 100) |
Context attribute names collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "items": [
- {
- "kind": "user",
- "names": [
- {
- "name": "/firstName",
- "weight": 2225,
- "redacted": false
}
]
}
]
}
Get context attribute values.
filter | string <string> A comma-separated list of context filters. This endpoint only accepts |
limit | integer <int64> Specifies the maximum number of items in the collection to return (max: 100, default: 50) |
Context attribute values collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "items": [
- {
- "kind": "user",
- "values": [
- {
- "name": "Sandy",
- "weight": 35
}
]
}
]
}
Search for context instances.
You can use either the query parameters or the request body parameters. If both are provided, there is an error.
To learn more about the filter syntax, read Filtering contexts and context instances. To learn more about context instances, read Context instances.
limit | integer <int64> Specifies the maximum number of items in the collection to return (max: 50, default: 20) |
continuationToken | string <string> Limits results to context instances with sort values after the value specified. You can use this for pagination, however, we recommend using the |
sort | string <string> Specifies a field by which to sort. LaunchDarkly supports sorting by timestamp in ascending order by specifying |
filter | string <string> A comma-separated list of context filters. This endpoint only accepts an |
includeTotalCount | boolean Specifies whether to include or omit the total count of matching context instances. Defaults to true. |
Context instances collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "filter": "{\"filter\": \"kindKeys:{\"contains\": [\"user:Henry\"]},\"sort\": \"-ts\",\"limit\": 50}",
- "sort": "-ts",
- "limit": 10,
- "continuationToken": "QAGFKH1313KUGI2351"
}
{- "_links": {
- "next": {
- "href": "/api/v2/projects/my-project/environments/my-env/context-instances/organization:launch-darkly:user:henry?limit=2&continuationToken=2022-04-15T15:00:57.526470334Z",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-proj/environments/my-env/context-instances/organization:launch-darkly:user:henry-jacobs?limit=2",
- "type": "application/json"
}
}, - "totalCount": 100,
- "_environmentId": "57be1db38b75bf0772d11384",
- "continuationToken": "QAGFKH1313KUGI2351",
- "items": [
- {
- "lastSeen": "2022-04-15T15:00:57.526470334Z",
- "id": "b3JnOmxhdW5jaGRhcmtseQ",
- "applicationId": "GoSDK/1.2",
- "anonymousKinds": [
- "device",
- "privateKind"
], - "context": "{\"kind\": \"user\", \"key\": \"context-key-123abc\", \"name\": \"Sandy Smith\", \"email\": \"sandy@example.com\"}",
- "_links": {
- "parent": {
- "href": "/api/v2/projects/my-project/environments/my-environment",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-project/environments/my-env/context-instances/organization:launch-darkly:user:henry?filter=applicationId:\"GoSDK/1.2\"",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/context-instances/organization:launch-darkly:user:henry",
- "type": "text/html"
}
}, - "_access": {
- "denied": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
], - "allowed": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
]
}
}
]
}
Get context instances by ID.
limit | integer <int64> Specifies the maximum number of context instances to return (max: 50, default: 20) |
continuationToken | string <string> Limits results to context instances with sort values after the value specified. You can use this for pagination, however, we recommend using the |
sort | string <string> Specifies a field by which to sort. LaunchDarkly supports sorting by timestamp in ascending order by specifying |
filter | string <string> A comma-separated list of context filters. This endpoint only accepts an |
includeTotalCount | boolean Specifies whether to include or omit the total count of matching context instances. Defaults to true. |
Context instances collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "_links": {
- "next": {
- "href": "/api/v2/projects/my-project/environments/my-env/context-instances/organization:launch-darkly:user:henry?limit=2&continuationToken=2022-04-15T15:00:57.526470334Z",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-proj/environments/my-env/context-instances/organization:launch-darkly:user:henry-jacobs?limit=2",
- "type": "application/json"
}
}, - "totalCount": 100,
- "_environmentId": "57be1db38b75bf0772d11384",
- "continuationToken": "QAGFKH1313KUGI2351",
- "items": [
- {
- "lastSeen": "2022-04-15T15:00:57.526470334Z",
- "id": "b3JnOmxhdW5jaGRhcmtseQ",
- "applicationId": "GoSDK/1.2",
- "anonymousKinds": [
- "device",
- "privateKind"
], - "context": "{\"kind\": \"user\", \"key\": \"context-key-123abc\", \"name\": \"Sandy Smith\", \"email\": \"sandy@example.com\"}",
- "_links": {
- "parent": {
- "href": "/api/v2/projects/my-project/environments/my-environment",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-project/environments/my-env/context-instances/organization:launch-darkly:user:henry?filter=applicationId:\"GoSDK/1.2\"",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/context-instances/organization:launch-darkly:user:henry",
- "type": "text/html"
}
}, - "_access": {
- "denied": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
], - "allowed": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
]
}
}
]
}
Delete context instances by ID.
Action succeeded
Invalid request
Invalid access token
Forbidden
Invalid resource identifier
Rate limited
{- "code": "invalid_request",
- "message": "Invalid request body"
}
Search for contexts.
You can use either the query parameters or the request body parameters. If both are provided, there is an error.
To learn more about the filter syntax, read Filtering contexts and context instances. To learn more about contexts, read Contexts and context kinds.
limit | integer <int64> Specifies the maximum number of items in the collection to return (max: 50, default: 20) |
continuationToken | string <string> Limits results to contexts with sort values after the value specified. You can use this for pagination, however, we recommend using the |
sort | string <string> Specifies a field by which to sort. LaunchDarkly supports sorting by timestamp in ascending order by specifying |
filter | string <string> A comma-separated list of context filters. To learn more about the filter syntax, read Filtering contexts and context instances. |
includeTotalCount | boolean Specifies whether to include or omit the total count of matching contexts. Defaults to true. |
Contexts collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "filter": "*.name startsWith Jo,kind anyOf [\"user\",\"organization\"]",
- "sort": "-ts",
- "limit": 10,
- "continuationToken": "QAGFKH1313KUGI2351"
}
{- "_links": {
- "next": {
- "href": "/app.launchdarkly.com/api/v2/projects/my-project/environments/my-environment/contexts?filter=kind:{\"equals\": [\"organization\"]}&limit=2&continuationToken=QAGFKH1313KUGI2351",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-proj/environments/my-env/contexts?filter=kind:{\"equals\": [\"organization\"]}&limit=2&continuationToken=QAGFKH1313KUGI2351",
- "type": "application/json"
}
}, - "totalCount": 100,
- "_environmentId": "57be1db38b75bf0772d11384",
- "continuationToken": "QAGFKH1313KUGI2351",
- "items": [
- {
- "lastSeen": "2022-04-15T15:00:57.526470334Z",
- "applicationId": "GoSDK/1.2",
- "context": "{\"kind\": \"user\", \"key\": \"context-key-123abc\", \"name\": \"Sandy Smith\", \"email\": \"sandy@example.com\"}",
- "_links": {
- "parent": {
- "href": "/api/v2/projects/my-project/environments/my-environment",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-project/environments/my-env/contexts/organization:launch-darkly:user:henry?filter=applicationId:\"GoSDK/1.2\"",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/context/organization:launch-darkly:user:henry",
- "type": "text/html"
}
}, - "_access": {
- "denied": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
], - "allowed": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
]
}, - "associatedContexts": 0
}
]
}
Get contexts based on kind and key.
limit | integer <int64> Specifies the maximum number of items in the collection to return (max: 50, default: 20) |
continuationToken | string <string> Limits results to contexts with sort values after the value specified. You can use this for pagination, however, we recommend using the |
sort | string <string> Specifies a field by which to sort. LaunchDarkly supports sorting by timestamp in ascending order by specifying |
filter | string <string> A comma-separated list of context filters. This endpoint only accepts an |
includeTotalCount | boolean Specifies whether to include or omit the total count of matching contexts. Defaults to true. |
Contexts collection response
Invalid request
Invalid access token
Forbidden
Rate limited
{- "_links": {
- "next": {
- "href": "/app.launchdarkly.com/api/v2/projects/my-project/environments/my-environment/contexts?filter=kind:{\"equals\": [\"organization\"]}&limit=2&continuationToken=QAGFKH1313KUGI2351",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-proj/environments/my-env/contexts?filter=kind:{\"equals\": [\"organization\"]}&limit=2&continuationToken=QAGFKH1313KUGI2351",
- "type": "application/json"
}
}, - "totalCount": 100,
- "_environmentId": "57be1db38b75bf0772d11384",
- "continuationToken": "QAGFKH1313KUGI2351",
- "items": [
- {
- "lastSeen": "2022-04-15T15:00:57.526470334Z",
- "applicationId": "GoSDK/1.2",
- "context": "{\"kind\": \"user\", \"key\": \"context-key-123abc\", \"name\": \"Sandy Smith\", \"email\": \"sandy@example.com\"}",
- "_links": {
- "parent": {
- "href": "/api/v2/projects/my-project/environments/my-environment",
- "type": "application/json"
}, - "self": {
- "href": "/api/v2/projects/my-project/environments/my-env/contexts/organization:launch-darkly:user:henry?filter=applicationId:\"GoSDK/1.2\"",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/context/organization:launch-darkly:user:henry",
- "type": "text/html"
}
}, - "_access": {
- "denied": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
], - "allowed": [
- {
- "action": "string",
- "reason": {
- "resources": [
- "proj/*:env/*;qa_*:/flag/*"
], - "notResources": [
- null
], - "actions": [
- "*"
], - "notActions": [
- null
], - "effect": "allow",
- "role_name": "string"
}
}
]
}, - "associatedContexts": 0
}
]
}
Evaluate flags for a context instance, for example, to determine the expected flag variation. Do not use this API instead of an SDK. The LaunchDarkly SDKs are specialized for the tasks of evaluating feature flags in your application at scale and generating analytics events based on those evaluations. This API is not designed for that use case. Any evaluations you perform with this API will not be reflected in features such as flag statuses and flag insights. Context instances evaluated by this API will not appear in the Contexts list. To learn more, read Comparing LaunchDarkly's SDKs and REST API.
LaunchDarkly supports the filter
query param for filtering, with the following fields:
query
filters for a string that matches against the flags' keys and names. It is not case sensitive. For example: filter=query equals dark-mode
.tags
filters the list to flags that have all of the tags in the list. For example: filter=tags contains ["beta","q1"]
.You can also apply multiple filters at once. For example, setting filter=query equals dark-mode, tags contains ["beta","q1"]
matches flags which match the key or name dark-mode
and are tagged beta
and q1
.
Flag evaluation collection response
Invalid request
Invalid access token
Forbidden
Invalid resource identifier
Rate limited
{- "key": "user-key-123abc",
- "kind": "user",
- "otherAttribute": "other attribute value"
}
{- "items": [
- {
- "_links": {
- "self": {
- "href": "/api/v2/projects/{projectKey}/environments/{environmentKey}/flags/evaluate",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/features/sort.order/targeting",
- "type": "text/html"
}
}, - "_value": true,
- "key": "sort.order",
- "name": "SortOrder",
- "reason": {
- "kind": "FALLTHROUGH"
}
}, - {
- "_links": {
- "self": {
- "href": "/api/v2/projects/{projectKey}/environments/{environmentKey}/flags/evaluate",
- "type": "application/json"
}, - "site": {
- "href": "/my-project/my-environment/features/alternate.page/targeting",
- "type": "text/html"
}
}, - "_value": false,
- "key": "alternate.page",
- "name": "AlternatePage",
- "reason": {
- "kind": "RULE_MATCH",
- "ruleID": "b2530cdf-14c6-4e16-b660-00239e08f19b",
- "ruleIndex": 1
}
}
], - "totalCount": 2,
- "_links": {
- "self": {
- "href": "/api/v2/projects/{projectKey}/environments/{environmentKey}/flags/evaluate",
- "type": "application/json"
}
}
}