Contexts
Provide user and request information for flag evaluation and targeting
A context is the set of attributes you provide to the SDK that describes the current user or request. Flagpool uses the context to evaluate targeting rules and determine rollout bucketing.
Setting context
Context is provided when initializing the SDK and can be updated at any time:
const client = new FlagpoolClient({
projectId: 'your-project-id',
apiKey: 'your-api-key',
decryptionKey: 'your-decryption-key',
context: {
userId: 'user-123',
email: 'jane@acme.com',
plan: 'enterprise',
country: 'US',
},
})
<FlagpoolProvider
projectId="your-project-id"
apiKey="your-api-key"
decryptionKey="your-decryption-key"
context={{
userId: 'user-123',
email: 'jane@acme.com',
plan: 'enterprise',
country: 'US',
}}
>
<App />
</FlagpoolProvider>
client = FlagpoolClient(
project_id="your-project-id",
api_key="your-api-key",
decryption_key="your-decryption-key",
context={
"userId": "user-123",
"email": "jane@acme.com",
"plan": "enterprise",
"country": "US",
},
)
Context attributes
A context is a flat key-value object. You can include any attributes that are relevant to your targeting rules.
Common attributes
| Attribute | Type | Purpose |
|---|---|---|
userId | string | Unique user identifier — required for deterministic rollouts |
email | string | User email — useful for contains rules (e.g., @company.com) |
plan | string | Subscription tier — "free", "pro", "enterprise" |
country | string | Country code — "US", "DE", "JP" |
organizationId | string | Organization or team identifier |
Custom attributes
You can add any business-relevant attributes:
{
userId: 'user-123',
plan: 'pro',
role: 'admin',
department: 'engineering',
loginCount: 42,
platform: 'ios',
appVersion: '2.4.1',
createdAt: '2024-01-15',
}
These attributes can all be referenced in targeting rules. For example, a rule like role eq "admin" will match any context where role is "admin".
The userId attribute
The userId attribute has special significance:
- Rollout bucketing — percentage rollouts use
hash(userId + flagKey) % 100to assign users to buckets. Without auserId, rollouts are not deterministic. - Target lists — the
inTargetListandnotInTargetListoperators typically match againstuserId(though they can match any attribute).
Always provide a
userIdif you use percentage rollouts or target lists. Without it, the same user may get different results across evaluations.
Updating context
Context can be updated after initialization — for example, after a user logs in:
// After login
client.updateContext({
userId: user.id,
email: user.email,
plan: user.subscription.plan,
})
import { useFlagpool } from '@flagpool/react'
function AfterLogin() {
const { setContext } = useFlagpool()
const { user } = useAuth()
useEffect(() => {
if (user) {
setContext({
userId: user.id,
email: user.email,
plan: user.subscription.plan,
})
}
}, [user, setContext])
}
# After login
client.update_context({
"userId": user.id,
"email": user.email,
"plan": user.subscription.plan,
})
When context changes, all flags are re-evaluated automatically with the new values.
Privacy
Context attributes are never sent to Flagpool servers. All flag evaluation happens locally inside your application. The context exists only in memory within your SDK instance.
This is a core design principle of Flagpool — your user data stays in your application.
Next steps
- Targeting — use context attributes in targeting rules
- Rollouts — how
userIddrives percentage bucketing - Using Flags in Code — practical context patterns