Official Python client for Supermetrics
- PyPI package: https://pypi.org/project/supermetrics/
- Free software: Apache License v2
- Type-safe Python client generated from OpenAPI specification
- Dual sync/async support via separate Client classes
- Pydantic v2 models for request/response validation
- Comprehensive API coverage: login links, logins, accounts, queries
- Custom exception hierarchy with HTTP status code mapping
- Resource-based API organization
pip install supermetricsfrom supermetrics import SupermetricsClient
# Initialize client
client = SupermetricsClient(api_key="your_api_key")
# Create login link for data source authentication
link = client.login_links.create(
ds_id="GAWA",
description="My Analytics Authentication"
)
# Get login details after user authenticates
login = client.logins.get(login_id=link.login_id)
# List available accounts
accounts = client.accounts.list(
ds_id="GAWA",
login_usernames=login.username
)
# Execute query
result = client.queries.execute(
ds_id="GAWA",
ds_accounts=[accounts[0].account_id],
fields=["Date", "Sessions", "Users"],
start_date="2024-01-01",
end_date="2024-01-07"
)
print(f"Retrieved {len(result.data)} rows")See the examples/ directory for complete working examples:
complete_flow.py- Full sync workflow from authentication to query executionasync_flow.py- Async version of complete workflow
See examples/README.md for setup and running instructions.
The SDK provides specific exception types for different error scenarios:
from supermetrics import (
SupermetricsClient,
AuthenticationError,
ValidationError,
APIError,
NetworkError,
)
client = SupermetricsClient(api_key="your_key")
try:
link = client.login_links.create(ds_id="GAWA", description="Test")
except AuthenticationError as e:
print(f"Invalid API key: {e.message}")
except ValidationError as e:
print(f"Invalid parameters: {e.message}")
except APIError as e:
print(f"API error: {e.message}")
except NetworkError as e:
print(f"Network error: {e.message}")The SDK client is auto-generated from the Supermetrics OpenAPI specification.
- Location:
openapi-specs/directory (containsopenapi-data.yamlandopenapi-management.yaml) - Merged Spec:
openapi-spec.yaml(project root) - filtered, patched, and merged from source specs - Configuration:
scripts/references/sdk-endpoint-filters.yaml- controls which endpoints are included and applies patches/customizations - Documentation: See scripts/README.md for detailed patch system documentation
The SDK uses a configuration-driven process to create a focused, customizable client from multiple OpenAPI specifications.
This YAML file defines which API endpoints to include in the SDK and allows you to apply patches/customizations to both endpoints and shared components.
Key Features:
- Endpoint Filtering: Include only the endpoints your application needs
- Endpoint Patches: Customize individual endpoint definitions (descriptions, parameters, responses, etc.)
- Component Patches: Apply surgical modifications to shared schemas, responses, and other components
- Merge & Replace Strategies: Deep merge or complete replacement of OpenAPI sections
Basic Example:
endpoints:
- method: GET
path: /ds/logins
- method: GET
path: /query/data/json
component_patches:
schemas:
DataResponse:
merge:
properties:
meta:
properties:
result:
properties:
cache_time:
nullable: trueFor detailed documentation on the configuration format, patch strategies, and comprehensive examples, see scripts/README.md.
This Python script processes multiple OpenAPI specifications, applies customizations, and creates a single openapi-spec.yaml file.
What it does:
- Reads configuration from
scripts/references/sdk-endpoint-filters.yaml - Scans and loads all
.yaml/.ymlfiles fromopenapi-specs/directory - Filters endpoints based on configuration
- Applies endpoint patches (merge/replace operations)
- Collects all referenced components via
$reftraversal (dependency resolution) - Resolves external file references
- Applies component patches to shared schemas, responses, etc.
- Detects and fails on duplicate
METHOD|PATHacross specs - Merges everything into single specification
- Validates all requested endpoints were found
Usage:
python scripts/filter_openapi_spec.pyConfiguration:
- Input:
openapi-specs/*.yamlandscripts/references/sdk-endpoint-filters.yaml - Output:
openapi-spec.yaml
Exit codes:
0- Success1- Error (missing files, duplicates, or validation failure)
For detailed documentation on patch strategies, troubleshooting, and examples, see scripts/README.md.
Full Regeneration (recommended):
# 1. Update source specs in openapi-specs/ if needed
# 2. Update scripts/references/sdk-endpoint-filters.yaml to add/remove endpoints or apply patches
# 3. Run filter script to regenerate merged spec
python scripts/filter_openapi_spec.py
# 4. Regenerate SDK from merged spec
./scripts/regenerate_client.shQuick Regeneration (if openapi-spec.yaml unchanged):
./scripts/regenerate_client.sh- Monthly (or when Supermetrics API changes)
- After updating source specs in
openapi-specs/ - After modifying
scripts/references/sdk-endpoint-filters.yaml(adding/removing endpoints or changing patches)
- Edit
scripts/references/sdk-endpoint-filters.yaml:- Add/remove endpoints in the
endpointslist - Add/modify patches in
component_patchesor endpoint-levelpatches
- Add/remove endpoints in the
- Run
python scripts/filter_openapi_spec.pyto regenerate the merged spec - Run
./scripts/regenerate_client.shto regenerate the SDK client
See scripts/README.md for detailed documentation on:
- Configuration file format
- Endpoint and component patch strategies
- Comprehensive examples
- Troubleshooting guide
Note: The adapter pattern (implemented in Story 1.3+) protects users from breaking changes during regeneration
Contributions are welcome! Please see CONTRIBUTING.md for guidelines on how to contribute, run tests, and deploy releases.
This package was created with Cookiecutter and the audreyfeldroy/cookiecutter-pypackage project template.