An example Flask application demonstrating how to use the WorkOS Python SDK to authenticate users via Magic Link.
- Python 3.6+
-
Clone the main git repo for these Python example apps using your preferred secure method (HTTPS or SSH).
# HTTPS $ git clone https://github.com/workos/python-flask-example-applications.gitor
# SSH $ git clone git@github.com:workos/python-flask-example-applications.git -
Navigate to the Magic Link example app within the cloned repo.
$ cd python-flask-example-applications/python-flask-magic-link-example -
Create and source a Python virtual environment. You should then see
(env)at the beginning of your command-line prompt.$ python3 -m venv env $ source env/bin/activate (env) $ -
Install the cloned app's dependencies.
(env) $ pip install -r requirements.txt
-
Obtain and make note of the following values. In the next step, these will be set as environment variables.
- Your WorkOS API key
- Your SSO-specific, WorkOS Client ID
-
Ensure you're in the root directory for the example app,
python-flask-sso-example/. Create a.envfile to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's.gitignorefile, so your sensitive information will not be checked into version control.)(env) $ touch .env (env) $ nano .env
-
Once the Nano text editor opens, you can directly edit the
.envfile by listing the environment variables:export WORKOS_API_KEY=<value found in step 6> export WORKOS_CLIENT_ID=<value found in step 6>
To exit the Nano text editor, type
CTRL + x. When prompted to "Save modified buffer", typeY, then press theEnterorReturnkey. -
Source the environment variables so they are accessible to the operating system.
(env) $ source .envYou can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.
(env) $ echo $WORKOS_API_KEY (env) $ echo $WORKOS_CLIENT_ID
-
The final setup step is to start the server.
(env) $ flask run
If you are using macOS Monterey, port 5000 is not available and you'll need to start the app on a different port with this slightly different command.
(env) $ flask run -p 5001
You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed:
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Navigate to
localhost:5000, orlocalhost:5001depending on which port you launched the server, in your web browser. You should see a "Login" button.You can stop the local Flask server for now by entering
CTRL + con the command line.
Change the default REDIRECT_URI value in the "Configuration" page of your WorkOS dashboard to http://localhost:5000/success, or http://localhost:5001/success depending on which port you launched the server.
- Naviagte to http://localhost:5000, or
http://localhost:5001/successdepending on which port you launched the server, to test out the Magic Link flow!
If you get stuck and aren't able to resolve the issue by reading our API reference or tutorials, you can reach out to us at support@workos.com and we'll lend a hand.