SuperTokens Python SDK

0.31.2 · active · verified Fri Apr 17

SuperTokens Python SDK, version 0.31.2, provides a comprehensive solution for user authentication and session management in Python applications. It integrates with various web frameworks and offers recipes like email/password, social login, and multi-factor authentication. Releases are frequent, with multiple patch and minor versions often appearing monthly to add features and fix bugs.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the SuperTokens SDK with application information, connection to the SuperTokens Core, and the desired authentication recipes. It's crucial to have the SuperTokens Core service running separately and accessible via `connection_uri`.

import os
import supertokens_python
from supertokens_python.recipe import session, emailpassword

# NOTE: This example requires a running SuperTokens Core service (e.g., at http://localhost:3567)

supertokens_python.init(
    app_info=supertokens_python.AppInfo(
        app_name="My SuperTokens App",
        api_domain="http://localhost:3001", # Your backend domain
        website_domain="http://localhost:3000" # Your frontend domain
    ),
    connection_uri=os.environ.get('SUPERTOKENS_CONNECTION_URI', 'http://localhost:3567'),
    recipes=[
        session.init(), # Provides session management
        emailpassword.init() # Provides email/password login
    ]
)

# In a web framework like Flask/FastAPI/Django, you would then integrate the middleware.
# Example for Flask (requires 'supertokens-python[flask]' installed):
# from flask import Flask
# from supertokens_python.framework.flask import get_middleware
# app = Flask(__name__)
# app.register_blueprint(get_middleware())
# @app.route('/test-session')
# @session.verify_session()
# def test_session():
#     s = session.get_session() # Get current session
#     return f"Hello user {s.get_user_id()}"

view raw JSON →