Add Magic Links to Your Django App Using WorkOS

November 9, 2021

If your SaaS product is built with Python’s Django web framework and you want to incorporate Magic Links into your users’ sign-on experience, you can do a dry-run of WorkOS’ Magic Links integration using our example Django app, which makes use of the WorkOS Python SDK.

If you get stuck while following the steps below and aren't able to resolve the issue by reading our API reference or Magic Link Setup Guide, reach out to us at [email protected] so we can help!

Prerequisites

Clone Directory

1. In your CLI, navigate to the directory into which you want to clone this git repo.


2. Clone the main Python Django example app repo using your preferred secure method (HTTPS or SSH).


3. Navigate to the Magic Link example app within the cloned repo.


Install Dependencies


4. Create and source a Python virtual environment. You should then see (env) at the beginning of your command-line prompt.


5. Navigate to the Django Magic Links example app directory and install the cloned app's dependencies.


Set Environment Variables

6. Obtain and make note of your WorkOS API key and SSO-specific, WorkOS Client ID from the WorkOS Dashboard. The locations of these values are shown in the screenshots below.


7. Ensure you're in the root app directory for the example app, magic_link_example/ where the manage.py file resides. Create a .env file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's .gitignore file, so your sensitive information will not be checked into version control.)


8. Once the Nano text editor opens, you can directly edit the .env file by listing the environment variables:


To exit the Nano text editor, type CTRL + x. When prompted to "Save modified buffer", type Y, then press the Enter or Return key.


9. Source the environment variables so they are accessible to the operating system.


You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.


10. Run the Django migrations. Again, ensure you're in the magic_link_example/ directory where the manange.py file is.


You should see output like:


11. Update the default Redirect URI in the "Configuration" page of your WorkOS Dashboard. The URL to use is http://localhost:8000/success.


Start the server


12. Start the server. To serve static files in development while still having debug=True in settings.py while still being able to send requests to WorkOS, be sure to include the --insecure flag when starting the server locally.


You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed:


13. With the Django server running, navigate to http://localhost:8000/ and enter the email address to which you want to send the Magic Link.

14. You’ll see an example webpage that directs the user to check their email. For the sake of the tutorial, the redirect URL is also shown:

15. You can either click the link in the email or click the linked redirect URL to navigate to the “success” page, which looks like this:

Need help?

When you clone this repo, the DEBUG setting is False by default in app.py. You can set DEBUG = True if you need to troubleshoot something during the tutorial, but you must use DEBUG = False in order to successfully connect to the WorkOS API.

If you get stuck and aren't able to resolve the issue by reading our API reference or tutorials, please  reach out to us at [email protected] and we'll help you out.

Start Integrating Today
Create an account to begin adding enterprise-ready features to your application today.
Get Started

This site uses cookies to improve your experience. Please accept the use of cookies on this site. You can review our cookie policy here and our privacy policy here. If you choose to refuse, functionality of this site will be limited.