| 1 | # Auth0 Python Library |
| 2 | |
| 3 |  |
| 4 | |
| 5 |  |
| 6 | [](https://codecov.io/gh/auth0/auth0-python) |
| 7 | [](https://deepwiki.com/auth0/auth0-python) |
| 8 |  |
| 9 | [](https://opensource.org/licenses/MIT) |
| 10 | [](https://circleci.com/gh/auth0/auth0-python) |
| 11 | [](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fauth0%2Fauth0-python) |
| 12 | |
| 13 | The Auth0 Python library provides convenient access to the Auth0 APIs from Python. |
| 14 | |
| 15 | ## Table of Contents |
| 16 | |
| 17 | - [Installation](#installation) |
| 18 | - [Reference](#reference) |
| 19 | - [Authentication API](#authentication-api) |
| 20 | - [Management API](#management-api) |
| 21 | - [Async Client](#async-client) |
| 22 | - [Exception Handling](#exception-handling) |
| 23 | - [Pagination](#pagination) |
| 24 | - [Advanced](#advanced) |
| 25 | - [Access Raw Response Data](#access-raw-response-data) |
| 26 | - [Retries](#retries) |
| 27 | - [Timeouts](#timeouts) |
| 28 | - [Custom Client](#custom-client) |
| 29 | - [Feedback](#feedback) |
| 30 | |
| 31 | ## Installation |
| 32 | ```sh |
| 33 | pip install auth0-python |
| 34 | ``` |
| 35 | |
| 36 | **Requirements:** |
| 37 | - Python ≥3.10 (Python 3.9 support has been dropped) |
| 38 | |
| 39 | ## Reference |
| 40 | |
| 41 | A full reference for this library is available [here](https://github.com/auth0/auth0-python/blob/master/reference.md). |
| 42 | |
| 43 | ## Authentication API |
| 44 | |
| 45 | The Authentication API is used for authentication flows such as obtaining tokens via client credentials, authorization codes, or resource owner password grants: |
| 46 | |
| 47 | ```python |
| 48 | from auth0.authentication import GetToken |
| 49 | |
| 50 | token_client = GetToken( |
| 51 | domain="your-tenant.auth0.com", |
| 52 | client_id="YOUR_CLIENT_ID", |
| 53 | client_secret="YOUR_CLIENT_SECRET", |
| 54 | ) |
| 55 | |
| 56 | # Get an access token using client credentials |
| 57 | token_response = token_client.client_credentials( |
| 58 | audience="https://your-tenant.auth0.com/api/v2/" |
| 59 | ) |
| 60 | access_token = token_response["access_token"] |
| 61 | ``` |
| 62 | |
| 63 | ## Management API |
| 64 | |
| 65 | ### Recommended: Using ManagementClient |
| 66 | |
| 67 | The `ManagementClient` is the recommended way to interact with the Auth0 Management API. It provides a simpler interface using just your Auth0 domain, and supports automatic token management with client credentials: |
| 68 | |
| 69 | ```python |
| 70 | from auth0.management import ManagementClient |
| 71 | |
| 72 | # With an existing token |
| 73 | client = ManagementClient( |
| 74 | domain="your-tenant.auth0.com", |
| 75 | token="YOUR_TOKEN", |
| 76 | ) |
| 77 | |
| 78 | # Or with client credentials (automatic token acquisition and refresh) |
| 79 | client = ManagementClient( |
| 80 | domain="your-tenant.auth0.com", |
| 81 | client_id="YOUR_CLIENT_ID", |
| 82 | client_secret="YOUR_CLIENT_SECRET", |
| 83 | ) |
| 84 | ``` |
| 85 | |
| 86 | For async usage: |
| 87 | |
| 88 | ```python |
| 89 | import asyncio |
| 90 | from auth0.management import AsyncManagementClient |
| 91 | |
| 92 | client = AsyncManagementClient( |
| 93 | domain="your-tenant.auth0.com", |
| 94 | token="YOUR_TOKEN", |
| 95 | ) |
| 96 | |
| 97 | async def main() -> None: |
| 98 | users = await client.users.list() |
| 99 | print(users) |
| 100 | |
| 101 | asyncio.run(main()) |
| 102 | ``` |
| 103 | |
| 104 | ### Using a Token from the Authentication API |
| 105 | |
| 106 | You can obtain a token using the Authentication API and use it with the Management API client: |
| 107 | |
| 108 | ```python |
| 109 | from auth0.authentication import GetToken |
| 110 | from auth0.management import Auth0 |
| 111 | |
| 112 | domain = "your-tenant.auth0.com" |
| 113 | |
| 114 | # Get a token using the Authentication API |
| 115 | token_client = GetToken( |
| 116 | domain=domain, |
| 117 | client_id="YOUR_CLIENT_ID", |
| 118 | client_secret="YOUR_CLIENT_SECRET", |
| 119 | ) |
| 120 | token_response = token_client.client_credentials( |
| 121 | audience=f"https://{domain}/api/v2/" |
| 122 | ) |
| 123 | access_token = token_response["access_token"] |
| 124 | |
| 125 | # Use the token with the Management API client |
| 126 | client = Auth0( |
| 127 | base_url=f"https://{domain}/api/v2", |
| 128 | token=access_token, |
| 129 | ) |
| 130 | ``` |
| 131 | |
| 132 | ### Using the Base Client |
| 133 | |
| 134 | Alternatively, you can use the `Auth0` client directly with a full base URL: |
| 135 | |
| 136 | ```python |
| 137 | from auth0.management import ActionTrigger, Auth0 |
| 138 | |
| 139 | client = Auth0( |
| 140 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 141 | token="YOUR_TOKEN", |
| 142 | ) |
| 143 | client.actions.create( |
| 144 | name="name", |
| 145 | supported_triggers=[ |
| 146 | ActionTrigger( |
| 147 | id="id", |
| 148 | ) |
| 149 | ], |
| 150 | ) |
| 151 | ``` |
| 152 | |
| 153 | ## Async Client |
| 154 | |
| 155 | The SDK also exports an `async` client so that you can make non-blocking calls to our API. Note that if you are constructing an Async httpx client class to pass into this client, use `httpx.AsyncClient()` instead of `httpx.Client()` (e.g. for the `httpx_client` parameter of this client). |
| 156 | |
| 157 | ```python |
| 158 | import asyncio |
| 159 | |
| 160 | from auth0.management import ActionTrigger, AsyncAuth0 |
| 161 | |
| 162 | client = AsyncAuth0( |
| 163 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 164 | token="YOUR_TOKEN", |
| 165 | ) |
| 166 | |
| 167 | |
| 168 | async def main() -> None: |
| 169 | await client.actions.create( |
| 170 | name="name", |
| 171 | supported_triggers=[ |
| 172 | ActionTrigger( |
| 173 | id="id", |
| 174 | ) |
| 175 | ], |
| 176 | ) |
| 177 | |
| 178 | |
| 179 | asyncio.run(main()) |
| 180 | ``` |
| 181 | |
| 182 | ## Exception Handling |
| 183 | |
| 184 | When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 185 | will be thrown. |
| 186 | |
| 187 | ```python |
| 188 | from auth0.management.core.api_error import ApiError |
| 189 | |
| 190 | try: |
| 191 | client.actions.create(...) |
| 192 | except ApiError as e: |
| 193 | print(e.status_code) |
| 194 | print(e.body) |
| 195 | ``` |
| 196 | |
| 197 | ## Pagination |
| 198 | |
| 199 | Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object. |
| 200 | |
| 201 | ```python |
| 202 | from auth0.management import Auth0 |
| 203 | |
| 204 | client = Auth0( |
| 205 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 206 | token="YOUR_TOKEN", |
| 207 | ) |
| 208 | response = client.actions.list( |
| 209 | trigger_id="post-login", |
| 210 | action_name="actionName", |
| 211 | deployed=True, |
| 212 | page=1, |
| 213 | per_page=1, |
| 214 | installed=True, |
| 215 | ) |
| 216 | for item in response: |
| 217 | print(item) |
| 218 | # alternatively, you can paginate page-by-page |
| 219 | for page in response.iter_pages(): |
| 220 | print(page) |
| 221 | ``` |
| 222 | |
| 223 | ```python |
| 224 | # You can also iterate through pages and access the typed response per page |
| 225 | pager = client.actions.list(...) |
| 226 | for page in pager.iter_pages(): |
| 227 | print(page.response) # access the typed response for each page |
| 228 | for item in page: |
| 229 | print(item) |
| 230 | ``` |
| 231 | |
| 232 | ## Advanced |
| 233 | |
| 234 | ### Access Raw Response Data |
| 235 | |
| 236 | The SDK provides access to raw response data, including headers, through the `.with_raw_response` property. |
| 237 | The `.with_raw_response` property returns a "raw" client that can be used to access the `.headers` and `.data` attributes. |
| 238 | |
| 239 | ```python |
| 240 | from auth0.management import Auth0 |
| 241 | |
| 242 | client = Auth0( |
| 243 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 244 | token="YOUR_TOKEN", |
| 245 | ) |
| 246 | response = client.actions.with_raw_response.create(...) |
| 247 | print(response.headers) # access the response headers |
| 248 | print(response.data) # access the underlying object |
| 249 | pager = client.actions.list(...) |
| 250 | print(pager.response) # access the typed response for the first page |
| 251 | for item in pager: |
| 252 | print(item) # access the underlying object(s) |
| 253 | for page in pager.iter_pages(): |
| 254 | print(page.response) # access the typed response for each page |
| 255 | for item in page: |
| 256 | print(item) # access the underlying object(s) |
| 257 | ``` |
| 258 | |
| 259 | ### Retries |
| 260 | |
| 261 | The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 262 | as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 263 | retry limit (default: 2). |
| 264 | |
| 265 | A request is deemed retryable when any of the following HTTP status codes is returned: |
| 266 | |
| 267 | - [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 268 | - [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 269 | - [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 270 | |
| 271 | Use the `max_retries` request option to configure this behavior. |
| 272 | |
| 273 | ```python |
| 274 | client.actions.create(..., request_options={ |
| 275 | "max_retries": 1 |
| 276 | }) |
| 277 | ``` |
| 278 | |
| 279 | ### Timeouts |
| 280 | |
| 281 | The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 282 | |
| 283 | ```python |
| 284 | from auth0.management import Auth0 |
| 285 | |
| 286 | client = Auth0( |
| 287 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 288 | token="YOUR_TOKEN", |
| 289 | timeout=20.0, |
| 290 | ) |
| 291 | |
| 292 | |
| 293 | # Override timeout for a specific method |
| 294 | client.actions.create(..., request_options={ |
| 295 | "timeout_in_seconds": 1 |
| 296 | }) |
| 297 | ``` |
| 298 | |
| 299 | ### Custom Client |
| 300 | |
| 301 | You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies |
| 302 | and transports. |
| 303 | |
| 304 | ```python |
| 305 | import httpx |
| 306 | from auth0.management import Auth0 |
| 307 | |
| 308 | client = Auth0( |
| 309 | base_url="https://YOUR_TENANT.auth0.com/api/v2", |
| 310 | token="YOUR_TOKEN", |
| 311 | httpx_client=httpx.Client( |
| 312 | proxy="http://my.test.proxy.example.com", |
| 313 | transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 314 | ), |
| 315 | ) |
| 316 | ``` |
| 317 | |
| 318 | ### Custom Domains |
| 319 | |
| 320 | If your Auth0 tenant uses multiple custom domains, you can specify which custom domain to use via the `Auth0-Custom-Domain` header. The SDK enforces a whitelist, the header is only sent on supported endpoints. |
| 321 | |
| 322 | **Global (all whitelisted requests):** |
| 323 | |
| 324 | ```python |
| 325 | from auth0.management import ManagementClient |
| 326 | |
| 327 | client = ManagementClient( |
| 328 | domain="your-tenant.auth0.com", |
| 329 | token="YOUR_TOKEN", |
| 330 | custom_domain="login.mycompany.com", |
| 331 | ) |
| 332 | ``` |
| 333 | |
| 334 | **Per-request override:** |
| 335 | |
| 336 | ```python |
| 337 | from auth0.management import ManagementClient, CustomDomainHeader |
| 338 | |
| 339 | client = ManagementClient( |
| 340 | domain="your-tenant.auth0.com", |
| 341 | token="YOUR_TOKEN", |
| 342 | custom_domain="login.mycompany.com", |
| 343 | ) |
| 344 | |
| 345 | # Override the global custom domain for this specific request |
| 346 | client.users.create( |
| 347 | connection="Username-Password-Authentication", |
| 348 | email="[email protected]", |
| 349 | password="SecurePass123!", |
| 350 | request_options=CustomDomainHeader("other.mycompany.com"), |
| 351 | ) |
| 352 | ``` |
| 353 | |
| 354 | If both a global `custom_domain` and a per-request `CustomDomainHeader` are provided, the per-request value takes precedence. |
| 355 | |
| 356 | ## Feedback |
| 357 | |
| 358 | ### Contributing |
| 359 | |
| 360 | We appreciate feedback and contribution to this repo! Before you get started, please see the following: |
| 361 | |
| 362 | - [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md) |
| 363 | - [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md) |
| 364 | |
| 365 | ### Raise an issue |
| 366 | |
| 367 | To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-python/issues). |
| 368 | |
| 369 | ### Vulnerability Reporting |
| 370 | |
| 371 | Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/responsible-disclosure-policy) details the procedure for disclosing security issues. |
| 372 | |
| 373 | --- |
| 374 | |
| 375 | <p align="center"> |
| 376 | <picture> |
| 377 | <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150"> |
| 378 | <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150"> |
| 379 | <img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150"> |
| 380 | </picture> |
| 381 | </p> |
| 382 | <p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0</a></p> |
| 383 | <p align="center"> |
| 384 | This project is licensed under the MIT license. See the <a href="https://github.com/auth0/auth0-python/blob/master/LICENSE"> LICENSE</a> file for more info |
| 385 | </p> |