The Auth0 Python library provides convenient access to the Auth0 APIs from Python.
- Installation
- Reference
- Authentication API
- Management API
- Async Client
- Exception Handling
- Pagination
- Advanced
- Feedback
pip install auth0-pythonRequirements:
- Python ≥3.10 (Python 3.9 support has been dropped)
A full reference for this library is available here.
The Authentication API is used for authentication flows such as obtaining tokens via client credentials, authorization codes, or resource owner password grants:
fromauth0.authenticationimportGetTokentoken_client=GetToken(
domain="your-tenant.auth0.com",
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)
# Get an access token using client credentialstoken_response=token_client.client_credentials(
audience="https://your-tenant.auth0.com/api/v2/"
)
access_token=token_response["access_token"]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:
fromauth0.managementimportManagementClient# With an existing tokenclient=ManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
)
# Or with client credentials (automatic token acquisition and refresh)client=ManagementClient(
domain="your-tenant.auth0.com",
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)For async usage:
importasynciofromauth0.managementimportAsyncManagementClientclient=AsyncManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
)
asyncdefmain() ->None:
users=awaitclient.users.list()
print(users)
asyncio.run(main())You can obtain a token using the Authentication API and use it with the Management API client:
fromauth0.authenticationimportGetTokenfromauth0.managementimportAuth0domain="your-tenant.auth0.com"# Get a token using the Authentication APItoken_client=GetToken(
domain=domain,
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)
token_response=token_client.client_credentials(
audience=f"https://{domain}/api/v2/"
)
access_token=token_response["access_token"]
# Use the token with the Management API clientclient=Auth0(
base_url=f"https://{domain}/api/v2",
token=access_token,
)Alternatively, you can use the Auth0 client directly with a full base URL:
fromauth0.managementimportActionTrigger, Auth0client=Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
client.actions.create(
name="name",
supported_triggers=[
ActionTrigger(
id="id",
)
],
)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).
importasynciofromauth0.managementimportActionTrigger, AsyncAuth0client=AsyncAuth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
asyncdefmain() ->None:
awaitclient.actions.create(
name="name",
supported_triggers=[
ActionTrigger(
id="id",
)
],
)
asyncio.run(main())When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
fromauth0.management.core.api_errorimportApiErrortry:
client.actions.create(...)
exceptApiErrorase:
print(e.status_code)
print(e.body)Paginated requests will return a SyncPager or AsyncPager, which can be used as generators for the underlying object.
fromauth0.managementimportAuth0client=Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
response=client.actions.list(
trigger_id="post-login",
action_name="actionName",
deployed=True,
page=1,
per_page=1,
installed=True,
)
foriteminresponse:
print(item)
# alternatively, you can paginate page-by-pageforpageinresponse.iter_pages():
print(page)# You can also iterate through pages and access the typed response per pagepager=client.actions.list(...)
forpageinpager.iter_pages():
print(page.response) # access the typed response for each pageforiteminpage:
print(item)The SDK provides access to raw response data, including headers, through the .with_raw_response property.
The .with_raw_response property returns a "raw" client that can be used to access the .headers and .data attributes.
fromauth0.managementimportAuth0client=Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
response=client.actions.with_raw_response.create(...)
print(response.headers) # access the response headersprint(response.data) # access the underlying objectpager=client.actions.list(...)
print(pager.response) # access the typed response for the first pageforiteminpager:
print(item) # access the underlying object(s)forpageinpager.iter_pages():
print(page.response) # access the typed response for each pageforiteminpage:
print(item) # access the underlying object(s)The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
Use the max_retries request option to configure this behavior.
client.actions.create(..., request_options={
"max_retries": 1
})The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.
fromauth0.managementimportAuth0client=Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
timeout=20.0,
)
# Override timeout for a specific methodclient.actions.create(..., request_options={
"timeout_in_seconds": 1
})You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies
and transports.
importhttpxfromauth0.managementimportAuth0client=Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
httpx_client=httpx.Client(
proxy="http://my.test.proxy.example.com",
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
),
)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.
Global (all whitelisted requests):
fromauth0.managementimportManagementClientclient=ManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
custom_domain="login.mycompany.com",
)Per-request override:
fromauth0.managementimportManagementClient, CustomDomainHeaderclient=ManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
custom_domain="login.mycompany.com",
)
# Override the global custom domain for this specific requestclient.users.create(
connection="Username-Password-Authentication",
email="user@example.com",
password="SecurePass123!",
request_options=CustomDomainHeader("other.mycompany.com"),
)If both a global custom_domain and a per-request CustomDomainHeader are provided, the per-request value takes precedence.
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
To provide feedback or report a bug, please raise an issue on our issue tracker.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0
This project is licensed under the MIT license. See the LICENSE file for more info

