Server SDK

Python Integration

Track server-side events from your Python backend with our simple HTTP API.

Installation

No special SDK required! Just use the requests library:

Shell
pip install requests

Basic Setup

Python
import requests
import os

def track_event(event_name, metadata=None):
    url = "https://phi.llc/api/phi/track"
    payload = {
        "website_id": os.getenv("PHI_WEBSITE_ID"),
        "event_name": event_name,
        "url": "python-backend",
        "metadata": metadata or {}
    }
    try:
        requests.post(url, json=payload, timeout=5)
    except Exception as e:
        print(f"Phi Tracking Failed: {e}")

# Usage
track_event("UserSignedUp", {"plan": "premium"})

FastAPI Background Tasks

Always use background tasks to avoid blocking your API responses:

Python
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()

@app.post("/signup")
async def signup(background_tasks: BackgroundTasks):
    background_tasks.add_task(track_event, "SignupStarted")
    return {"status": "ok"}

Pro Tips

  • Timeout: Always set a short timeout (e.g. 5s) for tracking requests.
  • Resilience: Wrap calls in try/except to ensure your app stays up.