> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/sonar/reference/api.md).

# API

The SONAR REST API: base address, API key, and the endpoints for devices, commands, alerts, checks, tasks, software, patches, inventory, employees and terms.

## What this is and when to use it

SONAR exposes a REST API over HTTPS. The console uses the same API, so what a role can do in the console, a script can do through the API with a key of a user holding that role.

Use it to pull the device inventory into a CMDB, to raise tickets from alerts, to run a command or a script on devices from an external scheduler, or to trigger patch scans and installs from a change window.

## Base address and authentication

Every path on this page is relative to the API address of your installation, provided with it. This page writes it as `https://api.example.com`.

Authenticate each request with an API key in the `X-API-KEY` header. Create the key in **Admin Center › Settings › API Keys**; see [API keys](https://wiki.datadike.com/sonar/administration/settings/api-keys.md).

```bash
curl -s \
  -H "X-API-KEY: <your-api-key>" \
  -H "Content-Type: application/json" \
  "https://api.example.com/agents/?page=1&page_size=10"
```

The request runs as the key's user: it has the permissions of that user's role, and the role's **Client & Site Access** applies to it. A call the role does not allow answers 403. See [Role permissions](https://wiki.datadike.com/sonar/reference/permissions.md).

`GET /accounts/me/` tells what a key can do. It answers with `user_id`, `role_id`, `username`, `is_admin`, and one `true` or `false` per permission of the role, such as `can_list_agents`.

To plan a whole integration, with a service user, webhooks and the key store, see [Plan an integration](https://wiki.datadike.com/sonar/administration/integrations/plan-an-integration.md).

## Conventions

* **JSON in and out.** Send bodies as JSON with `Content-Type: application/json`.
* **Device identifier.** Device paths take the device's `agent_id`, a long identifier returned by `GET /agents/`. It is not the hostname.
* **Other identifiers.** Clients, sites, checks, tasks, scripts, alerts and updates are addressed by their numeric `id`.
* **Trailing slash.** Every path ends with `/`.
* **Action responses.** Many actions answer with a plain text message, for example `Checks will now be run on WS-0001`, rather than an object.
* **Live actions.** Pinging, running a command, running a script and waiting for its output, rebooting and shutting down contact the device while the request is open. They need the device to be online.
* **Many devices at once.** To act on many devices, use `POST /agents/actions/bulk/` instead of one request per device. It queues the work and returns at once.

### Pagination and filters on the device list

`GET /agents/` is paginated and answers with this envelope:

```json
{ "count": 1250, "total_pages": 125, "next": "...", "previous": null, "results": [] }
```

| Parameter      | What it does                                                                                                    |
| -------------- | --------------------------------------------------------------------------------------------------------------- |
| `page`         | Page number, starting at 1                                                                                      |
| `page_size`    | Devices per page. Default 10, maximum 100                                                                       |
| `search`       | Free text across hostname, description, operating system, client, site, IP addresses and logged-in user         |
| `client`       | One client `id`                                                                                                 |
| `clients`      | Several client ids, comma-separated                                                                             |
| `site`         | One site `id`                                                                                                   |
| `type`         | `server` or `workstation`                                                                                       |
| `plat`         | `windows`, `linux` or `darwin`                                                                                  |
| `os`           | Text contained in the operating system name                                                                     |
| `availability` | `online`, `offline12h`, `offline24h`, `offline7d`, `overdue`, `maintenance` or `archived`                       |
| `ordering`     | Field to sort by, for example `hostname`; prefix with `-` to reverse                                            |
| `detail`       | `false` returns a plain list `[{id, hostname, agent_id, client, site}]` of every matching device, without pages |

Each result carries, among others, `agent_id`, `hostname`, `client_name`, `site_name`, `status`, `plat`, `operating_system`, `last_seen`, `needs_reboot`, `has_patches_pending` and `maintenance_mode`.

## Endpoints

### Devices

| Method | Path                          | What it does                                                                                                                                                                                                                                    | Permission         |
| ------ | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| GET    | `/agents/`                    | List devices, paginated and filtered as above                                                                                                                                                                                                   | List Agents        |
| GET    | `/agents/<agent_id>/`         | Full details of one device, including hardware, disks, checks and custom fields                                                                                                                                                                 | List Agents        |
| PUT    | `/agents/<agent_id>/`         | Change a device. Accepts `description`, `site`, `monitoring_type`, `maintenance_mode`, `time_zone`, `check_interval`, `offline_time`, `overdue_time`, the three `overdue_*_alert` flags and `custom_fields`                                     | Edit Agent         |
| DELETE | `/agents/<agent_id>/`         | Remove the agent from the device and the device from SONAR                                                                                                                                                                                      | Uninstall Agents   |
| GET    | `/agents/<agent_id>/ping/`    | Ask the device whether it answers. Returns `{"name": ..., "status": "online"}` or `"offline"`                                                                                                                                                   | List Agents        |
| GET    | `/agents/clients/summary/`    | Device counts per client: `total_devices`, `online_lt_12h`, `offline_gt_12h`, `offline_gt_24h`, `offline_gt_7d`, `overdue_gt_30d`, `in_maintenance`, `archived`, and the same counters under `totals`. `?clients=1,2` limits it to some clients | List Agents        |
| POST   | `/agents/maintenance/bulk/`   | Turn maintenance mode on or off for a whole client or site: `{"type": "Client", "id": 3, "action": true}`                                                                                                                                       | Edit Agent         |
| GET    | `/agents/<agent_id>/history/` | Commands and scripts run on the device, with their results                                                                                                                                                                                      | List Agent History |
| GET    | `/agents/<agent_id>/notes/`   | Notes of a device                                                                                                                                                                                                                               | List Notes         |
| POST   | `/agents/notes/`              | Add a note: `{"agent_id": "...", "note": "..."}`. Answers `"Note added!"`                                                                                                                                                                       | Manage Notes       |

In the details of a device, `client` is the client's name and `site` the site's `id`, with its name in `site_name`. The serial number is inside `wmi_detail`: at `wmi_detail.bios[0][0].SerialNumber` on Windows, and at `wmi_detail.serialnumber` on Linux and macOS. `is_owned` is `owned` or `leased`. In `PUT /agents/<agent_id>/`, send only the fields you change: setting `archived` to `true` uninstalls the agent from the device.

### Commands, scripts and power

| Method            | Path                            | What it does                                                              | Permission          |
| ----------------- | ------------------------------- | ------------------------------------------------------------------------- | ------------------- |
| POST              | `/agents/<agent_id>/cmd/`       | Run a command and return its output. Body below                           | Send Command        |
| POST              | `/agents/<agent_id>/runscript/` | Run a script from the library. Body below                                 | Run Scripts         |
| POST              | `/agents/actions/bulk/`         | Run a command, a script or a patch action on many devices. Body below     | Run Bulk Operations |
| POST              | `/agents/<agent_id>/reboot/`    | Reboot now                                                                | Reboot Agents       |
| PATCH             | `/agents/<agent_id>/reboot/`    | Schedule a reboot on a Windows device: `{"datetime": "2026-10-01T22:00"}` | Reboot Agents       |
| POST              | `/agents/<agent_id>/shutdown/`  | Shut down                                                                 | Reboot Agents       |
| POST              | `/agents/<agent_id>/wol/`       | Send Wake-on-LAN                                                          | Send Wake-on-LAN    |
| GET               | `/scripts/`                     | List the scripts in the library, with their `id`                          | List Scripts        |
| GET               | `/scripts/<id>/`                | One script with its body                                                  | List Scripts        |
| POST, PUT, DELETE | `/scripts/`, `/scripts/<id>/`   | Create, change or delete a script                                         | Manage Scripts      |

Run a command:

```json
{
  "cmd": "ipconfig /all",
  "shell": "cmd",
  "timeout": 30,
  "run_as_user": false
}
```

`shell` is `cmd` or `powershell` on Windows, and `/bin/bash` on Linux and macOS. For another shell, send `"shell": "custom"` with its path in `custom_shell`. The request waits up to `timeout` seconds for the output.

Run a script:

```json
{
  "script": 42,
  "output": "wait",
  "args": [],
  "env_vars": [],
  "timeout": 90,
  "run_as_user": false
}
```

| `output`    | Effect                                                                                                              |
| ----------- | ------------------------------------------------------------------------------------------------------------------- |
| `wait`      | Waits and returns the script's output                                                                               |
| `forget`    | Returns at once; the result appears in the device's **History**                                                     |
| `email`     | Emails the result. Add `"emailMode": "default"`, or `"emailMode": "custom"` with `"emails": [...]`                  |
| `collector` | Saves the last line of output, or all of it with `"save_all_output": true`, to the custom field in `"custom_field"` |
| `note`      | Saves the output as a note on the device                                                                            |

Run on many devices:

```json
{
  "mode": "script",
  "target": "site",
  "site": 7,
  "monType": "all",
  "osType": "windows",
  "script": 42,
  "args": [],
  "env_vars": [],
  "timeout": 90,
  "run_as_user": false
}
```

| Field     | Values                                                                                                                                           |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `mode`    | `command` (with `cmd`, `shell`, `timeout`, `run_as_user`), `script` (with the script fields), or `patch` (with `patchMode`: `scan` or `install`) |
| `target`  | `agents` with `"agents": ["<agent_id>", ...]`, `client` with `"client": <id>`, `site` with `"site": <id>`, or `all`                              |
| `monType` | `all`, `servers` or `workstations`                                                                                                               |
| `osType`  | `all`, `windows`, `linux` or `darwin`                                                                                                            |

The answer says how many devices received the work, for example `... will now be run on 120 agents. Check the History tab on the agent to view the results.`

### Clients and sites

| Method   | Path                                          | What it does                                                                                         | Permission                |
| -------- | --------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ------------------------- |
| GET      | `/clients/`                                   | Clients with their sites and device counts                                                           | List Clients              |
| POST     | `/clients/`                                   | Create a client with its first site: `{"client": {"name": "CLIENT-A"}, "site": {"name": "SITE-01"}}` | Manage Clients            |
| GET      | `/clients/<id>/`                              | One client                                                                                           | List Clients              |
| PUT      | `/clients/<id>/`                              | Change a client: `{"client": {"name": "..."}}`                                                       | Manage Clients            |
| DELETE   | `/clients/<id>/?move_to_site=<site-id>`       | Delete a client. When it has devices, `move_to_site` says where they go                              | Manage Clients            |
| GET      | `/clients/sites/`                             | All sites                                                                                            | List Sites                |
| POST     | `/clients/sites/`                             | Create a site: `{"site": {"client": 3, "name": "SITE-02"}}`                                          | Manage Sites              |
| GET, PUT | `/clients/sites/<id>/`                        | Read or change a site: `{"site": {"name": "..."}}`                                                   | List Sites / Manage Sites |
| DELETE   | `/clients/sites/<id>/?move_to_site=<site-id>` | Delete a site, moving its devices. A client keeps at least one site                                  | Manage Sites              |

Both create bodies also accept `custom_fields`.

### Alerts

| Method | Path                 | What it does                                                                            | Permission           |
| ------ | -------------------- | --------------------------------------------------------------------------------------- | -------------------- |
| PATCH  | `/alerts/`           | Query alerts. Body below                                                                | List Alerts          |
| GET    | `/alerts/<id>/`      | One alert                                                                               | List Alerts          |
| PUT    | `/alerts/<id>/`      | `{"type": "resolve"}`, `{"type": "snooze", "snooze_days": 2}` or `{"type": "unsnooze"}` | Manage Alerts        |
| DELETE | `/alerts/<id>/`      | Delete an alert                                                                         | Manage Alerts        |
| POST   | `/alerts/bulk/`      | `{"bulk_action": "resolve", "alerts": [1, 2]}`, or `snooze` with `snooze_days`          | Manage Alerts        |
| GET    | `/alerts/templates/` | Alert templates                                                                         | List Alert Templates |

Query body:

```json
{
  "timeFilter": 7,
  "severityFilter": ["error", "warning"],
  "clientFilter": [3],
  "resolvedFilter": false,
  "snoozedFilter": false
}
```

`timeFilter` is a number of days. `resolvedFilter: false` and `snoozedFilter: false` return only open, unsnoozed alerts. Always send `timeFilter` to keep the answer small. The body `{"top": 10}` answers instead with `{"alerts_count": <n>, "alerts": [...]}`: the oldest open alerts.

Each alert carries `id`, `severity` (`info`, `warning`, `error`), `alert_type` (`availability`, `check`, `task`, `custom`), `message`, `alert_time`, `resolved`, `resolved_on`, `snoozed`, `snooze_until`, and, for device alerts, `agent_id`, `hostname`, `client` and `site`. Resolving an alert through the API runs no resolved action of its alert template.

### Checks

| Method           | Path                             | What it does                                                                                             | Permission                  |
| ---------------- | -------------------------------- | -------------------------------------------------------------------------------------------------------- | --------------------------- |
| GET              | `/agents/<agent_id>/checks/`     | Checks of a device, including those inherited from policies, with their last result under `check_result` | List Checks                 |
| POST             | `/checks/`                       | Add a check. Send `"agent": "<agent_id>"` and the check's settings                                       | Manage Checks               |
| GET, PUT, DELETE | `/checks/<id>/`                  | Read, change or delete a check                                                                           | List Checks / Manage Checks |
| POST             | `/checks/<agent_id>/run/`        | Run all checks of the device now                                                                         | Run Checks                  |
| POST             | `/checks/client/<id>/csbulkrun/` | Run checks on every device of a client. Use `site` for a site                                            | Run Checks                  |
| POST             | `/checks/<result-id>/reset/`     | Reset one check to passing. `<result-id>` is the `id` inside `check_result`                              | Manage Checks               |
| POST             | `/checks/<agent_id>/resetall/`   | Reset every check of the device to passing                                                               | Manage Checks               |
| PATCH            | `/checks/<result-id>/history/`   | Result history. `{"timeFilter": 7}` limits it to the last days                                           | List Checks                 |

The settings of a check depend on its type. The quickest way to learn them is to create one check in the console and read it back with `GET /checks/<id>/`.

### Automated tasks

| Method           | Path                        | What it does                                                        | Permission                |
| ---------------- | --------------------------- | ------------------------------------------------------------------- | ------------------------- |
| GET              | `/agents/<agent_id>/tasks/` | Tasks of a device, including those inherited from policies          | List Tasks                |
| POST             | `/tasks/`                   | Create a task. Send `"agent": "<agent_id>"` and the task's settings | Manage Tasks              |
| GET, PUT, DELETE | `/tasks/<id>/`              | Read, change or delete a task                                       | List Tasks / Manage Tasks |
| POST             | `/tasks/<id>/run/`          | Run a task now. For a policy task, add `{"agent_id": "<agent_id>"}` | Run Tasks                 |

As with checks, create one task in the console and read it with `GET /tasks/<id>/` to learn the fields.

### Software

| Method | Path                    | What it does                                                                                                                                 | Permission      |
| ------ | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
| GET    | `/software/<agent_id>/` | Software installed on a device: `{id, agent, software: [{name, version, publisher, install_date, ...}]}`, or `[]` before the first inventory | List Software   |
| GET    | `/software/`            | The same for every device of the role's clients and sites, in one answer without pages. Prefer the call per device                           | List Software   |
| PUT    | `/software/<agent_id>/` | Ask a Windows device for a fresh software list                                                                                               | Manage Software |
| GET    | `/software/chocos/`     | Packages available to install                                                                                                                | List Software   |
| POST   | `/software/<agent_id>/` | Install a package on a Windows device: `{"name": "7zip"}`. Follow it in the device's pending actions                                         | Manage Software |

### Patches (Windows Update)

| Method | Path                             | What it does                                                                                                                                                                                                                                     | Permission             |
| ------ | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------- |
| GET    | `/winupdate/<agent_id>/`         | Updates known on a device. Filters: `state` (`pending`, `downloaded`, `installed`, `failed`), `severity` (`Critical`, `Important`, `Moderate`, `Low`, `other`), `q` (title or KB). Add `page` and `page_size` (up to 100) for a paginated answer | Manage Windows Updates |
| POST   | `/winupdate/<agent_id>/scan/`    | Scan the device for updates                                                                                                                                                                                                                      | Manage Windows Updates |
| POST   | `/winupdate/<agent_id>/install/` | Install the approved updates                                                                                                                                                                                                                     | Manage Windows Updates |
| PUT    | `/winupdate/<id>/`               | Set one update's action: `{"action": "approve"}`, or `ignore`, `inherit`, `nothing`                                                                                                                                                              | Manage Windows Updates |

To scan or install on many devices, use `POST /agents/actions/bulk/` with `"mode": "patch"`.

### Software catalog

The consolidated catalog of the fleet, as **Observability › Softwares** shows it:

| Method | Path                                      | What it does                                                                                                                                      |
| ------ | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| GET    | `/softwaresdash/softwares/`               | One line per name and version: `[{uuid, name, version, publisher, last_install, count}]`. `search` matches the name, the version or the publisher |
| GET    | `/softwaresdash/softwares-device/<uuid>/` | The devices that have that software: `[{hostname, last_install}]`                                                                                 |

The catalog is rebuilt every hour, and each rebuild gives every line a new `uuid`: use it right after reading it, never store it. To identify software in another system, use the name, the version and the publisher. Right after the server restarts, the catalog answers `503` with `{"error": "cache not ready"}` for a few minutes.

### Asset inventory

The data of **Devices › Asset Inventory**, one line per device, drive or network adapter:

| Method | Path                   | What each line carries                                                                                                                                                                                                                                                                                                                                    |
| ------ | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GET    | `/inventory/devices/`  | `agent_id`, `hostname`, `device_type`, `departamento` (the site's name), `operating_system`, `local_ips`, `status`, `logged_in_username`, `last_seen`, `cpu` (`name`, `cores`, `threads`), `ram_gb`, `disk` (the C: drive, or the largest). Filters: `search` (hostname), `plat` (`windows`, `linux`, `macos`), `status` (`online`, `offline`, `overdue`) |
| GET    | `/inventory/hardware/` | `agent_id`, `hostname`, `manufacturer`, `model_name`, `cpu_name`, `cpu_cores`, `cpu_threads`, `total_ram`, `ram_type`, `bios_date`, `primary_disk`                                                                                                                                                                                                        |
| GET    | `/inventory/storage/`  | One line per drive: `agent_id`, `hostname`, `drive`, `disk_model`, `capacity_bytes`, `percent_used`, `free`, `used`, `total`, `partition_count`                                                                                                                                                                                                           |
| GET    | `/inventory/network/`  | One line per adapter: `agent_id`, `hostname`, `adapter_name`, `mac_address`, `ip_address`, `gateway`, `dns_servers`, `speed_mbps` (text, such as `1 Gbps`), `dhcp_enabled`                                                                                                                                                                                |
| GET    | `/inventory/os/`       | `agent_id`, `hostname`, `operating_system`, `os_build`, `os_version`, `architecture`, `domain`, `uptime_seconds`, `patches_last_installed`, `patch_status`. Filter `patch_status`: `critico`, `pendente`, `atualizado`                                                                                                                                    |
| GET    | `/inventory/overview/` | Totals and distributions of the fleet, in one object                                                                                                                                                                                                                                                                                                      |

All but `/inventory/overview/` are paginated as `{summary, count, next, previous, results}`, with `page` and `page_size` (default 10, maximum 100); every list also takes `search`. `summary` counts the whole fleet and ignores the filters. The hardware, drive and adapter details are refreshed every 10 minutes. `device_type` is `desktop`, `laptop`, `workstation`, `server` or `unknown`. The serial number is not in these endpoints: read it in the device details, see [Devices](#devices). Right after the server restarts, `/inventory/overview/` answers `503` with `{"error": "cache not ready"}` for a few minutes.

### Employees

| Method | Path                     | What it does                                                                                                                               |
| ------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
| GET    | `/employees/`            | Every employee of the sites in the role's **Client & Site Access**, in one answer without pages or filters                                 |
| GET    | `/employees/<id>/`       | One employee                                                                                                                               |
| GET    | `/employees/<id>/terms/` | The employee's terms: `{employee_id, total_count, terms: [{term_id, agent_id, hostname, local_ips, file_name, upload_date, uploaded_by}]}` |
| GET    | `/employees/usernames/`  | The Windows usernames seen on devices, with the employee each one belongs to: `[{username, employee_id, employee_name, sites}]`            |
| POST   | `/employees/`            | Add an employee. Roles with **Super User** only                                                                                            |
| PATCH  | `/employees/<id>/`       | Change an employee, for example `{"is_active": false}`. Roles with **Super User** only                                                     |
| DELETE | `/employees/<id>/`       | Delete an employee and their terms. Roles with **Super User** only                                                                         |

Each employee carries `id`, `employer`, `username`, `email`, `phone`, `position`, `department`, `is_active`, `created_at`, `devices_count` (the number of terms) and `sites` (site ids). `employer` is the **Name** shown in **Management › Employees** and `username` the **Username**. A role that is not **Super User** and has no clients or sites in its **Client & Site Access** gets an empty list. Other roles get `403` on writes, with `Only administrators can create employees.` (or `edit`, `delete`).

### Terms

| Method | Path                               | What it does                                                                                                                                                                                                                                |
| ------ | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| POST   | `/terms/term-upload/`              | Upload a signed term, as `multipart/form-data`. Fields below                                                                                                                                                                                |
| GET    | `/agents/<agent_id>/terms/`        | The terms of a device: `{terms: [{name, term_id, upload_date, added_by, employee: {employer, registration}}]}`                                                                                                                              |
| GET    | `/terms/agents-with-terms/`        | Devices with and without terms, paginated as `{count, total_pages, next, previous, results}`. Parameters: `filter` (`with-terms` or `without-terms`), `search`, `page`, `page_size`. Each line has `status`: `attached` or `pending attach` |
| GET    | `/terms/terms/<term_id>/download/` | Download the file                                                                                                                                                                                                                           |
| DELETE | `/terms/delete-term/<term_id>/`    | Delete the term and its file. Answers `204`                                                                                                                                                                                                 |
| GET    | `/terms/terms-stats/`              | Devices with and without terms, and terms uploaded today                                                                                                                                                                                    |

Upload fields, all required:

| Field          | Value                                                                                                                                                                      |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `agent_id`     | The device                                                                                                                                                                 |
| `registration` | The employee's `username`                                                                                                                                                  |
| `employer`     | The employee's `employer`, the **Name** shown in SONAR                                                                                                                     |
| `file`         | PDF or DOCX, up to 10 MB, with its content type declared on the file part: `application/pdf`, or `application/vnd.openxmlformats-officedocument.wordprocessingml.document` |

A successful upload answers `201` with `"Term sent successfully"`, without the term's id: read it with `GET /agents/<agent_id>/terms/`. An employee holds one term per device. When no employee has that `username`, SONAR creates one with the `username` and the `employer` sent. Upload errors are answered with `400`:

| Message                                                                                        | Cause                                                                           |
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| `agent_id is required`, `employer is required`, `registration is required`, `file is required` | A field is missing                                                              |
| `File size must be at most 10MB`                                                               | The file is larger than 10 MB                                                   |
| `Only PDF and DOCX files are allowed`                                                          | The content type of the file part is missing or not one of the two              |
| `The record '...' already belongs to '...', not '...'.`                                        | That `username` belongs to an employee with another **Name**                    |
| `The employee '...' already has a term in this device.`                                        | The employee already holds a term on that device. Delete it first to replace it |

An `agent_id` that does not exist answers `404`.

For the procedures that use these endpoints, see [Jira Service Management](https://wiki.datadike.com/sonar/administration/integrations/jira-service-management.md) and [DocuSign](https://wiki.datadike.com/sonar/administration/integrations/docusign.md).

### Audit trail and pending actions

| Method | Path                                 | What it does                                                                | Permission             |
| ------ | ------------------------------------ | --------------------------------------------------------------------------- | ---------------------- |
| PATCH  | `/logs/audit/`                       | Query the audit trail. Body below                                           | View Audit Logs        |
| GET    | `/agents/<agent_id>/pendingactions/` | Queued actions of a device, such as scheduled reboots and software installs | List Pending Actions   |
| DELETE | `/logs/pendingactions/<id>/`         | Cancel a pending action                                                     | Manage Pending Actions |

Audit query body:

```json
{
  "pagination": { "sortBy": "entry_time", "descending": true, "page": 1, "rowsPerPage": 100 },
  "timeFilter": 1,
  "actionFilter": ["login", "failed_login"],
  "userFilter": ["jdoe"]
}
```

Other filters: `agentFilter` (a list of `agent_id`), `clientFilter` (client ids) and `objectFilter` (for example `user`, `role`, `agent`). The answer is `{"audit_logs": [...], "total": <n>}`. Keep `rowsPerPage` between 25 and 1000.

## Errors

| Status | Body                                                                                                                                                                               | Usual cause                                                                                                       |
| ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| 400    | A text message, for example `"Unable to contact the agent"`, or field errors such as `{"name": ["This field may not be blank."]}`                                                  | The device is offline for a live action, or the body failed validation                                            |
| 401    | `{"detail": "Invalid token."}`, `{"detail": "The token has expired."}`, `{"detail": "User inactive or deleted."}` or `{"detail": "Authentication credentials were not provided."}` | The key is wrong, expired, belongs to a disabled user, or was not sent                                            |
| 403    | `{"detail": "You do not have permission to perform this action."}`                                                                                                                 | The key's user lacks the permission, or the device, client or site is outside the role's **Client & Site Access** |
| 404    | `{"detail": "..."}`, with a message naming what was not found                                                                                                                      | The `agent_id` or `id` does not exist                                                                             |
| 503    | `{"error": "cache not ready"}`                                                                                                                                                     | The inventory overview or the software catalog is being rebuilt after a server restart. Retry in a few minutes    |

## How to verify you can reach the API

1. From the machine that will run the integration, call `GET /agents/?page=1&page_size=1` with your key, as in the example above. A JSON envelope with `count` and one result proves the address, TLS and the key.
2. Call `GET /agents/<agent_id>/ping/` for an online device. `"status": "online"` proves that the API can reach the device.
3. Make one small change, for example a new description on a test device with `PUT /agents/<agent_id>/`, and find it in **Reports & Audits › Audits** under the username of the key's user.

## Common errors

| Symptom                                                | Cause                                                                    | Fix                                                                                                                 |
| ------------------------------------------------------ | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------- |
| 401 on every call                                      | Missing or wrong `X-API-KEY` header                                      | Send the key exactly as copied from **Admin Center › Settings › API Keys**                                          |
| 403 on a call the console allows you                   | The key belongs to another user with a smaller role                      | Check the key's **User** in **Admin Center › Settings › API Keys**                                                  |
| 404 on a device path                                   | The hostname was used instead of the `agent_id`                          | Take `agent_id` from `GET /agents/`                                                                                 |
| A call is answered with a redirect instead of a result | The trailing `/` is missing                                              | End every path with `/`                                                                                             |
| `Unable to contact the agent`                          | The device is offline or not answering                                   | Check `status` in `GET /agents/`, or use a queued action such as `POST /agents/actions/bulk/` or `output: "forget"` |
| `No agents were found meeting the selected criteria`   | The bulk target, `monType` and `osType` select no device the role covers | Widen the filters, or check the role's **Client & Site Access**                                                     |
| `Must select at least 1 agent`                         | `target` is `agents` with an empty `agents` list                         | Send at least one `agent_id`                                                                                        |

## Related pages

* [API keys](https://wiki.datadike.com/sonar/administration/settings/api-keys.md) — create and revoke the key
* [Role permissions](https://wiki.datadike.com/sonar/reference/permissions.md) — what each permission allows
* [Run a command or a script on a device](https://wiki.datadike.com/sonar/administration/automation/run-commands-and-scripts.md) — the same actions in the console
* [Audit trail](https://wiki.datadike.com/sonar/administration/audit/audit-trail.md) — where API changes are recorded
* [Plan an integration](https://wiki.datadike.com/sonar/administration/integrations/plan-an-integration.md) — service user, webhooks and key store
