Flask Quickstart

This quickstart guide outlines the essential steps to integrate Stytch’s B2B SaaS Authentication product within a Python Flask application.


Stytch offers a Python SDK that can be integrated within Flask applications:





Source Code

Python SDK


For server-side code. Our Python SDK facilitates robust authentication and authorization mechanisms and can be integrated directly with Flask apps to handle backend logic.

SDK Reference


In addition to our Python SDK, we recommend you consider using one of Stytch's frontend SDKs (headless or with pre-built UI) to build your client-side auth UI.

Learn more about different integration methods and their benefits by visiting our integration guides.

Getting Started

To begin, we'll set up Email Magic Links utilizing our Discovery flow.

1Install Stytch SDK and configure your API Keys

Create a Stytch B2B Project in your Stytch Dashboard if you haven't already.

Install our Python SDK in your Flask environment:

pip install stytch

Configure your Stytch Project's API keys as environment variables:

# Use your Project's 'test' or 'live' credentials

2Set up your Flask app

Initialize the Stytch client in your Flask app and set up routes to handle authentication:

from flask import Flask, request, redirect
from stytch import Client
import os 

app = Flask(__name__)
stytch_client = Client(

@app.route('/login', methods=['POST'])
def login():
  email = request.form['email']
    resp = stytch_client.magic_links.email.discovery.send(
    return jsonify(resp)
  except Exception as e:
    return str(e)

@app.route('/authenticate', methods=['GET'])
def authenticate():
  token = request.args.get('token')
    resp = stytch_client.magic_links.discovery.authenticate(
    return f"Hello, {resp.email_address}! Complete the Discovery flow by creating an Organization with your intermediate session token: {resp.intermediate_session_token}"
  except Exception as e:
    return str(e)

if __name__ == '__main__':

For Email Magic Links, you must specify a redirect URL in your Project's Dashboard to authenticate the token. By default, the redirect URL is set to http://localhost:3000/authenticate.

You can specify additional Redirect URLs in your Project's Dashboard, and override the default by passing in an explicit discovery_redirect_url argument.

You can read more about redirect URLs in this guide.

3Test your application

Run your Flask application and send a POST request to the /login endpoint with an email address to test the Discovery auth flow. You will recieve an email in your inbox with an Email Magic Link, which redirects you to the /authenticate endpoint or the redirect URL you set in your [Dashboard].

4What's next

Check out our product-specific guides for how to handle full authentication flows for each product you'd like to support, like Email Magic Links and OAuth.