Setup
Welcome to the Python setup guide. If you're looking for guides for other frameworks, check out the Next.js SDK Setup, React SDK Setup, or the JavaScript SDK Setup.
Our recommended way to use Stack Auth with Python is with the REST API. It provides a fully documented way to interact with Stack Auth from any Python framework, including Flask, FastAPI, and Django.
For the purpose of this guide, we will use the requests
library to make HTTP requests to the Stack Auth API. If you haven't already, you can install it in your environment with pip install requests
.
Create API keys
First, create an account on the Stack Auth dashboard, and copy your project ID, publishable client key, and secret server key into a safe place (eg. environment variables).
From there, you can access them in your Python code. You can then read them like this:
import os
stack_project_id = os.getenv("STACK_PROJECT_ID")
stack_publishable_client_key = os.getenv("STACK_PUBLISHABLE_CLIENT_KEY")
stack_secret_server_key = os.getenv("STACK_SECRET_SERVER_KEY")
Make a request
Next, create a helper function to make requests to the Stack Auth API:
import requests
def stack_auth_request(method, endpoint, **kwargs):
res = requests.request(
method,
f'https://api.stack-auth.com/{endpoint}',
headers={
'x-stack-access-type': 'server', # or 'client' if you're only accessing the client API
'x-stack-project-id': stack_project_id,
'x-stack-publishable-client-key': stack_publishable_client_key,
'x-stack-secret-server-key': stack_secret_server_key, # not necessary if access type is 'client'
**kwargs.pop('headers', {}),
},
**kwargs,
)
if res.status_code >= 400:
raise Exception(f"Stack Auth API request failed with {res.status_code}: {res.text}")
return res.json()
print(stack_auth_request('GET', '/api/v1/projects/current'))
Handle user access tokens
If you're building a backend server, most likely you'll want to use the currently signed in user's access token. Most normally, you would send this with all your requests to the backend in an HTTP header.
In Stack Auth's JavaScript SDK, you can retrieve the access token from the stackClientApp
object. Then, you can use said access token to make requests to Stack Auth:
access_token = # access token retrieved from the JavaScript SDK
print(stack_auth_request('GET', '/api/v1/users/me', headers={
'x-stack-access-token': access_token,
}))
Here's how to extract and use the access token in your Python backend:
Extract access token from request headers:
def get_access_token_from_request(request):
"""Extract access token from x-stack-access-token header"""
return request.headers.get('x-stack-access-token')
Use the access token with Stack Auth:
def get_current_user_from_token(access_token):
"""Get current user information using their access token"""
return stack_auth_request('GET', '/api/v1/users/me', headers={
'x-stack-access-token': access_token,
})
Example usage in a Flask route:
@app.route('/api/user-profile')
def user_profile():
access_token = get_access_token_from_request(request)
if not access_token:
return {'error': 'No access token provided'}, 401
try:
user_info = get_current_user_from_token(access_token)
return {'user': user_info}
except Exception as e:
return {'error': 'Invalid access token'}, 401
Next steps
Now that you have Stack Auth set up in your Python application, you can:
- User Authentication - Learn how to sign up and sign in users
- Teams Management - Implement team functionality
Check out the REST API documentation to learn more about the available endpoints and how to use them in your Python application.