Metronome SDK

4.4.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart initializes the Metronome client using an API key from an environment variable and then demonstrates listing existing customers and creating a new one. Ensure you have a `METRONOME_API_KEY` environment variable set with your actual Metronome API key for successful API calls.

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}")

view raw JSON →