Python SDK
Official Python SDK for GoGreen feature flags and experimentation. Supports real-time streaming, event tracking, and OpenFeature provider.
Requirements
- Python 3.10+
- GoGreen evaluation API (e.g.
https://app.gogreenflags.com)
Installation
From PyPI (when published)
pip install gogreen-sdkFrom source (GitHub)
pip install git+https://github.com/MarkDurbin104/GoGreen.git#subdirectory=sdk/pythonOr clone and install in editable mode:
git clone https://github.com/MarkDurbin104/GoGreen.git
cd GoGreen/sdk/python
pip install -e .Quick start
from gogreen import GoGreenClient
from gogreen.config import GoGreenConfig
config = GoGreenConfig(
sdk_key="YOUR_SDK_KEY",
environment_id="YOUR_ENV_ID",
default_context={"key": "user-123", "custom": {"plan": "enterprise"}},
api_base_url="https://app.gogreenflags.com",
)
client = GoGreenClient(config)
client.wait_for_initialization(timeout=5.0)
# Boolean flag
if client.bool_variation("new-checkout", False):
show_new_checkout()
# With optional context override
user = {"key": "user-456", "custom": {"country": "US"}}
theme = client.string_variation("ui-theme", "light", context=user)
# Number flag (e.g. for gradual rollouts)
limit = client.number_variation("rate-limit", 100)
# JSON flag
config_value = client.json_variation("feature-config", {})
# Detail (value + variation id, reason, prerequisite_key)
detail = client.bool_variation_detail("new-checkout", False)
print(detail.value, detail.reason, detail.variation_id)
# Track custom events for experimentation
client.track("checkout_completed", {"revenue": 49.99})
client.close()Configuration
| Parameter | Type | Required | Description |
|---|---|---|---|
sdk_key | str | Yes | SDK key from your GoGreen environment |
environment_id | str | Yes | Environment ID (e.g. UUID) |
default_context | dict | Yes | Default evaluation context; must include "key" |
api_base_url | str | No | Evaluation API base URL (default: http://localhost:8092) |
stream_url | str | No | Streaming SSE URL (default: same as api_base_url) |
events_url | str | No | Events ingestion URL (default: same as api_base_url) |
enable_streaming | bool | No | Real-time flag updates via SSE (default: True) |
offline | bool | No | Start in offline mode (default: False) |
bootstrap | dict | No | Initial flags to skip first fetch |
request_timeout | float | No | HTTP timeout in seconds (default: 10.0) |
flush_interval | float | No | Event batch flush interval in seconds (default: 30.0) |
private_attributes | list[str] | No | Attribute names to redact in events (PII) |
Evaluating flags
- Variations:
bool_variation,string_variation,number_variation,json_variation— each has a*_variation_detailvariant returning value, variation_id, reason, rule_index, prerequisite_key. - Lifecycle:
wait_for_initialization(timeout=None),initialized(),close() - Context:
set_context(context),identify(context) - Offline:
enter_offline_mode(),exit_offline_mode() - Events:
on(event, handler)/off(event, handler)for"ready","update","error";track(event_key, properties=None) - Test helper:
GoGreenClient.test_client(flags: dict)for unit tests (offline client with fixed flag values).
OpenFeature provider
from gogreen import GoGreenClient, GoGreenProvider
from gogreen.config import GoGreenConfig
from openfeature import api
config = GoGreenConfig(
sdk_key="YOUR_SDK_KEY",
environment_id="YOUR_ENV_ID",
default_context={"key": "user-123", "custom": {}},
)
client = GoGreenClient(config)
api.set_provider(GoGreenProvider(client))
client = api.get_client()
value = client.get_boolean_value("my-flag", False)Testing
# Create an offline test client with predetermined flag values
client = GoGreenClient.test_client({
"feature-x": True,
"ui-theme": "dark",
"rate-limit": 50,
})
# Use in tests — no network calls
enabled = client.bool_variation("feature-x", False) # returns True