If you have more than 200 users in your Okta org, you can’t get them all back at once; you have to paginate.

I find myself doing this all the time, so here’s the specific Python snippet I use to retrieve all active Okta users.

Hopefully someone else finds this useful!

import os
from urllib.parse import parse_qs, urlparse

import requests


def fetch_okta_users():
    api_token = os.environ['OKTA_API_KEY']
    url = "https://{}/api/v1/users".format(os.environ['OKTA_API_BASE_URL'])

    api_request_headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': "SSWS {}".format(api_token)
    }

    okta_users = []
    done = False

    after_token = None
    while not done:
        params = {
            'limit': 200,
        }

        if after_token:
            params['after'] = after_token

        list_users_response = requests.get(
            url=url,
            params=params,
            headers=api_request_headers
        )

        if list_users_response.status_code != 200:
            raise Exception("Got HTTP {} listing users",
                            list_users_response.status_code)

        list_users_response_body = list_users_response.json()

        okta_users.extend(
            [x for x in list_users_response_body if x['status'] == 'ACTIVE'])

        if 'next' in list_users_response.links:
            next_url = list_users_response.links['next']['url']
            after_token = parse_qs(urlparse(next_url).query)['after'][0]
        else:
            done = True

    return okta_users