Flask Quickstart

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

Overview

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

SDK

Development

Notes

Reference

Source Code

Python SDK

Server-side

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

GitHub

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:

STYTCH_PROJECT_ID="YOUR_STYTCH_PROJECT_ID"
STYTCH_SECRET="YOUR_STYTCH_PROJECT_SECRET"
# 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(
  project_id=os.environ['STYTCH_PROJECT_ID'],
  secret=os.environ['STYTCH_SECRET'],
)

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

@app.route('/authenticate', methods=['GET'])
def authenticate():
  token = request.args.get('token')
  try:
    resp = stytch_client.magic_links.discovery.authenticate(
      discovery_magic_links_token=token,
    )
    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__':
  app.run(debug=True)

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.