Metronome SDK
The Metronome SDK is the official Python library for interacting with the Metronome API, designed for integrating metered billing and usage-based pricing into applications. It provides access to endpoints for managing customers, contracts, usage, invoices, and more. The library is currently at version 4.4.0 and follows a rapid release cadence with frequent updates and bug fixes.
Common errors
-
AttributeError: 'Rate' object has no attribute 'minimum_config'
cause You are attempting to set or access the `minimum_config` field on a `Rate` object, which was removed in version 4.3.0.fixRemove the `minimum_config` attribute from your `Rate` or `Override` object definitions. Update your code to reflect the schema changes. -
TypeError: 'priority' is an unexpected keyword argument for 'create_contract'
cause The `priority` field was removed as a parameter for contract creation/update in version 4.2.0.fixRemove the `priority` keyword argument from your `client.contracts.create()` or `client.contracts.edit()` calls. -
metronome.ApiException: 401 Unauthorized
cause The API key provided is missing, invalid, or has insufficient permissions.fixEnsure the `METRONOME_API_KEY` environment variable is correctly set, or pass a valid `api_key` directly when initializing `metronome.client.Metronome()`. -
TypeError: Missing required argument 'billing_config'
cause You are trying to create a resource (e.g., a customer) without providing a required configuration object.fixReview the Metronome API documentation for the specific endpoint you are using and provide all mandatory fields, such as `billing_config` when creating a customer.
Warnings
- breaking The `minimum_config` field was removed from Rate and Override schemas.
- breaking The `priority` parameter and response field were removed from contract-related API endpoints.
- breaking The `use_list_prices` field was removed from Rate objects.
- breaking The `customer_id` parameter was removed from preview events requests.
- breaking The Preview Events API changed its response structure to return a list of invoices instead of a single invoice object.
- breaking Pagination support was added to many list endpoints (e.g., Customers, Invoices, Commits, Credits, Usage).
Install
-
pip install metronome-sdk
Imports
- Metronome
import metronome; client = metronome.Metronome()
from metronome.client import Metronome
- types
from metronome import types
import metronome.types as types
Quickstart
import os
from metronome.client import Metronome
import metronome.types as types
METRONOME_API_KEY = os.environ.get('METRONOME_API_KEY', 'YOUR_API_KEY')
if not METRONOME_API_KEY or METRONOME_API_KEY == 'YOUR_API_KEY':
print("Warning: METRONOME_API_KEY not set. Using placeholder.")
# Example: run in dry-run mode or with a mocked client for testing
# For real API calls, ensure API key is set.
exit(1)
client = Metronome(api_key=METRONOME_API_KEY)
try:
# Example: List customers
customers = client.customers.list()
print(f"Found {len(customers.items)} customers:")
for customer in customers.items[:3]: # Print first 3 customers
print(f" ID: {customer.id}, Name: {customer.name}")
# Example: Create a new test customer
new_customer = client.customers.create(
name="My Test Customer from SDK",
billing_config=types.BillingConfig(
currency="USD",
billing_provider_type=types.BillingProviderType.STRIPE,
billing_provider_customer_id="cus_test_123",
),
external_id="my-test-customer-123"
)
print(f"Created new customer with ID: {new_customer.id}")
except Exception as e:
print(f"An error occurred: {e}")