How can I use the query engage api via python to get a user disticnt id by email?
Hi Yaniv K., Please stand by as I search the Mixpanel knowledge base for an AI-powered answer. I normally take about 15 seconds or so. In case AI cannot answer your question, feel free to reach out to Mixpanel support at mixpanel.com/get-support. If you have follow-up questions for this thread, please tag me again with your question: @kapa.ai <follow-up question> ๐
Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai
To use the Query Engage API via Python to get a user's distinct ID by email, you can utilize the query_engage() function from the mixpanel-utils library. Here's how you can do it:
```
import mputils
mputils.setup('YOUR_API_SECRET')
params = { 'where': 'properties["$email"] == "user@โexample.com"', 'output_properties': ['$distinct_id'] }
results = mputils.query_engage(params)
if results: distinct_id = results[0]['$distinct_id'] print(f"Distinct ID: {distinct_id}") else: print("No user found with that email.")
LBKSThis code snippet does the following:
1. It uses the `mputils.setup()` function to authenticate with your Mixpanel project.
2. It sets up the query parameters to filter for the specific email and request only the `$distinct_id` property.
3. It calls the `query_engage()` function with these parameters.
4. If a result is found, it extracts the distinct ID from the first (and likely only) result.
Note that you need to replace 'YOUR_API_SECRET' with your actual Mixpanel API secret, and 'user@โ[example.com](http://example.com)' with the email you're searching for.
This approach uses the mixpanel-utils library, which provides a convenient wrapper around the Mixpanel API. The underlying API being used is the Engage API, as described in the [Query API - Engage documentation](https://github.com/mixpanel/docs/blob/main/openapi/src/query.openapi.yaml).
Remember that the distinct ID returned will be the same as the one used when tracking events for this user, as mentioned in the [Managing User Identity documentation](https://docs.mixpanel.com/docs/tracking-methods/sdks/python#managing-user-identity).
[Mixpanel-utils Module](https://github.com/mixpanel/mixpanel-utils/blob/master/README.md)
kapa.ai is still learning and improving, please let me know how I did by reacting below.
โน๏ธย Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
try again this is not a real lib mputils
Let me check the knowledge base for you again. One moment! ๐
I apologize for the confusion in my previous response. You're right, and I made a mistake by referring to a library that doesn't exist. Let me provide you with the correct information based on the actual Mixpanel Python SDK and API.
To query the Engage API using Python to get a user's distinct ID by email, you can use the requests library to make an HTTP POST request to the Mixpanel Engage API. Here's how you can do it:
```
import requests import json
API_SECRET = "YOUR_API_SECRET" PROJECT_ID = "YOUR_PROJECT_ID"
email_to_search = "user@โexample.com"
url = "https://mixpanel.com/api/2.0/engage"
params = { "project_id": PROJECT_ID, "where": f'properties["$email"] == "{email_to_search}"', "output_properties": '["$distinct_id"]' }
response = requests.post(url, auth=(API_SECRET, ''), params=params)
if response.status_code == 200: data = response.json() if data["results"]: distinct_id = data["results"][0]["$distinct_id"] print(f"Distinct ID: {distinct_id}") else: print("No user found with that email.") else: print(f"Error: {response.status_code}, {response.text}")
LBKSThis code does the following:
1. It sets up the API credentials and the email to search for.
2. It defines the Engage API endpoint URL.
3. It sets up the query parameters, including the project ID, a where clause to filter by email, and specifies that we only want the $distinct_id in the output.
4. It makes a POST request to the API using the requests library.
5. If successful, it parses the JSON response and extracts the distinct ID.
Make sure to replace 'YOUR_API_SECRET' and 'YOUR_PROJECT_ID' with your actual Mixpanel API secret and project ID.
This approach directly uses the Mixpanel Engage API as described in the [Query API - Engage documentation](https://github.com/mixpanel/docs/blob/main/openapi/src/query.openapi.yaml).
[Exporting Profiles documentation](https://docs.mixpanel.com/docs/export-methods#exporting-profiles)
Remember that the distinct ID returned will be the same as the one used when tracking events for this user, as mentioned in the [Managing User Identity documentation](https://docs.mixpanel.com/docs/tracking-methods/sdks/python#managing-user-identity).
kapa.ai is still learning and improving, please let me know how I did by reacting below.
โน๏ธย Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
