Import the Stigg SDK and initialize it with your environment API key:
from stigg_sidecar_sdk import Stigg, ApiConfig, LocalSidecarConfigstigg = Stigg( ApiConfig( api_key="<SERVER_API_KEY>", ), # for development purposes, a sidecar will be spawned as a subprocess: local_sidecar_config=LocalSidecarConfig( redis=RedisOptions( environment_prefix="development", host="localhost", port=6379, db=0 ) ))
For testing & development purposes, Stigg will run the Sidecar service in a child process. The service listens for requests on address localhost:8433 by default.
For production, it is highly recommend to do run the Sidecar in a separate container, you can find more info in the Sidecar page.
For production use, its recommended to deploy the Sidecar service using the sidecar pattern, or as a standalone service. You set the remote sidecar host and port parameters as part of the SDK initialization:
from stigg_sidecar_sdk import Stigg, ApiConfigstigg = Stigg( # To access the API you need to set the server AP key ApiConfig( api_key="<SERVER_API_KEY>", ), # For production use, set remote sidecar host and port: remote_sidecar_host='localhost', remote_sidecar_port=8443)
Read about how to run the Sidecar service as separate container in the Sidecar page.
from stigg_sidecar_sdk import GetBooleanEntitlementRequestresp = await stigg.get_boolean_entitlement(GetBooleanEntitlementRequest( customer_id='customer-demo-01', feature_id='feature-03-custom-domain', resource_id='site-01' # Optional, specify the resource ID))if resp.has_access: pass # Customer has access to the feature
Checking if a customer has access to a metered feature
from stigg_sidecar_sdk import GetMeteredEntitlementRequestresp = await stigg.get_metered_entitlement(GetMeteredEntitlementRequest( customer_id='customer-demo-01', feature_id='feature-01-templates', resource_id='site-01', # Optional, resource ID)) if resp.has_access: pass # Has access, current usage is under the entitlement limit
Getting all of the entitlements for a specific customer
from stigg_sidecar_sdk import GetEntitlementsRequestresp = await stigg.get_entitlements(GetEntitlementsRequest( customer_id='customer-demo-01', resource_id='site-01' # Optional, resource ID))# All the entitlements the customer is entitled to entitlements = resp.entitlements
The Stigg SDK allows you to report usage measurements for metered features. The reported usage will be used to track, limit and bill the customer’s usage of metered features.
Stigg supports metering of usage from the following data sources:
Calculated usage - usage that has been aggregated and calculated by your application. This type is useful for features, such as: seats.
Raw events - raw events from your application, which Stigg filters and aggregates aggregate to calculate customer usage. This type is useful for features, such as: monthly active users (MAUs).
More details about Stigg’s metering and aggregation capabilities can be found here:
Reporting of measurements to Stigg should be done only after the relevant resources have been provisioned in your application.
To ensure that the provisioned resources are aligned with the measurements that are reported to Stigg, ensure that customers are not allowed to allocate new resources until an acknowledgement about the processed measurement is received from the Stigg backend.
Validating that a measurement was successfully reported to Stigg is also possible in the customer details section of the relevant customer in the Stigg app.
from stigg_sidecar_sdk import ReportUsageRequest# Example: incrementing usage of a metered feature by 2await stigg.report_usage(ReportUsageRequest( customer_id='customer-demo-01', feature_id='feature-01-templates', value=2, # Usage amount resource_id='site-01' # Optional, resource ID))
By default, the value reported should represent the change in usage of the feature, for example user added 2 more seats then the report usage call should have the value of 2.
In some cases, it’s more useful to set the usage to some value instead of reporting the change value, it’s possible by passing the updateBehavior parameter:
# Example: set usage of a metered feature to 3resp = await stigg.report_usage(ReportUsageRequest( customer_id='customer-demo-01', feature_id='feature-01-templates', value=3, # Usage amount update_behavior=UsageUpdateBehavior.USAGE_UPDATE_BEHAVIOR_SET, resource_id='site-01' # optional, resource ID))
In order for the cache to survive between restarts or to be shared across multiple instances of the Sidecar service, you can configure the Sidecar to use Redis as a cache provider.
from stigg_sidecar_sdk import Stigg, ApiConfig, LocalSidecarConfig, RedisOptionsstigg = Stigg( ApiConfig( api_key="<SERVER_API_KEY>", ), # For development purposes, configure local sidecar to use redis: local_sidecar_config=LocalSidecarConfig( redis=RedisOptions( environment_prefix="development", host="localhost", port=6379, db=0 ) ), # For production use, provide remote sidecar with the REDIS_* env vars: remote_sidecar_host='localhost', remote_sidecar_port=8443)
To keep the cache up-to-date, you will also need to run a persistent cache service in a separate process.
Read about how to run the Sidecar service as separate container in the Sidecar page.