Authentication methods
The SDK supports two ways to authenticate. Configure only one of them.OAuth2 client credentials
OAuth2 client credentials is a standard flow in which an application authenticates as itself by exchanging a client ID and client secret for a short-lived access token. You create the client ID and client secret in the HiddenLayer Console. See Create API Key. When you provide a client ID and client secret, the SDK handles the token for you:- Before the first API request, the client sends a
POSTrequest to{base_url}/oauth2/tokenwithgrant_type=client_credentials, authenticating with your client ID and client secret using HTTP Basic authentication. - The client caches the returned access token and adds it to each request as an
Authorization: Bearer <token>header. - The client refreshes the token shortly before it expires, based on the
expires_invalue in the token response (3600 seconds if the response does not include it). Token handling is thread-safe. - If the API returns
401 Unauthorizedbecause the cached token expired, the client discards the token, gets a new one, and retries the request.
prod-eu API key does not work against the prod-us host.
If the token request fails, the SDK does not raise an error at that point. It sends the API request without a token, and the API responds with
401, which the SDK raises as AuthenticationError. If you see AuthenticationError, check your client ID, client secret, and region.Bearer token
If you already have an access token, provide it as a bearer token with theHIDDENLAYER_TOKEN environment variable or the bearer_token argument. The SDK sends it as Authorization: Bearer <token> and does not refresh it. When the token expires, requests fail with AuthenticationError until you supply a new one.
If you configure neither, the client is still created, but requests are sent without credentials and fail with AuthenticationError.
Environment variables
The client reads these environment variables when you create it. Arguments that you pass to the constructor take precedence over environment variables.Set environment variables
Set your credentials in the terminal session that runs your code:- macOS/Linux
- Windows
Optional: use a .env file
A.env file is a plain text file of NAME=value lines that you keep in your project folder. The SDK does not read .env files on its own. You can load one with the third-party python-dotenv package.
1
Install python-dotenv
2
Create a .env file
.env
3
Keep the file out of version control
Add
.env to your .gitignore file so that you never commit your secret:.gitignore
4
Load the file before you create the client
load_dotenv() before you create the client, because the client reads environment variables only when it is constructed.Choose a region or base URL
The base URL is the root address that the client sends every request to, for examplehttps://api.us.hiddenlayer.ai. HiddenLayer provides two SaaS environments (regions):
Select the EU region with the
environment argument:
environment="eu", raises ValueError: Unknown environment: eu.
How the client resolves the base URL
The client picks the base URL in this order and uses the first match:- The
base_urlargument, if you pass one. - The
environmentargument, if you pass one. - The
HIDDENLAYER_BASE_URLenvironment variable, if it is set. - The
prod-usenvironment.
environment while HIDDENLAYER_BASE_URL is also set, the client cannot tell which one you intend and raises:
HIDDENLAYER_BASE_URL, remove the environment argument, or pass base_url=None together with environment to confirm that you want the environment:
Pass credentials explicitly
By default, the client reads credentials from environment variables. You can also pass them as constructor arguments, for example when your application loads secrets from a secret manager under different names. Read the values from your secret store or the environment. Never write them as literals in code.Client options
All constructor arguments are keyword-only. The same arguments work forHiddenLayer and AsyncHiddenLayer.
Example with custom timeout and retries:
Use a proxy
If your network requires an HTTP proxy, pass a custom HTTP client.DefaultHttpxClient keeps the SDK’s default settings and adds your changes:
proxy argument is passed to httpx. Support for it depends on your installed httpx version, because older versions used a proxies argument instead.
Override options for a single request
client.with_options(...) returns a copy of the client with some options changed. The original client is not modified. Use it when one call needs different settings, such as a longer timeout for a large request:
Close the client
The client holds open HTTP connections so that it can reuse them. Connections are closed when the client is garbage collected. For predictable cleanup, use the client as a context manager, which closes it when the block ends:client.close() when you are finished. In long-running applications, create one client and reuse it for all requests instead of creating a client per request.
Async client
AsyncHiddenLayer accepts the same arguments and reads the same environment variables as HiddenLayer. Await each method call, and use async with or await client.close() to close it.
aiohttp backend for higher concurrency, install "hiddenlayer-sdk[aiohttp]" and pass DefaultAioHttpClient():
Differences from api.md
The SDK’s generated api.md file has a “Regional Configuration” section that refers to aHIDDEN_LAYER_BASE_URL variable and lists https://api.hiddenlayer.ai as the US host. The SDK source code reads HIDDENLAYER_BASE_URL (no underscore between HIDDEN and LAYER) and uses https://api.us.hiddenlayer.ai for prod-us. Follow the values on this page.

