State & Variables
1. Overview
State is the hierarchical data tree available to ResultFly campaigns, composed of declared business variables plus system-managed fields. This reference describes the APIs for defining variables and interacting with State at runtime. For concepts, see Overview → State in ResultFly and Developers → State Model in ResultFly.
2. Authentication
- Variable definition endpoints require admin or server-to-server credentials. Follow Integration Basics → Authentication for key provisioning.
- Runtime state endpoints use user-context JWTs (client-managed or ResultFly-managed). See Integration Basics → User Tokens for issuance details.
3. Endpoints
3.1 POST /v1/campaigns/{campaign_id}/variables:define
Declare a variable for a campaign.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
scope | enum(user, campaign, app) | ✅ | Variable scope. |
name | string | ✅ | Variable name (unique per scope). |
type | enum(number, string, boolean) | ✅ | Data type. |
default | match type | ❌ | Initial value applied when absent. |
description | string | ❌ | Human-readable note for operators. |
Behavior
- Variable becomes available under
state.{scope}.{name}once defined. - If the scope already has the maximum allowed variables, ResultFly returns a configuration error.
Response example
{
"status": "defined",
"variable": {
"scope": "user",
"name": "loyalty_points",
"type": "number",
"default": 0
}
}3.2 GET /v1/campaigns/{campaign_id}/variables
List declared variables for a campaign (all scopes).
Response example
{
"variables": [
{ "scope": "user", "name": "loyalty_points", "type": "number", "default": 0 },
{ "scope": "campaign", "name": "tier", "type": "string", "default": "bronze" },
{ "scope": "app", "name": "round_score", "type": "number" }
]
}3.3 GET /v1/runtime/state
Return the current values of declared variables for the authenticated user context.
Response example
{
"state": {
"user": {
"loyalty_points": 1200,
"daily_attempts": 1
},
"campaign": {
"tier": "silver"
},
"app": {
"round_score": 35
}
}
}System fields (such as sessionId) are excluded from this endpoint.
3.4 POST /v1/runtime/state:mutate
Mutate declared variables for the authenticated user context.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
ops | array | ✅ | Each entry describes one mutation. |
ops[].path | string | ✅ | Variable path (for example state.user.loyalty_points). |
ops[].op | enum(set, inc, dec) | ✅ | Mutation type. |
ops[].value | number/string/boolean | ✅ | Value applied; type must match declaration. |
mutation_id | string | ❌ | Optional idempotency key to deduplicate retries. |
Behavior
- Each operation must target a declared variable; referencing
state.sessionIdor any system field results in an error. setassigns the provided value,inc/decadjust numeric values.- If
mutation_idrepeats, ResultFly returns the original outcome without reapplying changes.
Response example
{
"status": "applied",
"mutations": [
{ "path": "state.user.loyalty_points", "previous": 1200, "current": 1250 },
{ "path": "state.app.round_score", "previous": 35, "current": 0 }
]
}4. Error Handling
- Validation errors — missing fields, type mismatches, or invalid scopes. These return
422 Unprocessable Entitywith details. - Scope errors — attempting to exceed per-scope variable limits or mutate a variable outside the authenticated campaign/app context results in
403 Forbiddenor409 Conflictdepending on policy. - Idempotency behavior — when
mutation_idrepeats, the API returns the original response (statusduplicate) without reapplying changes. - System State violations — writing to system fields or undeclared variables returns
400 Bad Requestwith the offending path; callers must fix configuration before retrying.
Last updated on