Skip to main content

Server Side SDKs

tip

Server Side SDKs can run in 2 different modes: Local Evaluation and Remote Evaluation. We recommend reading up about the differences first before integrating the SDKS into your applications.

Once you've got that understood, lets get the SDKs integrated!

All our SDKs are on Github.

Add the Flagsmith package

pip install flagsmith

Initialise the SDK

from flagsmith import Flagsmith

flagsmith = Flagsmith(
environment_key = os.environ.get("<FLAGSMITH_ENVIRONMENT_KEY>")
)

Get Flags for an Environment

# The method below triggers a network request
flags = flagsmith.get_environment_flags()
show_button = flags.is_feature_enabled("secret_button")
button_data = json.loads(flags.get_feature_value("secret_button"))

Get Flags for an Identity

identifier = "delboy@trotterstraders.co.uk"
traits = {"car_type": "robin_reliant"}

# The method below triggers a network request
identity_flags = flagsmith.get_identity_flags(identifier=identifier, traits=traits)
show_button = identity_flags.is_feature_enabled("secret_button")
button_data = json.loads(identity_flags.get_feature_value("secret_button"))

Managing Default Flags

Default Flags are configured by passing in a function that is called when a Flag cannot be found or if the network request to the API fails when retrieving flags.

from flagsmith import Flagsmith
from flagsmith.models import DefaultFlag

def default_flag_handler(feature_name: str) -> DefaultFlag:
"""
Function that will be used if the API doesn't respond, or an unknown
feature is requested
"""
if feature_name == "secret_button":
return DefaultFlag(
enabled=False,
value=json.dumps({"colour": "#b8b8b8"}),
feature_name="secret_button",
)
],
return DefaultFlag(False, None)

flagsmith = Flagsmith(
environment_key=os.environ.get("<FLAGSMITH_ENVIRONMENT_KEY>"),
default_flag_handler=default_flag_handler,
)

Network Behaviour

The Server Side SDKS share the same network behaviour across the different languages:

Remote Evaluation Mode Network Behaviour

  • A blocking network request is made every time you make a call to get an Environment Flags. In Python, for example, flagsmith.get_environment_flags() will trigger this request.
  • A blocking network request is made every time you make a call to get an Identities Flags. In Python, for example, flagsmith.get_identity_flags(identifier=identifier, traits=traits) will trigger this request.

Local Evaluation Mode Network Behaviour

  • When the SDK is initialised, it will make an asnchronous network request to retrieve details about the Environment.
  • Every 60 seconds (by default), it will repeat this aysnchronous request to ensure that the Environment information it has is up to date.

Configuring the SDK

You can modify the behaviour of the SDK during initialisation. Full configuration options are shown below.

flagsmith = Flagsmith(
# Your API Token.
# Note that this is either the `Environment API` key or the `Server Side SDK Token`
# depending on if you are using Local or Remote Evaluation
# Required.
environment_key = os.environ.get("FLAGSMITH_ENVIRONMENT_KEY"),

# Controls which mode to run in; local or remote evaluation.
# See the `SDKs Overview Page` for more info
# Optional.
# Defaults to False.
enable_local_evaluation = False,

# Override the default Flagsmith API URL if you are self-hosting.
# Optional.
# Defaults to https://edge.api.flagsmith.com/api/v1/
api_url = "https://api.yourselfhostedflagsmith.com/api/v1/",

# The network timeout in seconds.
# Optional.
# Defaults to 10 seconds
request_timeout_seconds = 10,

# When running in local evaluation mode, defines
# how often to request an updated Environment document in seconds
# Optional
# Defaults to 60 seconds
environment_refresh_interval_seconds: int = 60,

# A `urllib3` Retries object to control network retry policy
# See https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.Retry
# Optional
# Defaults to None
retries: Retry = None,

# Controls whether Flag Analytics data is sent to the Flagsmith API
# See https://docs.flagsmith.com/advanced-use/flag-analytics
# Optional
# Defaults to False
enable_analytics: bool = False,

# You can pass custom headers to the Flagsmith API with this Dictionary.
# This can be helpful, for example, when sending request IDs to help trace requests.
# Optional
# Defaults to None
custom_headers: typing.Dict[str, typing.Any] = None,

# You can specify a function to handle returning defaults in the case that
# the request to flagsmith fails or the flag requested is not included in the
# response
# Optional
default_flag_handler = lambda feature_name: return DefaultFlag(enabled=False, value=None)
)

Contribute to the SDKs

All our SDKs are Open Source.