> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 你的第一个查询

> 向 OASM API 发送 Attack Surface Query、读取响应，并在运行前检查查询

ASQ 端点位于 `/api/attack-surface` 下。它们使用与控制台相同的身份验证：已登录的会话，以及 `X-Workspace-Id` 请求头中的工作区。

> [!NOTE]
>
> 要从外部工具使用工作区 API 密钥查询工作区，请使用 [MCP 服务器](https://docs.oasm.io/zh/settings/mcp.md)：其 `query_attack_surface` 工具接受相同的 JSON。参见 [AI 智能体和 MCP](https://docs.oasm.io/zh/attack-surface-query/ai-agent-and-mcp.md)。

## 端点

| 方法和路径 | 用途 |
| - | - |
| `GET /api/attack-surface/schema` | 你可以查询的资源、字段、运算符和关联 |
| `POST /api/attack-surface/query` | 运行批量查询（或以报告模式将其加入队列） |
| `POST /api/attack-surface/explain` | 校验批量查询并返回其开销，但不运行 |
| `GET /api/attack-surface/jobs/:jobId` | 报告模式批量查询的状态和结果 |
| `/api/attack-surface/saved-queries` | 已保存的查询，参见 [已保存的查询和报告](https://docs.oasm.io/zh/attack-surface-query/saved-queries-and-reports.md) |

## 运行查询

**1. 编写批量查询**

此批量查询列出工作区的目标，并统计每个目标下的主机数量。

```json theme={null}
{
  "queries": {
    "targets": { "from": "targets", "select": ["value", "type", "firstSeen"], "limit": 200 },
    "hostsPerTarget": {
      "from": "hosts",
      "groupBy": ["target.value"],
      "aggregate": { "hosts": { "fn": "count" } },
      "orderBy": [{ "field": "hosts", "dir": "desc" }]
    }
  }
}
```

**2. 发送**

会话 cookie 是你登录时控制台设置的 cookie（名为 `session`，当 OASM 通过 HTTPS 提供服务时为 `__Secure-session`）。

```bash curl theme={null}
curl -X POST "$OASM_URL/api/attack-surface/query" \
  -H "Content-Type: application/json" \
  -H "X-Workspace-Id: $WORKSPACE_ID" \
  -H "Cookie: session=$SESSION" \
  -d @batch.json
```

```javascript fetch (from the console origin) theme={null}
const res = await fetch('/api/attack-surface/query', {
  method: 'POST',
  credentials: 'include',
  headers: { 'Content-Type': 'application/json', 'X-Workspace-Id': workspaceId },
  body: JSON.stringify(batch),
});
const { data, meta } = await res.json();
```

**3. 读取响应**

每个查询的结果在 `data` 中以其名称返回。`meta` 包含批量查询的求值时间以及每个查询的统计信息。

```json theme={null}
{
  "data": {
    "targets": {
      "rows": [
        { "id": "82ec4412-…", "value": "example.com", "type": "DOMAIN", "firstSeen": "2026-11-02T08:14:03.120Z" },
        { "id": "5b591b68-…", "value": "203.0.113.64/28", "type": "CIDR", "firstSeen": "2026-10-28T10:01:44.002Z" }
      ],
      "nextCursor": null,
      "truncated": false
    },
    "hostsPerTarget": {
      "rows": [
        { "target": { "value": "example.com" }, "hosts": 43 },
        { "target": { "value": "203.0.113.64/28" }, "hosts": 16 }
      ],
      "truncated": false
    }
  },
  "meta": {
    "now": "2026-11-18T09:30:00.000Z",
    "tookMs": 18,
    "queries": {
      "targets": { "tookMs": 6, "sqlCount": 1, "cost": 360, "rowCount": 2 },
      "hostsPerTarget": { "tookMs": 9, "sqlCount": 1, "cost": 370, "rowCount": 2 }
    }
  }
}
```

## 读取结果

* **每一行都有 `id`。** 保留它以用于后续查询（`"field": "id", "op": "eq"`）和其他 API 调用。
* **关联行是嵌套的。** `select: ["host.value"]` 返回 `{ "host": { "value": "…" } }`。不存在的关联行（例如没有 HTTP 响应的服务）为 `null`。
* **时间采用 UTC 的 ISO 8601 格式。**
* 当还有更多行时会设置 **`nextCursor`**；带上 `"cursor"` 再次发送相同的查询即可获取下一页。参见 [分页、限制和开销](https://docs.oasm.io/zh/attack-surface-query/paging-and-limits.md)。
* **`truncated: true`** 表示某个长字符串被截断，或达到了响应大小限制。
* **失败的查询** 会在其名称下返回 `{ "error": { "code", "message" } }`，其他查询仍正常返回行。整体无效的批量查询会以 HTTP 400 被拒绝，参见 [错误](https://docs.oasm.io/zh/attack-surface-query/errors.md)。

## 在运行前检查查询

`POST /api/attack-surface/explain` 接受相同的请求体。它会校验批量查询，并为每个查询返回静态开销和数据库查询规划器的估算值；当查询范围过宽时，还会附带提示：

```json theme={null}
{
  "queries": {
    "targets": {
      "cost": 360,
      "budget": 1000,
      "hints": ["add a window (e.g. \"window\": { \"since\": \"now-7d\" })", "lower limit"],
      "resources": ["targets"],
      "planner": { "totalCost": 12.4, "sortRows": 0 }
    }
  }
}
```

开销是在访问数据库之前计算的启发式估值；`budget` 是你的单个查询允许的最大开销。提示只是建议，而不是错误：此查询完全在其预算之内。参见 [分页、限制和开销](https://docs.oasm.io/zh/attack-surface-query/paging-and-limits.md)。

## 了解 schema

`GET /api/attack-surface/schema` 返回你可以读取的资源，包括每个字段的类型、运算符、枚举值以及是否可排序或分组，还有每个资源的关联。[资源和字段](https://docs.oasm.io/zh/attack-surface-query/resources.md) 页面以易读的形式提供了相同的信息。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.