WorkOS Python Client

5.46.0 · active · verified Thu Apr 09

WorkOS Python Client provides a comprehensive SDK to interact with the WorkOS API for enterprise-ready features like Single Sign-On (SSO), Directory Sync (SCIM), Multi-Factor Authentication (MFA), Admin Portal, and Audit Logs. It simplifies integration with various identity providers and directory services. The library is actively maintained with frequent updates, often on a weekly or bi-weekly cadence. The current version is 5.46.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the WorkOS Python Client using environment variables for API keys and client IDs. It then shows examples of listing organizations, generating an SSO authorization URL, and verifying a webhook event. Remember to replace placeholder API keys, client IDs, domains, and redirect URIs with your actual WorkOS credentials and configurations. Webhook verification is critical and requires a valid signature and secret.

import os
import workos
from pprint import pprint

# Configure WorkOS using environment variables
# For local testing, replace with actual test keys if env vars are not set
workos.api_key = os.environ.get("WORKOS_API_KEY", "sk_test_YOUR_API_KEY")
workos.client_id = os.environ.get("WORKOS_CLIENT_ID", "client_test_YOUR_CLIENT_ID")

# Optional: Set the API base URL for custom environments (e.g., local development)
# workos.base_api_url = "http://localhost:8000/"

# Example 1: List organizations
try:
    print("\n--- Listing Organizations ---")
    organizations = workos.organizations.list_organizations(limit=5)
    if organizations.data:
        for org in organizations.data:
            pprint(org.to_dict())
    else:
        print("No organizations found.")
except Exception as e:
    print(f"Error listing organizations: {e}")

# Example 2: Generate an SSO authorization URL
try:
    print("\n--- Generating SSO Authorization URL ---")
    authorization_url = workos.sso.get_authorization_url(
        domain="example.com", # Replace with a domain configured in WorkOS
        redirect_uri="http://localhost:8000/callback", # Must be a valid redirect URI configured in WorkOS
        state="some-secure-random-state"
    )
    print(f"SSO Authorization URL for example.com: {authorization_url}")
except Exception as e:
    print(f"Error generating SSO URL: {e}")

# Example 3: Verify a webhook event (simulated payload)
try:
    print("\n--- Simulating Webhook Verification ---")
    # In a real scenario, payload and signature_header come from the HTTP request
    webhook_payload = '{"id": "wh_01G827B6V8F1PZ1D7E40P5940X", "event": "directory_sync.user.created", "data": {}}'
    webhook_signature = "t=1678886400,v1=signature_hash"
    webhook_secret = os.environ.get("WORKOS_WEBHOOK_SECRET", "webhook_secret_YOUR_WEBHOOK_SECRET")

    # This will likely fail with a dummy signature, but demonstrates the call
    print("Attempting to verify webhook (will likely fail with dummy data)...")
    event = workos.webhooks.verify_event(
        payload=webhook_payload,
        signature_header=webhook_signature,
        secret=webhook_secret
    )
    print(f"Webhook event verified: {event['event']}")
except workos.errors.WebhookSignatureError as e:
    print(f"Webhook signature verification failed: {e}")
except Exception as e:
    print(f"Error during webhook verification simulation: {e}")

view raw JSON →