Amplitude Python Server SDK for Experiment.
pip install amplitude-experiment
from amplitude_experiment import Experiment, RemoteEvaluationConfig, RemoteEvaluationClient, User
# (1) Get your deployment's API key
apiKey = 'YOUR-API-KEY'
# (2) Initialize the experiment remote evaluation
experiment = Experiment.initialize_remote(api_key)
# (3) Fetch variants for a user
user = User(
device_id="abcdefg",
user_id="[email protected]",
user_properties={
'premium': True
}
)
# (4) Lookup a flag's variant
#
# To fetch synchronous
variants = experiment.fetch(user)
variant = variants['YOUR-FLAG-KEY']
if variant:
if variant.value == 'on':
# Flag is on
else:
# Flag is off
# To fetch asynchronous
experiment.fetch_async(user, fetch_callback)
def fetch_callback(user, variants):
variant = variants['YOUR-FLAG-KEY']
if variant:
if variant.value == 'on':
# Flag is on
else:
# Flag is off
# (1) Initialize the local evaluation client with a server deployment key.
experiment = Experiment.initialize_local(api_key)
# (2) Start the local evaluation client.
experiment.start()
# (3) Evaluate a user.
user = User(
device_id="abcdefg",
user_id="[email protected]",
user_properties={
'premium': True
}
)
variants = experiment.evaluate(user)
To setup for running test on local, create a .env
file with following
contents, and replace {API_KEY}
and {SECRET_KEY}
(or {EU_API_KEY}
and {EU_SECRET_KEY}
for EU data center) for the project in test:
API_KEY={API_KEY}
SECRET_KEY={SECRET_KEY}
Please visit our 💯Developer Center for more instructions on using our the SDK.
See our Experiment Python SDK Docs for a list and description of all available SDK methods.
If you have any problems or issues over our SDK, feel free to create a github issue or submit a request on Amplitude Help.