SDKs and Recipes
Python SDK
5min
install the sdk set up virtual environment and install sdk go to settings and copy your api key \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] 3\ set up the customer api client \ you can use it to manage your customers meter records are associated with customers \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) 4\ create a customer \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) 5\ set up ingest api client by default, it will collect the meter records and send them in batches asynchronously \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) \# set up ingest api client and ingest a meter from time import time from metering ingest import create ingest client ingest api = create ingest client(api key=api key) 6\ send a meter record meter records will be collected in batches and sent asynchronously \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) \# set up ingest api client and ingest a meter from time import time from metering ingest import create ingest client ingest api = create ingest client(api key=api key) ingest api meter( meter api name="api calls", meter value=1, meter time in millis=int(time() 1000), customer id=customer\["customerid"], ) 7\ shut down ingest api client this is a blocking call that ensures all batches have been sent use it in your server shutdown procedure \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) \# set up ingest api client and ingest a meter from time import time from metering ingest import create ingest client ingest api = create ingest client(api key=api key) ingest api meter( meter api name="api calls", meter value=1, meter time in millis=int(time() 1000), customer id=customer\["customerid"], ) \# shut down ingest api client ingest api shutdown() 8\ set up usage api client \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) \# set up ingest api client and ingest a meter from time import time from metering ingest import create ingest client ingest api = create ingest client(api key=api key) ingest api meter( meter api name="api calls", meter value=1, meter time in millis=int(time() 1000), customer id=customer\["customerid"], ) \# shut down ingest api client ingest api shutdown() \# setup usage api client and get a usage report from metering usage import (aggregationtype, take, timegroupinginterval, timerange, usageapiclient, create usage query) usage api = usageapiclient(os environ get("api key")) 9\ get a usage report \# set up virtual environment and install sdk \# \# $ python3 m virtualenv venv \# $ venv/bin/activate \# $ pip install amberflo metering python import os \# sign up for free at https //ui amberflo io/ to get an api key api key = os environ\["amberflo api key"] \# set up customer api client and create a customer from metering customer import customerapiclient, create customer payload customer api = customerapiclient(os environ get("api key")) message = create customer payload( customer id="customer 123", customer name="customer 123", \# email is optional customer email="customer 123\@example com", \# traits are optional they can be used as filters or aggregation buckets traits={ "region" "us east 1", }, ) customer = customer api add or update(message) \# set up ingest api client and ingest a meter from time import time from metering ingest import create ingest client ingest api = create ingest client(api key=api key) ingest api meter( meter api name="api calls", meter value=1, meter time in millis=int(time() 1000), customer id=customer\["customerid"], ) \# shut down ingest api client ingest api shutdown() \# setup usage api client and get a usage report from metering usage import (aggregationtype, take, timegroupinginterval, timerange, usageapiclient, create usage query) usage api = usageapiclient(os environ get("api key")) since two days ago = timerange(int(time()) 60 60 24 2) query = create usage query( meter api name="api calls", aggregation=aggregationtype sum, time grouping interval=timegroupinginterval day, time range=since two days ago, group by=\["customerid"], usage filter={"customerid" \["customer 123"]}, take=take(limit=10, is ascending=false), ) report = usage api get(query)