Skip to main content

Fetch Available Variants

Enterprise

This endpoint is part of Countly Enterprise. To get access, contact sales or compare versions. Existing customers can reach the support portal with questions.

Endpoint

/o/sdk?method=ab_fetch_variants

Overview

Fetch available variant names and values for active experiments (documents with status missing or status="running"), optionally filtered by parameter keys.

Authentication

Authentication Methods:

  • App Key (parameter): app_key=YOUR_APP_KEY

Permissions

  • No additional permissions required

Request Parameters

ParameterTypeRequiredDescription
app_keyStringYesApplication key
device_idStringYesDevice identifier used by the SDK request context; required by the live handler.
keysStringNoJSON array of parameter names to filter by (empty array returns all)

Examples

Example 1: Fetch Variants for All Parameters

Request:

curl "https://your-server.com/o/sdk?method=ab_fetch_variants" \
-d "app_key=YOUR_APP_KEY" \
-d "device_id=DEVICE_ID"

Example 2: Fetch Variants for Specific Parameters

Request:

curl "https://your-server.com/o/sdk?method=ab_fetch_variants" \
-d "app_key=YOUR_APP_KEY" \
-d "device_id=DEVICE_ID" \
-d 'keys=["button_text","header_text"]'

Response

Success Response

{
"button_text": [
{
"name": "Control group",
"value": "q"
},
{
"name": "Variant A",
"value": "w"
}
]
}

Response Fields

FieldTypeDescription
*ObjectParameter name to variants array mapping
*.nameStringVariant name
*.valueAnyParameter value for that variant

Error Responses

  • Query error payload:
{
"result": "Error while fetching ab-testing variants."
}

Behavior

  • Handles /o/sdk?method=ab_fetch_variants and reads experiments from countly_out.ab_testing_experiments{appId}.
  • Includes experiments whose status is missing or exactly running; draft and completed experiments are not returned.
  • Parses keys as a JSON array. If omitted, it defaults to an empty array and all eligible experiment parameters are returned.
  • For each experiment, the grouping key is the name of the first parameter in the first variant (variants[0].parameters[0].name).
  • If keys is non-empty, an experiment is included only when that first parameter name is present in keys.
  • For included experiments, the response maps the grouping key to an array of {name, value} objects, one per variant. value is taken from each variant's first parameter.
  • On database errors, returns Error while fetching ab-testing variants.. Invalid keys JSON is not caught by this handler and can fail the request before a normal error payload is produced.

Limitations

  • The live handler requires device_id even though this endpoint is read-only.
  • keys must be a valid JSON array string.
  • This endpoint uses the first parameter in the first variant as the grouping key for each experiment.
Implementation details

Database Collections

CollectionUsed forData touched by this endpoint
countly_out.ab_testing_experiments{appId}Primary:Experiment definitions and variants.