Descope Python SDK
The Descope Python SDK provides convenient access to Descope's user management and authentication APIs for backend applications. It facilitates secure user authentication, session management, and various identity-related operations. The library is actively maintained with frequent updates, ensuring compatibility with the latest Descope platform features.
Warnings
- gotcha The `DescopeClient` requires a `project_id` for initialization. Failing to provide this will lead to an `AuthException` during client creation or subsequent API calls.
- gotcha If public access to authentication APIs is disabled in your Descope project settings, you must provide a management key to perform authentication API calls. Without it, these calls will fail.
- gotcha When using a custom domain for your Descope project, you must explicitly set the `base_url` during `DescopeClient` initialization. Otherwise, the SDK will attempt to use the default Descope API endpoint, leading to incorrect routing.
- gotcha API requests can fail with 'Invalid JSON Payload' errors if the JSON data sent is malformed. Common mistakes include missing commas, incorrect use of brackets/braces, or using single quotes instead of double quotes for keys and string values.
Install
-
pip install descope
Imports
- DescopeClient
from descope import DescopeClient
- AuthException
from descope import AuthException
- SESSION_TOKEN_NAME, REFRESH_SESSION_TOKEN_NAME
from descope import SESSION_TOKEN_NAME, REFRESH_SESSION_TOKEN_NAME
Quickstart
import os
from descope import DescopeClient, AuthException
# Ensure DESCOPE_PROJECT_ID and optionally DESCOPE_AUTH_MANAGEMENT_KEY are set as environment variables
project_id = os.environ.get('DESCOPE_PROJECT_ID', '')
management_key = os.environ.get('DESCOPE_AUTH_MANAGEMENT_KEY', '')
# Optional: If using a custom domain
base_url = os.environ.get('DESCOPE_BASE_URI', '')
if not project_id:
print("Error: DESCOPE_PROJECT_ID environment variable is not set.")
exit(1)
try:
# Initialize the DescopeClient
# If base_url is an empty string, it will default to Descope's API endpoint
descope_client = DescopeClient(project_id=project_id, auth_management_key=management_key, base_url=base_url)
print("DescopeClient initialized successfully.")
# Example: Validate a session token (replace 'your_session_token' with an actual token)
# In a real application, this token would come from an incoming request's Authorization header or cookie
test_session_token = "your_session_token"
if test_session_token == "your_session_token":
print("Warning: Please replace 'your_session_token' with a real session token for validation.")
try:
session_jwt = descope_client.validate_session(session_token=test_session_token)
print(f"Session validated successfully. User ID: {session_jwt['sub']}")
except AuthException as e:
print(f"Session validation failed: {e}")
except Exception as e:
print(f"An unexpected error occurred during session validation: {e}")
except AuthException as e:
print(f"Failed to initialize DescopeClient due to authentication error: {e}")
except Exception as e:
print(f"An unexpected error occurred during DescopeClient initialization: {e}")