Exporting Raw Data from Mixpanel: Troubleshooting 400 Error
How do I export raw data for mixpanel? I've this script, but it keeps throwing 400 error and fails on authenication
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime, timedelta
# --- Configuration ---
service_account_username = "service_account" # Replace
service_account_password = "service_password" # Replace
project_id = 3732260 # Replace with your integer project ID
output_filename = "mixpanel_weekly_export.csv"
# --- Calculate Last Week's Date Range (Monday to Sunday) ---
today = datetime.today()
last_monday = today - timedelta(days=today.weekday() + 7)
last_sunday = last_monday + timedelta(days=6)
from_date = last_monday.strftime("%Y-%m-%d")
to_date = last_sunday.strftime("%Y-%m-%d")
# --- Build API URL ---
api_url = f"https://data.mixpanel.com/api/2.0/export?project_id={project_id}&from_date={from_date}&to_date={to_date}"
# api_url = f"https://api.mixpanel.com/"
# --- Fetch Data ---
print(f"📅 Fetching Mixpanel data from {from_date} to {to_date}...")
response = requests.get(
api_url,
auth=HTTPBasicAuth(service_account_username, service_account_password),
timeout=180
)
# --- Handle Response ---
if response.status_code == 200:
with open(output_filename, "w", encoding="utf-8") as f:
f.write(response.text)
print(f"✅ Data saved to {output_filename}")
else:
print(f"❌ Failed to fetch data. HTTP {response.status_code}")
print("🔍 Details:", response.text)
print("Response: ", response)
