From c0f290a3854d1127592a00c5172d05eca4e88fb4 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Wed, 8 Jul 2026 13:14:42 -0400 Subject: [PATCH] fix(fastapi): use the Python context API for identify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fastapi example called posthog.identify() and posthog.capture(distinct_id, event) — neither exists in the Python SDK v6+ — so login/signup would raise at runtime; switch to new_context()/identify_context()/capture() like the rest of the example (person props via $set). Co-Authored-By: Claude Opus 4.8 --- example-apps/fastapi/app/routers/main.py | 40 +++++++++++++----------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/example-apps/fastapi/app/routers/main.py b/example-apps/fastapi/app/routers/main.py index 0d1c5e53..ffb0719f 100644 --- a/example-apps/fastapi/app/routers/main.py +++ b/example-apps/fastapi/app/routers/main.py @@ -7,7 +7,7 @@ from fastapi import APIRouter, Cookie, Depends, Form, Request from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates -from posthog import capture +from posthog import capture, identify_context, new_context from app.dependencies import ( CurrentUser, @@ -47,15 +47,16 @@ async def login( if user: is_new_user = user.record_login(db) - posthog.identify(user.email, {"email": user.email, "is_staff": user.is_staff}) - posthog.capture( - user.email, - "user_logged_in", - properties={ - "username": user.email, - "is_new_user": is_new_user, - }, - ) + with new_context(): + identify_context(user.email) + capture( + "user_logged_in", + properties={ + "$set": {"email": user.email, "is_staff": user.is_staff}, + "username": user.email, + "is_new_user": is_new_user, + }, + ) # Create session and redirect response = RedirectResponse(url="/dashboard", status_code=302) @@ -112,15 +113,16 @@ async def signup( # Create new user user = User.create_user(db, email=email, password=password, is_staff=False) - posthog.identify(user.email, {"email": user.email, "is_staff": user.is_staff}) - posthog.capture( - user.email, - "user_signed_up", - properties={ - "username": user.email, - "signup_method": "form", - }, - ) + with new_context(): + identify_context(user.email) + capture( + "user_signed_up", + properties={ + "$set": {"email": user.email, "is_staff": user.is_staff}, + "username": user.email, + "signup_method": "form", + }, + ) # Create session and redirect response = RedirectResponse(url="/dashboard", status_code=302)