Offline-testable Python client for Amplitude Analytics APIs
Project description
PyAmplitude
pyamplitude is a modern, offline-testable Python client for Amplitude Analytics APIs.
This rewrite targets the current Amplitude API families instead of the legacy 2017 Dashboard-only surface:
- Dashboard REST API
- Export API
- Behavioral Cohorts API, including asynchronous cohort export requests
- HTTP V2 ingestion API
- Batch Event Upload API
- Optional Redshift helpers for historical Amplitude exports
The package is designed to be tested without real Amplitude credentials. Every client accepts an injectable transport, so unit tests can validate URLs, auth, query parameters, JSON payloads, response parsing and error handling locally.
Installation
pip install pyamplitude
For local development:
python -m pip install -e ".[dev]"
python -m pytest
For optional Redshift support:
python -m pip install "pyamplitude[redshift]"
Credentials
Dashboard, Export and Behavioral Cohorts use Basic Auth with an API key and secret key:
from pyamplitude import AmplitudeCredentials
credentials = AmplitudeCredentials(
api_key="AMPLITUDE_API_KEY",
secret_key="AMPLITUDE_SECRET_KEY",
project_name="production",
)
HTTP V2 and Batch ingestion only require the API key:
from pyamplitude.ingestion import make_ingestion_credentials
credentials = make_ingestion_credentials("AMPLITUDE_API_KEY")
US and EU regions are supported:
from pyamplitude import BatchClient
client = BatchClient(credentials, region="EU")
Ingest Events
from pyamplitude import AmplitudeEvent, HTTPV2Client
from pyamplitude.ingestion import make_ingestion_credentials
client = HTTPV2Client(make_ingestion_credentials("AMPLITUDE_API_KEY"))
client.upload([
AmplitudeEvent(
event_type="Signup",
user_id="user-123",
event_properties={"source": "docs"},
insert_id="signup-user-123",
)
])
Query Dashboard Charts
from pyamplitude import AmplitudeCredentials, DashboardClient, Segment
credentials = AmplitudeCredentials(api_key="key", secret_key="secret")
client = DashboardClient(credentials)
segment = Segment.user_property("country", "is", ["Uruguay"])
data = client.active_users(
start="20240101",
end="20240131",
mode="active",
interval=1,
segments=[segment],
group_by="country",
)
Export Events
from pyamplitude import AmplitudeCredentials, ExportClient
client = ExportClient(AmplitudeCredentials(api_key="key", secret_key="secret"))
events = client.export_events(start="20240101T00", end="20240101T23")
Behavioral Cohorts
from pyamplitude import AmplitudeCredentials, CohortsClient
client = CohortsClient(AmplitudeCredentials(api_key="key", secret_key="secret"))
job = client.request_cohort("cohort-id", include_properties=True)
status = client.request_status(job["request_id"])
archive = client.download_cohort(job["request_id"])
Compatibility Imports
The old import paths still exist as wrappers:
from pyamplitude.amplituderestapi import AmplitudeRestApi
from pyamplitude.behavioralcohortsapi import BehavioralCohortsApi
from pyamplitude.exportapi import AmplitudeExportApi
from pyamplitude.projectshandler import ProjectsHandler
New code should prefer DashboardClient, CohortsClient, ExportClient,
HTTPV2Client and BatchClient.
Testing Without API Keys
The project does not require real Amplitude credentials for normal test runs.
python -m pytest
Integration tests should be marked with @pytest.mark.integration and skipped unless
these environment variables exist:
AMPLITUDE_API_KEYAMPLITUDE_SECRET_KEYAMPLITUDE_PROJECT_ID
Documentation
Sphinx documentation lives in docs/source.
python -m sphinx -b html docs/source docs/_build/html
Project status badges in this README and the Sphinx documentation track the
develop branch until a release branch is promoted.
Release builds use semantic version tags such as v2.0.0. The PyPI long
description is sourced from this README through pyproject.toml.
Current Amplitude API references:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyamplitude-2.0.0.tar.gz.
File metadata
- Download URL: pyamplitude-2.0.0.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9b98216b3f75813ec357cf5bdc90841c10587624d3c251e0388989e46e077d0
|
|
| MD5 |
77d36c2e5a98537a74c4e1b35b21e3f0
|
|
| BLAKE2b-256 |
2e1f4bb6b61fb351674a1547e38dfbb783c0ce2163075a23417248227c7f6f24
|
File details
Details for the file pyamplitude-2.0.0-py3-none-any.whl.
File metadata
- Download URL: pyamplitude-2.0.0-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc0a136d6b9e77086dc08aa5aaf289c532d01dcfd38f8ac83183c728ac110e1e
|
|
| MD5 |
52d619275b1f67c2667a08d6d212f620
|
|
| BLAKE2b-256 |
72860bd21f5cb181a7302833c60630ab4a35282e115ae021924cd6424f57492a
|