A basic Flask app that uses the WorkOS Python SDK to support Directory Sync.
- Python 3.6+
First, follow the Create a New Directory Connection step in the WorkOS Directory Sync guide.
If you get stuck, please reach out to us at support@workos.com so we can help.
-
In your CLI, navigate to the directory into which you want to clone this git repo.
$ cd ~/Desktop/
-
Clone this git repo using your preferred secure method (HTTPS or SSH).
# HTTPS $ git clone https://github.com/workos-inc/python-flask-directory-sync-example.git
or
# SSH
$ git clone git@github.com:workos-inc/python-flask-directory-sync-example.git
-
Navigate to the cloned repo.
$ cd python-flask-directory-sync-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
DIRECTORY_ID
, in the formatdirectory_<random-alphanumeric-string>
, retrievable from the URL in the Directory Sync area of the WorkOS dashboard:
-
Ensure you're in the root directory for the example app,
python-flask-directory-sync-example/
. 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.)(env) $ touch .env (env) $ nano .env
-
Once the Nano text editor opens, you can directly edit the
.env
file by listing the environment variables:export WORKOS_API_KEY=<value found in step 6> export DIRECTORY_ID=<value found in step 6>
To exit the Nano text editor, type
CTRL + x
. When prompted to "Save modified buffer", typeY
, then press theEnter
orReturn
key. -
Source the environment variables so they are accessible to the operating system.
(env) $ source .env
You 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 $DIRECTORY_ID
-
Use this command to run the app:
flask run
-
Once the server is running, navigate to http://localhost:5000 to view the home page of the app where you can then select the view for users or groups. Alternatively you could also view these directly using the following url's: http://localhost:5000/users and http://localhost:5000/groups.
- The
/users
URL corresponds to the WorkOS API's List Directory Users endpoint - The
/groups
URL corresponds to the WorkOS API's List Directory Groups endpoint - You can extend this Django example app by adding views to
directory_sync/views.py
for the other available Directory Sync API endpoints.
- The
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 support@workos.com and we'll help you out.