PropelAuth Python SDK
PropelAuth is a Python library for managing authentication and authorization in B2B/multi-tenant applications. It provides features like user login, signup, organization management, roles, permissions, and API key authentication. The library simplifies backend authorization with hosted UIs and a developer-friendly SDK. It maintains an active release cadence with frequent updates for new features and improvements. Current version is 4.3.2.
Common errors
-
TypeError: 'User' object is not subscriptable
cause Attempting to unpack a PropelAuth response object (like the `User` object) using dictionary unpacking syntax (`**`) after it was changed to an explicit datatype in v4.x.fixAccess user attributes directly (e.g., `user.user_id`, `user.email`). If you need a dictionary representation, you might need to convert it explicitly if the library doesn't provide a `.to_dict()` method. -
propelauth_py.errors.UnauthorizedException: Invalid access token
cause The provided Authorization header is missing, malformed, or contains an expired/invalid access token. It can also occur if the `AUTH_URL` or `API_KEY` used during initialization are incorrect.fixEnsure the `Authorization` header is present and in the format `Bearer {TOKEN}`. Verify that your `AUTH_URL` and `API_KEY` are correct and match your PropelAuth project settings. Check if the token itself is valid and not expired. -
ModuleNotFoundError: No module named 'propelauth_py'
cause The `propelauth-py` library is not installed in the current Python environment or is not included in the deployment package for serverless functions (e.g., AWS Lambda with Chalice).fixRun `pip install propelauth-py` to install the library. For serverless applications, ensure `propelauth-py` is listed in your `requirements.txt` file (or equivalent) so it gets bundled with your deployment.
Warnings
- breaking In v4.x, response objects (e.g., from validation or API calls) are now explicit datatypes with proper type hints instead of plain dictionaries. Attempting to unpack a response using the `**` operator will result in a `TypeError`.
- gotcha The core `propelauth-py` library provides general authentication functionalities. However, for specific web frameworks like FastAPI, Flask, or Django REST Framework, dedicated libraries (`propelauth-fastapi`, `propelauth-flask`, `propelauth-django-rest-framework`) offer a more integrated and 'first-class' experience for route protection and user handling.
- gotcha By default, the PropelAuth Python library logs exceptions using Python's standard logging module. This might lead to sensitive information in logs if not properly managed.
- gotcha When using asynchronous operations, remember to use the `_async` suffix for initialization functions (e.g., `init_base_async_auth` instead of `init_base_auth`). All subsequent API calls will then be asynchronous.
Install
-
pip install propelauth-py
Imports
- init_base_auth
from propelauth_py import init_base_auth
- init_base_async_auth
from propelauth_py import init_base_async_auth
- UnauthorizedException
from propelauth_py.errors import UnauthorizedException
- User
from propelauth_py import User
Quickstart
import os
from propelauth_py import init_base_auth, UnauthorizedException
# Your PropelAuth Auth URL and API Key from your PropelAuth dashboard
AUTH_URL = os.environ.get('PROPELAUTH_AUTH_URL', 'YOUR_AUTH_URL')
API_KEY = os.environ.get('PROPELAUTH_API_KEY', 'YOUR_API_KEY')
if not AUTH_URL or AUTH_URL == 'YOUR_AUTH_URL' or not API_KEY or API_KEY == 'YOUR_API_KEY':
print("Please set PROPELAUTH_AUTH_URL and PROPELAUTH_API_KEY environment variables")
exit(1)
try:
auth = init_base_auth(AUTH_URL, API_KEY)
# Simulate an Authorization header from an incoming request
# In a real application, this would come from a client request
mock_auth_header = "Bearer a_mock_jwt_token"
# Validate the access token and get user information
user = auth.validate_access_token_and_get_user(mock_auth_header)
print(f"Successfully authenticated user: {user.user_id}")
print(f"User email: {user.email}")
if user.orgs:
print("User belongs to organizations:")
for org_member_info in user.orgs:
print(f" - Org ID: {org_member_info.org_id}, Name: {org_member_info.org_name}, Roles: {org_member_info.roles}")
# Example of calling a backend API to create a magic link
# Note: This requires appropriate permissions on your API key
# magic_link_response = auth.create_magic_link("test@example.com")
# print(f"Magic link URL: {magic_link_link.url}")
except UnauthorizedException:
print("Authentication failed: Invalid access token or configuration.")
except Exception as e:
print(f"An error occurred: {e}")