---
title: Query a code workspace
description: Run a command or read code in a workspace.
url: https://www.hacktron.ai/docs/api/code-workspaces/query-code-workspace
---

# Query a code workspace

Runs one query in a workspace that [`POST /code-workspaces`](/docs/api/code-workspaces/open-code-workspace) opened. Paths are relative to the repository root. You need the member role or above.

`tool` picks what runs:

| `tool` | What it does |
| --- | --- |
| `run_command` | Runs the bash command line in `command` from the repository root, such as `rg`, `sed`, `git log`, `git diff`, or `git blame`, with no credentials and no network access. It has 120 seconds, and its output is cut at 500 lines. |
| `search` | Searches for `pattern`, an extended regular expression, with `git grep -E`. `context_lines` adds lines around each match. |
| `read` | Returns a file's numbered lines, from `start_line` to `end_line`, up to 400 at a time. |
| `list` | Lists a directory, or with `recursive` every file under it. `name_pattern` keeps the paths that match it. |
| `diff` | Diffs a pull request against its base, in a workspace opened from a pull request. |
| `log` | Lists the commits that touched a path. |
| `blame` | Shows who last changed each line, from `start_line` to `end_line`, up to 400 at a time. |
| `dependencies` | Lists the dependency manifests and lock files, or with `pattern` the lines in them that name that package. |

`truncated` is `true` when the output was cut at its limit. Narrow the command, path, or pattern and run it again.

A closed workspace returns `404 Not Found`, and one that failed or is closing returns `409 Conflict`. In both cases, open the workspace again.

## Request

`POST /code-workspaces/{workspace_id}/query`

Send an [API key](/docs/api/authentication) with the `read` scope in the `X-Api-Key` header.

:::examples

```bash title="Request" wrap=false
curl -X POST "https://api.hacktron.ai/v1/code-workspaces/<workspace_id>/query" \
  -H "X-Api-Key: $HACKTRON_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "tool": "search",
  "pattern": "jwt\\.verify|verifyToken",
  "path": "src/auth"
}'
```

```json title="Response 200 OK" showLineNumbers=false wrap=false
{
  "output": "src/auth/session.ts:18:  const claims = jwt.verify(token, publicKey)\nsrc/auth/middleware.ts:9:export function verifyToken(req: Request) {",
  "truncated": false
}
```

:::

### Path parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `workspace_id` | string, required | workspace_id from opening a code workspace. |

### Body

A required `application/json` body.

| Field | Type | Description |
| --- | --- | --- |
| `tool` | string, required | run_command (a bash command line in the repository root: rg, sed, git log, git diff, git blame and the like, with no credential and no network), or one of the fixed commands: search (git grep -E), read (a file's lines), list (a directory, or files by name), diff (a pull request against its base), log (commits touching a path), blame (who last changed a file's lines), dependencies (the dependency manifests and lock files, or the lines in them naming a package). One of `run_command`, `search`, `read`, `list`, `diff`, `log`, `blame`, or `dependencies`. |
| `command` | string | For run_command: the bash command line, run from the repository root with a 120s limit; output is cut at 500 lines. |
| `path` | string | A path relative to the repository root. |
| `pattern` | string | Extended regular expression for search; for dependencies, the package name as text. |
| `ignore_case` | boolean | Default `false`. |
| `context_lines` | number | Lines of context around each search match. Default `0`. At least 0. At most 5. |
| `name_pattern` | string | For list: only paths under path matching this extended regular expression, case-insensitive. |
| `start_line` | number | Default `1`. At least 1. |
| `end_line` | number | Up to 400 lines are read or blamed. At least 1. |
| `recursive` | boolean | list every file under the path instead of one level. Default `false`. |

## Responses

### 200 OK

| Field | Type | Description |
| --- | --- | --- |
| `output` | string, required | The command's output: for run_command, stdout and stderr together, ending with the exit code when it isn't 0; file:line:text for search, numbered lines for read, paths (directories end in /) for list, commit, author, date and text per line for blame. |
| `truncated` | boolean, required | Whether the output was cut at its limit; narrow the command, path or pattern. |

### 400 Bad Request

The query is invalid, such as a path outside the repository or a diff outside a pull request.

### 404 Not Found

The workspace has closed; open it again.

### 409 Conflict

The workspace failed or is closing; open it again.

### 429 Too Many Requests

The workspace is busy with other queries; try again in a few seconds.
