Skip to main content

Offline-testable Python client for Amplitude Analytics APIs

Project description

PyAmplitude logo

PyAmplitude

CI codecov GitHub release PyPI version Python versions License: MIT

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_KEY
  • AMPLITUDE_SECRET_KEY
  • AMPLITUDE_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyamplitude-2.0.0.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyamplitude-2.0.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

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

Hashes for pyamplitude-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f9b98216b3f75813ec357cf5bdc90841c10587624d3c251e0388989e46e077d0
MD5 77d36c2e5a98537a74c4e1b35b21e3f0
BLAKE2b-256 2e1f4bb6b61fb351674a1547e38dfbb783c0ce2163075a23417248227c7f6f24

See more details on using hashes here.

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

Hashes for pyamplitude-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc0a136d6b9e77086dc08aa5aaf289c532d01dcfd38f8ac83183c728ac110e1e
MD5 52d619275b1f67c2667a08d6d212f620
BLAKE2b-256 72860bd21f5cb181a7302833c60630ab4a35282e115ae021924cd6424f57492a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page