Skip to main content
This page collects short, runnable recipes that use the SDKs’ API resources. A resource method sends exactly one HTTP request to one REST endpoint. Use these recipes when the high-level helpers (the model scanner, the community scanner, and the Python red team session helper) do not cover what you need, for example listing past scans or exporting a report. If you are new to the SDKs, read Core concepts first.

Before you run the examples

Every example creates a client with no credential options. The client reads your credentials from environment variables, so no secrets appear in code. Set them before running a program:
See Authentication and configuration for other options, such as using a token. The TypeScript examples are ES modules that use top-level await. The short Java examples assume a HiddenLayerClient named client, created with new HiddenLayerClientBuilder().build(); the longer ones are complete classes. Replace placeholder values such as YOUR_SCAN_ID or YOUR_MODEL_ID with real IDs from your account. You can find scan IDs with the first recipe below, or in the scan ID field of the report the model scanner returns.

List recent scans

The list-scans method (GET /scan/v3/results) returns the scans that match your filters. Use it to build dashboards, audit recent activity, or find a scan ID. The method is paginated. limit sets how many scans each request returns, and iterating the result walks every matching scan across pages. The response envelope, with the total number of matching scans, is available from the page.
Other useful filters. Names are shown in Python and TypeScript form; the Java builder methods are the same names in camelCase, such as latestPerModelVersionOnly.
The API describes status only as a list of “Statuses”, and the format of the sort parameter is not documented, so neither is shown here.

Get a full scan report

The scan report method (GET /scan/v3/results/{scan_id}) returns the report for one scan, including per-file results. Pass has_detections to include only files with detections (and their parent files), which keeps large reports small.
Read counts from the report’s summary object (for example summary.detection_count). The report’s top-level detection_count, file_count, files_with_detections_count, severity, and detection_categories fields are deprecated.

Get a scan summary

The scan summary method (GET /scan/v3/results/{scan_id}/summary) returns the scan’s status, inventory, and summary counts, without per-file results. It is a lighter request than a full report, so use it to check progress or show a headline result.

Iterate over file results

A scan of a large model folder can contain many files. The file results method (GET /scan/v3/results/{scan_id}/files) returns them one page at a time using cursor pagination: each page includes a pointer (cursor) to the next page.

Automatic pagination

Iterate over the returned page and the SDK requests the next page whenever it needs more items.

Manual page control

Use manual control when you want to process one page at a time, for example to stop early or to show progress per page. Check for another page before you request it.
With Python’s AsyncHiddenLayer, await the method before you iterate (async for file_result in await client.scans.results.list_files(...)) and await page.next_page() for manual control.

Export a SARIF report

SARIF (Static Analysis Results Interchange Format) is a standard JSON format for security findings. Tools such as GitHub code scanning can import SARIF files. The SARIF method (GET /scan/v3/results/{scan_id}/sarif) returns the SARIF log as parsed JSON: a dictionary in Python, an object in TypeScript, and a Map<String, Object> in Java. Serialize it to write a .sarif file.

Retrieve a model

The model method (GET /api/v2/models/{model_id}) returns the model’s name, source, ID, tenant ID, and versions. A scan’s inventory.model_id gives you the model ID.

Delete a model

The delete method (DELETE /api/v2/models/{model_id}) deletes a model and returns nothing. Your credentials need permission for this operation.
Deleting a model is irreversible. Confirm the model ID before you delete it, and avoid running deletes from unattended scripts without a confirmation step. The examples below retrieve the model first and ask for confirmation.
What else is removed along with the model (for example, its versions and scan results) is not documented in the SDKs and needs confirmation.

Analyze a prompt with Prompt Analyzer

The Prompt Analyzer method (POST /api/v1/submit/prompt-analyzer) sends a prompt, and optionally a model output, to Prompt Analyzer. The response includes a verdict and per-category results. The requester ID identifies who sent the prompt and is sent as the X-Requester-Id header.
verdict is true when Prompt Analyzer flags the prompt. For the prompt-injection attempt in this example, the examples print:
TypeScript and Java print true. A harmless prompt, such as “What is the capital of France?”, returns a false verdict and no flagged categories. See the Prompt Analyzer documentation.

Read response headers with raw responses

Request the raw response to read the HTTP status and headers along with the parsed body. The most common use is the hl-runtime-action header that the Runtime Security request-evaluations endpoint (POST /detection/v2/request-evaluations) returns: the HTTP status is 200 whether or not the policy blocks the request. When the policy blocks it, the response has the header hl-runtime-action: BLOCK; otherwise the header is absent. The following example screens an OpenAI Chat Completions request and prints the header. For this harmless prompt the header is absent, so the Python example prints hl-runtime-action: None, the TypeScript example prints hl-runtime-action: null, and the Java example prints nothing. See Runtime security for the complete inline guard.
Java header names keep the case the server sent them in, so compare them without regard to case.

Set per-request options

Every resource method takes optional per-request options as its last argument. They override the client’s settings for that one call.

Timeouts and retries

Give one slow operation a longer timeout, or change its retries, without changing the shared client:

Extra headers, query parameters, and body fields

Use the extra options to send values that the SDK does not model yet, for example a field recently added to the REST API. Values passed this way take precedence over values set on the client or by the method.
The header and query parameter names above are placeholders. Check the HiddenLayer Developer Portal for real REST fields.

Call an endpoint the SDK does not wrap (advanced)

The SDKs cover every public operation in the HiddenLayer API, so you rarely need this. Prefer the typed resource methods whenever they exist: responses from custom requests are not validated or typed.
The Python SDK has no method for sending a request to an arbitrary path. Use the resource methods, and add fields the SDK does not model yet with request_options (see Set per-request options).

Run requests concurrently

When you need to evaluate many interactions at once, send the requests in parallel instead of one after another. The following example evaluates three prompts with the Runtime Security interaction-evaluations endpoint and reports each result, so one failure does not hide the others.
Large batches can trigger rate limiting (HTTP 429). The client retries 429 responses automatically, but you can also limit concurrency: with an asyncio.Semaphore in Python, by sending the requests in batches in TypeScript, or with maxConcurrentRequests(...) on the Java client builder.

Use the aiohttp backend (Python)

By default, AsyncHiddenLayer sends requests with httpx. Install the aiohttp extra and it uses aiohttp instead, with no code changes:

Deprecated resources

  • Model cards (models.cards.list, GET /models/v4/cards) are deprecated in favor of the asset inventory API (GET /asset-inventory/v2/assets with asset_type[in]=MODEL), which returns the same model-card data. The SDKs expose it as asset_inventory.assets (Python), assetInventory.assets (TypeScript), and assetInventory().assets() (Java). The asset inventory endpoints are Beta.
  • Sensors (sensors) are deprecated. The API states that each sensors endpoint “will be removed after April 13, 2026”. Do not use them in new code.