# 查询工作空间列表

查询当前身份可访问的工作空间。可按名称筛选结果。

```text
GET https://moi.matrixorigin.cn/newmoi/workspaces
```

## 调用前准备

准备个人访问令牌。

下方示例使用：

- `$AI_STUDIO_API_KEY`：实际个人访问令牌，通过 `X-API-Key` Header 传递。
- `$WORKSPACE_NAME`：可选的工作空间名称筛选值，通过查询参数 `name` 传递。

## 查询参数

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `name` | string | 否 | 按工作空间名称筛选结果。 |

## 请求示例

```bash
curl --get "https://moi.matrixorigin.cn/newmoi/workspaces" \
  --data-urlencode "name=$WORKSPACE_NAME" \
  -H "X-API-Key: $AI_STUDIO_API_KEY"
```

## 成功响应

成功时返回 `200`，`data.workspaces` 为当前身份可访问的工作空间列表；没有匹配项时返回空数组。

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "workspaces": [
      {
        "id": "ws_01",
        "name": "分析工作区",
        "status": "active",
        "owner_id": "user_01",
        "created_at": "2026-08-01T09:00:00Z",
        "access_status": "active",
        "invitation_id": ""
      }
    ],
    "total": 1
  }
}
```

响应字段如下。

本文中，字段路径中的 `[]` 表示数组中的每一项。例如，`data.workspaces[].id` 表示 `data.workspaces` 数组中每一项的 `id` 字段。

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | string | 成功时为 `OK`。 |
| `msg` | string | 成功时为 `OK`。 |
| `data.workspaces` | array | 工作空间列表。没有匹配项时为空数组。 |
| `data.workspaces[].id` | string | 工作空间 ID。 |
| `data.workspaces[].name` | string | 工作空间名称。 |
| `data.workspaces[].status` | string | 工作空间当前状态。 |
| `data.workspaces[].owner_id` | string | 工作空间所有者的用户 ID。 |
| `data.workspaces[].created_at` | string | 工作空间创建时间。 |
| `data.workspaces[].access_status` | string | 当前身份对该工作空间的访问状态。 |
| `data.workspaces[].invitation_id` | string | 与当前身份相关的邀请 ID；没有邀请时为空字符串。 |
| `data.total` | integer | 返回的工作空间数量。 |

## 错误响应

```json
{
  "code": "ErrWorkspaceListFailed",
  "msg": "查询工作空间失败",
  "data": null
}
```

### 常见 HTTP 错误

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP 状态码
  - 错误代码
  - 常见原因
  - 建议操作
* - `401`
  - `Unauthorized`
  - 缺少或无效的访问凭据。
  - 检查个人访问令牌。
* - `403`
  - `ErrUserNotSynced` 或 `PERMISSION_DENIED`
  - 当前身份尚未完成同步，或没有访问工作空间的权限。
  - 完成账户同步，或联系管理员授权后重试。
* - `400`
  - `INVALID_ARGUMENT`
  - 查询参数无效。
  - 检查 `name` 参数后重试。
* - `503`
  - `UNAVAILABLE`
  - 依赖服务暂时不可用。
  - 稍后重试。
* - `500`
  - `ErrWorkspaceListFailed`
  - 服务端未能查询工作空间。
  - 记录请求时间和错误信息后重试。
```
