{"id":6465,"library":"stytch","title":"Stytch Python Client","description":"The Stytch Python library provides an interface to the Stytch user infrastructure API, simplifying the integration of authentication and authorization flows into Python applications. It supports various authentication methods like Email Magic Links, OAuth, SMS/WhatsApp passcodes, and more for both B2C and B2B use cases. The library is actively maintained with frequent releases, often multiple times a month for minor versions, indicating an agile development cycle.","status":"active","version":"14.4.0","language":"en","source_language":"en","source_url":"https://github.com/stytchauth/stytch-python","tags":["authentication","security","identity-management","SaaS","authn","authz"],"install":[{"cmd":"pip install stytch","lang":"bash","label":"Install Stytch Python client"}],"dependencies":[{"reason":"Required runtime environment.","package":"python","optional":false}],"imports":[{"note":"Used for B2C authentication flows.","symbol":"Client","correct":"import stytch\nclient = stytch.Client(...)"},{"note":"Used for B2B authentication flows, requires different API calls specific to organizations and members.","symbol":"B2BClient","correct":"import stytch\nb2b_client = stytch.B2BClient(...)"},{"note":"The base exception for all structured errors from the Stytch API.","symbol":"StytchError","correct":"from stytch import StytchError"}],"quickstart":{"code":"import os\nimport stytch\n\n# Initialize the Stytch client using environment variables\n# Replace with your actual project_id and secret, or set as environment variables\n# For B2C authentication:\nclient = stytch.Client(\n    project_id=os.environ.get(\"STYTCH_PROJECT_ID\", \"project-test-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"),\n    secret=os.environ.get(\"STYTCH_SECRET\", \"secret-test-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"),\n    # Optionally specify a custom base URL for all API calls\n    # custom_base_url=\"https://api.custom-domain.com/\",\n)\n\n# For B2B authentication, use stytch.B2BClient instead:\n# b2b_client = stytch.B2BClient(\n#     project_id=os.environ.get(\"STYTCH_PROJECT_ID\", \"project-test-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"),\n#     secret=os.environ.get(\"STYTCH_SECRET\", \"secret-test-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"),\n# )\n\ntry:\n    # Example: Send a magic link for login or signup\n    email_to_authenticate = \"test@example.com\"\n    response = client.magic_links.email.login_or_create(\n        email=email_to_authenticate,\n        login_magic_link_url=\"http://localhost:3000/authenticate\",\n        signup_magic_link_url=\"http://localhost:3000/authenticate\",\n    )\n    print(f\"Magic link sent to {email_to_authenticate}. Status code: {response.status_code}\")\n    print(f\"Request ID: {response.request_id}\")\nexcept stytch.StytchError as e:\n    print(f\"Stytch API error occurred: {e.details}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the Stytch client using environment variables and send a magic link for user login or signup. It includes basic error handling for `StytchError` exceptions."},"warnings":[{"fix":"For async applications, append `_async` to method calls (e.g., `await client.users.get_async(...)`). For sync applications, use the methods without `_async`.","message":"Starting with Python SDK v6, asynchronous support was added. All API methods now have synchronous and asynchronous equivalents (e.g., `client.magic_links.authenticate` vs. `client.magic_links.authenticate_async`). Ensure you use the correct method based on your application's concurrency model.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Always catch `stytch.StytchError` to properly handle and inspect API-specific error details via the `.details` property.","message":"All structured errors returned from the Stytch API are encapsulated in `stytch.StytchError` exceptions. Generic exception catching might mask specific API error details.","severity":"gotcha","affected_versions":"All"},{"fix":"Instantiate `stytch.Client` for Consumer projects and `stytch.B2BClient` for B2B projects. Do not mix their usage, as B2B specific endpoints will not be available on `stytch.Client` and vice-versa.","message":"Stytch differentiates between Consumer (B2C) and B2B authentication. You must use the appropriate client (`stytch.Client` for B2C or `stytch.B2BClient` for B2B) as their API interfaces and expected payloads differ significantly.","severity":"gotcha","affected_versions":"All"},{"fix":"When interacting with M2M token endpoints, ensure you provide the M2M `client_id` and `client_secret` for authentication, either in the request body or via HTTP Basic Auth.","message":"For Machine-to-Machine (M2M) authentication endpoints, such as `POST /oauth2/token`, authentication is performed using `client_id` and `client_secret` rather than the standard `project_id` and `secret` used for other API calls.","severity":"gotcha","affected_versions":"All"},{"fix":"If MFA is handled by your legacy system before session migration, complete the MFA flow in your existing system, then call Stytch's `sessions.migrate` (or `migrate_async`) to establish the final Stytch session.","message":"When migrating sessions from an existing authentication system using `sessions.migrate_async`, the API is designed to return a full-fledged Stytch session. If your existing system handles Multi-Factor Authentication (MFA) prior to migration, that MFA logic typically needs to remain outside the Stytch ecosystem for the `migrate` call.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}