client.interactions.analyze because it is a generally available (non-beta) endpoint and needs no model files or other setup. It is a quick way to confirm that your installation, credentials, and network access work.
Before you begin
You need:- Python 3.9 or later (3.11 or later recommended).
- A HiddenLayer client ID and client secret with Interactions (Read, Write) permissions. See Create API Key.
- Your region:
prod-us(default) orprod-eu.
Make your first request
1
Create a project folder and virtual environment
A virtual environment keeps the SDK and its dependencies isolated from other Python projects.
- macOS/Linux
- Windows
2
Install the SDK
3
Set your credentials
The client reads your client ID and client secret from environment variables, so you never have to put them in code.Replace the placeholders with your values. If your API key is in the EU region, change
- macOS/Linux
- Windows
environment="prod-us" to environment="prod-eu" in the script in the next step.These variables last only for the current terminal session. If you open a new terminal, set them again, or use a
.env file as described in Authentication and configuration.4
Create quickstart.py
Create a file named The
quickstart.py in your project folder with this content:quickstart.py
try block catches the most common errors and prints a hint for each. APIStatusError is listed last because it is the parent class of AuthenticationError and PermissionDeniedError, so it catches any other HTTP error status.5
Run the script
Expected result
The script prints the overall evaluation, followed by one line per analysis. The output looks similar to this example. Analysis names, the number of analyses, and the results depend on your project’s policy and on the interaction you send, so your values will differ.
The response also includes
metadata (such as event_id and processing time), analyzed_data (the content that was analyzed), and modified_data (the input and output after any redaction). To see everything, print response.to_json().
For a description of the Interactions response, see Getting started with Interactions.
What just happened
- The client read its configuration.
HiddenLayer()readHIDDENLAYER_CLIENT_IDandHIDDENLAYER_CLIENT_SECRETfrom the environment and set the base URL tohttps://api.us.hiddenlayer.aiforprod-us. - The client obtained an access token. Before sending the request, the SDK exchanged your client ID and client secret for an OAuth2 access token at
https://api.us.hiddenlayer.ai/oauth2/token. It caches this token and refreshes it automatically, so you do not manage tokens yourself. - The client sent the request. It sent the metadata, input, and output to the Interactions endpoint with the token in the
Authorizationheader. If the request had failed with a temporary error, such as a timeout or HTTP 429 or 5xx, the SDK would have retried up to two times. - The SDK parsed the response. The JSON response was converted into an
InteractionAnalyzeResponsePydantic model, so you can read fields as attributes, such asresponse.evaluation.action.
Optional: async version
Optional: async version
If your application uses
asyncio, use AsyncHiddenLayer. It takes the same arguments and reads the same environment variables. Await each call.quickstart_async.py
If it fails
Authentication failed (401)
Authentication failed (401)
The client could not get a valid token. Check that both environment variables are set in the same terminal where you run the script, that the values have no extra spaces or quotes, and that
environment matches the region where the API key was created. See Troubleshooting.Permission denied (403)
Permission denied (403)
Your API key is valid but lacks permission for the Interactions endpoint. Create a key with Interactions (Read, Write) permissions. See Create API Key.
ValueError: Ambiguous URL
ValueError: Ambiguous URL
HIDDENLAYER_BASE_URL is set in your environment and the script also passes environment. Unset the variable, or see How the client resolves the base URL.
