Stytch Python Client
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.
Warnings
- breaking 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.
- gotcha All structured errors returned from the Stytch API are encapsulated in `stytch.StytchError` exceptions. Generic exception catching might mask specific API error details.
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
pip install stytch
Imports
- Client
import stytch client = stytch.Client(...)
- B2BClient
import stytch b2b_client = stytch.B2BClient(...)
- StytchError
from stytch import StytchError
Quickstart
import os
import stytch
# Initialize the Stytch client using environment variables
# Replace with your actual project_id and secret, or set as environment variables
# For B2C authentication:
client = stytch.Client(
project_id=os.environ.get("STYTCH_PROJECT_ID", "project-test-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
secret=os.environ.get("STYTCH_SECRET", "secret-test-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
# Optionally specify a custom base URL for all API calls
# custom_base_url="https://api.custom-domain.com/",
)
# For B2B authentication, use stytch.B2BClient instead:
# b2b_client = stytch.B2BClient(
# project_id=os.environ.get("STYTCH_PROJECT_ID", "project-test-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
# secret=os.environ.get("STYTCH_SECRET", "secret-test-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
# )
try:
# Example: Send a magic link for login or signup
email_to_authenticate = "test@example.com"
response = client.magic_links.email.login_or_create(
email=email_to_authenticate,
login_magic_link_url="http://localhost:3000/authenticate",
signup_magic_link_url="http://localhost:3000/authenticate",
)
print(f"Magic link sent to {email_to_authenticate}. Status code: {response.status_code}")
print(f"Request ID: {response.request_id}")
except stytch.StytchError as e:
print(f"Stytch API error occurred: {e.details}")
except Exception as e:
print(f"An unexpected error occurred: {e}")