Skip to content

Repository files navigation

mixpanel-python

July 24, 2026 - v5.3.0

PyPIPyPI - Python VersionPyPI - DownloadsTests

This is the official Mixpanel Python library. This library allows for server-side integration of Mixpanel.

To import, export, transform, or delete your Mixpanel data, please see our mixpanel-utils package.

Installation

The library can be installed using pip:

pip install mixpanel

Getting Started

Typical usage usually looks like this:

frommixpanelimportMixpanelmp=Mixpanel(YOUR_TOKEN)
# tracks an event with certain propertiesmp.track(DISTINCT_ID, 'button clicked', {'color' : 'blue', 'size': 'large'})
# sends an update to a user profilemp.people_set(DISTINCT_ID, {'$first_name' : 'Ilya', 'favorite pizza': 'margherita'})

You can use an instance of the Mixpanel class for sending all of your events and people updates.

Service Account Authentication

For enhanced security in server-to-server integrations, you can use service account credentials for authentication instead of API secrets:

frommixpanelimportMixpanel, ServiceAccountCredentials# Create credentials object# Service accounts replace api_key/api_secret for authenticationcredentials=ServiceAccountCredentials(
username='YOUR_SERVICE_ACCOUNT_USERNAME',
secret='YOUR_SERVICE_ACCOUNT_SECRET',
project_id='YOUR_PROJECT_ID'
)
# Token identifies the project and is used for event tracking# Credentials are used for endpoints that require authenticationmp=Mixpanel(YOUR_TOKEN, credentials=credentials)
# Event tracking operations use the token (sent in payload)mp.track(DISTINCT_ID, 'button clicked', {'color': 'blue'})
mp.people_set(DISTINCT_ID, {'$first_name': 'John'})

Service account credentials can also be used with custom consumers like BufferedConsumer:

frommixpanelimportMixpanel, BufferedConsumer, ServiceAccountCredentialscredentials=ServiceAccountCredentials(
username='YOUR_SERVICE_ACCOUNT_USERNAME',
secret='YOUR_SERVICE_ACCOUNT_SECRET',
project_id='YOUR_PROJECT_ID'
)
# Option 1: Pass credentials to the consumer constructorconsumer=BufferedConsumer(max_size=50, credentials=credentials)
mp=Mixpanel(YOUR_TOKEN, consumer=consumer)
# Option 2: Let Mixpanel create the default consumer with credentialsmp=Mixpanel(YOUR_TOKEN, credentials=credentials)
# Event tracking uses the token (sent in payload)mp.track(DISTINCT_ID, 'event_name')

Service account credentials are only used for the /import endpoint and feature flags. Regular event tracking operations (track, people_set, group_set) use the token provided in the constructor, which is sent in the event payload.

Service Accounts with Feature Flags

Service account credentials are automatically used for feature flag operations when configured:

frommixpanelimportMixpanel, ServiceAccountCredentialsfrommixpanel.flags.typesimportLocalFlagsConfigcredentials=ServiceAccountCredentials(
username='YOUR_SERVICE_ACCOUNT_USERNAME',
secret='YOUR_SERVICE_ACCOUNT_SECRET',
project_id='YOUR_PROJECT_ID'
)
# Token identifies the project for event tracking, credentials handle authenticationmp=Mixpanel(
YOUR_TOKEN,
credentials=credentials,
local_flags_config=LocalFlagsConfig()
)
# Feature flag requests will use service account authenticationvariant=mp.local_flags.get_variant_value('my-flag', fallback_value=False, context={...})

Note: When using service account credentials, the token parameter in the Mixpanel constructor is still required for event tracking operations (track, people_set, etc.) since the token is included in the event data payload. However, feature flag operations use the project_id from credentials instead of the token for authentication.

Additional Information

Ask DeepWiki

About

Official Mixpanel Python library.

Topics

Resources

Security policy

Stars

108 stars

Watchers

74 watching

Forks

Releases

Packages

Used by

Contributors

Languages