|
3 | 3 |  |
4 | 4 | [](https://workos.semaphoreci.com/projects/workos-python) |
5 | 5 |
|
6 | | -The WorkOS library for Python provides convenient access to the WorkOS API from applications written in Python, [hosted on PyPi](https://pypi.org/project/workos/) |
| 6 | +The WorkOS library for Python provides convenient access to the WorkOS API from applications written in Python, [hosted on PyPI](https://pypi.org/project/workos/). |
7 | 7 |
|
8 | 8 | ## Documentation |
9 | 9 |
|
10 | 10 | See the [API Reference](https://workos.com/docs/reference/client-libraries) for Python usage examples. |
11 | 11 |
|
12 | 12 | ## Installation |
13 | 13 |
|
14 | | -To install from PyPi, run the following: |
15 | | - |
16 | | -``` |
| 14 | +```bash |
17 | 15 | pip install workos |
18 | 16 | ``` |
19 | 17 |
|
20 | | -To install from source, clone the repo and run the following: |
| 18 | +## Quick Start |
21 | 19 |
|
22 | | -``` |
23 | | -python -m pip install . |
| 20 | +```python |
| 21 | +from workos import WorkOSClient |
| 22 | + |
| 23 | +client = WorkOSClient(api_key="sk_1234", client_id="client_1234") |
| 24 | + |
| 25 | +# List organizations |
| 26 | +page = client.organizations.list_organizations() |
| 27 | +for org in page.auto_paging_iter(): |
| 28 | + print(org.name) |
| 29 | + |
| 30 | +# Create an organization |
| 31 | +org = client.organizations.create_organizations(name="Acme Corp") |
| 32 | +print(org.id) |
24 | 33 | ``` |
25 | 34 |
|
26 | | -## Configuration |
| 35 | +### Async Client |
27 | 36 |
|
28 | | -The package will need to be configured with your [api key and client ID](https://dashboard.workos.com/api-keys). |
| 37 | +Every method has an identical async counterpart: |
29 | 38 |
|
30 | 39 | ```python |
31 | | -from workos import WorkOSClient |
| 40 | +from workos import AsyncWorkOSClient |
32 | 41 |
|
33 | | -workos_client = WorkOSClient( |
34 | | - api_key="sk_1234", client_id="client_1234" |
35 | | -) |
| 42 | +async_client = AsyncWorkOSClient(api_key="sk_1234", client_id="client_1234") |
| 43 | + |
| 44 | +page = await async_client.organizations.list_organizations() |
| 45 | +async for org in page.auto_paging_iter(): |
| 46 | + print(org.name) |
36 | 47 | ``` |
37 | 48 |
|
38 | | -The SDK also provides asyncio support for some SDK methods, via the async client: |
| 49 | +### Environment Variables |
| 50 | + |
| 51 | +The client reads credentials from the environment when not passed explicitly: |
| 52 | + |
| 53 | +| Variable | Description | |
| 54 | +|----------|-------------| |
| 55 | +| `WORKOS_API_KEY` | WorkOS API key | |
| 56 | +| `WORKOS_CLIENT_ID` | WorkOS client ID | |
| 57 | +| `WORKOS_BASE_URL` | Override the API base URL (defaults to `https://api.workos.com/`) | |
| 58 | +| `WORKOS_REQUEST_TIMEOUT` | HTTP timeout in seconds (defaults to `60`) | |
| 59 | + |
| 60 | +## Available Resources |
| 61 | + |
| 62 | +The client exposes the full WorkOS API through typed namespace properties: |
| 63 | + |
| 64 | +| Property | Description | |
| 65 | +|----------|-------------| |
| 66 | +| `client.sso` | Single Sign-On connections and authorization | |
| 67 | +| `client.organizations` | Organization management | |
| 68 | +| `client.user_management` | Users, identities, auth methods, invitations | |
| 69 | +| `client.directory_sync` | Directory connections and directory users/groups | |
| 70 | +| `client.admin_portal` | Admin Portal link generation | |
| 71 | +| `client.audit_logs` | Audit log events, exports, and schemas | |
| 72 | +| `client.authorization` | Fine-Grained Authorization (FGA) resources, roles, permissions, and checks | |
| 73 | +| `client.webhooks` | Webhook event verification | |
| 74 | +| `client.feature_flags` | Feature flag evaluation | |
| 75 | +| `client.api_keys` | Organization API key management | |
| 76 | +| `client.connect` | OAuth application management | |
| 77 | +| `client.widgets` | Widget session tokens | |
| 78 | +| `client.multi_factor_auth` | MFA enrollment and verification (also available as `client.mfa`) | |
| 79 | +| `client.pipes` | Data Integrations | |
| 80 | +| `client.radar` | Radar risk scoring | |
| 81 | +| `client.passwordless` | Passwordless authentication sessions | |
| 82 | +| `client.vault` | Encrypted data vault | |
| 83 | + |
| 84 | +## Pagination |
| 85 | + |
| 86 | +Paginated endpoints return `SyncPage[T]` (or `AsyncPage[T]`) with built-in auto-pagination: |
39 | 87 |
|
40 | 88 | ```python |
41 | | -from workos import AsyncWorkOSClient |
| 89 | +# Iterate through all pages automatically |
| 90 | +for user in client.user_management.list_users().auto_paging_iter(): |
| 91 | + print(user.email) |
| 92 | + |
| 93 | +# Or work with a single page |
| 94 | +page = client.user_management.list_users(limit=10) |
| 95 | +print(page.data) # List of items on this page |
| 96 | +print(page.has_more()) # Whether more pages exist |
| 97 | +print(page.after) # Cursor for the next page |
| 98 | +``` |
| 99 | + |
| 100 | +## Error Handling |
| 101 | + |
| 102 | +All API errors map to typed exception classes with rich context: |
| 103 | + |
| 104 | +```python |
| 105 | +from workos._errors import NotFoundError, RateLimitExceededError |
| 106 | + |
| 107 | +try: |
| 108 | + client.organizations.get_organization("org_nonexistent") |
| 109 | +except NotFoundError as e: |
| 110 | + print(f"Not found: {e.message}") |
| 111 | + print(f"Request ID: {e.request_id}") |
| 112 | +except RateLimitExceededError as e: |
| 113 | + print(f"Retry after: {e.retry_after} seconds") |
| 114 | +``` |
| 115 | + |
| 116 | +| Exception | Status Code | |
| 117 | +|-----------|-------------| |
| 118 | +| `BadRequestError` | 400 | |
| 119 | +| `AuthenticationError` | 401 | |
| 120 | +| `AuthorizationError` | 403 | |
| 121 | +| `NotFoundError` | 404 | |
| 122 | +| `ConflictError` | 409 | |
| 123 | +| `UnprocessableEntityError` | 422 | |
| 124 | +| `RateLimitExceededError` | 429 | |
| 125 | +| `ServerError` | 5xx | |
42 | 126 |
|
43 | | -async_workos_client = AsyncWorkOSClient( |
44 | | - api_key="sk_1234", client_id="client_1234" |
| 127 | +## Per-Request Options |
| 128 | + |
| 129 | +Every method accepts `request_options` for per-call overrides: |
| 130 | + |
| 131 | +```python |
| 132 | +result = client.organizations.list_organizations( |
| 133 | + request_options={ |
| 134 | + "timeout": 10, |
| 135 | + "max_retries": 5, |
| 136 | + "extra_headers": {"X-Custom": "value"}, |
| 137 | + "idempotency_key": "my-key", |
| 138 | + "base_url": "https://staging.workos.com/", |
| 139 | + } |
45 | 140 | ) |
46 | 141 | ``` |
47 | 142 |
|
| 143 | +## Type Safety |
| 144 | + |
| 145 | +This SDK ships with full type annotations (`py.typed` / PEP 561) and works with mypy, pyright, and IDE autocompletion out of the box. All models are `@dataclass(slots=True)` classes with `from_dict()` / `to_dict()` for serialization. |
| 146 | + |
48 | 147 | ## SDK Versioning |
49 | 148 |
|
50 | | -For our SDKs WorkOS follows a Semantic Versioning ([SemVer](https://semver.org/)) process where all releases will have a version X.Y.Z (like 1.0.0) pattern wherein Z would be a bug fix (e.g., 1.0.1), Y would be a minor release (1.1.0) and X would be a major release (2.0.0). We permit any breaking changes to only be released in major versions and strongly recommend reading changelogs before making any major version upgrades. |
| 149 | +WorkOS follows [Semantic Versioning](https://semver.org/). Breaking changes are only released in major versions. We strongly recommend reading changelogs before making major version upgrades. |
51 | 150 |
|
52 | 151 | ## Beta Releases |
53 | 152 |
|
54 | | -WorkOS has features in Beta that can be accessed via Beta releases. We would love for you to try these |
55 | | -and share feedback with us before these features reach general availability (GA). To install a Beta version, |
56 | | -please follow the [installation steps](#installation) above using the Beta release version. |
57 | | - |
58 | | -> Note: there can be breaking changes between Beta versions. Therefore, we recommend pinning the package version to a |
59 | | -> specific version. This way you can install the same version each time without breaking changes unless you are |
60 | | -> intentionally looking for the latest Beta version. |
| 153 | +WorkOS has features in Beta that can be accessed via Beta releases. We would love for you to try these and share feedback with us before these features reach general availability (GA). To install a Beta version, please follow the [installation steps](#installation) above using the Beta release version. |
61 | 154 |
|
62 | | -We highly recommend keeping an eye on when the Beta feature you are interested in goes from Beta to stable so that you |
63 | | -can move to using the stable version. |
| 155 | +> **Note:** there can be breaking changes between Beta versions. We recommend pinning the package version to a specific version. |
64 | 156 |
|
65 | 157 | ## More Information |
66 | 158 |
|
67 | 159 | - [Single Sign-On Guide](https://workos.com/docs/sso/guide) |
| 160 | +- [User Management Guide](https://workos.com/docs/user-management) |
| 161 | +- [AuthKit Guide](https://workos.com/docs/authkit) |
68 | 162 | - [Directory Sync Guide](https://workos.com/docs/directory-sync/guide) |
69 | 163 | - [Admin Portal Guide](https://workos.com/docs/admin-portal/guide) |
70 | | -- [Magic Link Guide](https://workos.com/docs/magic-link/guide) |
| 164 | +- [Audit Logs Guide](https://workos.com/docs/audit-logs) |
| 165 | +- [Authorization (FGA) Guide](https://workos.com/docs/fga) |
| 166 | +- [Feature Flags Guide](https://workos.com/docs/feature-flags) |
| 167 | +- [Webhooks Guide](https://workos.com/docs/webhooks) |
| 168 | +- [Radar Guide](https://workos.com/docs/radar) |
0 commit comments