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

# 创建 Environment

> 创建一个新的云端执行环境，用于托管 Agent 会话。

`POST /api/v1/cloud/environments`

创建一个新的云端执行环境，用于托管 Agent 会话。

## 请求头

| 头部              | 必选 | 说明                 |
| --------------- | -- | ------------------ |
| `Authorization` | 是  | `Bearer <PAT>`     |
| `Content-Type`  | 是  | `application/json` |

## 请求体

| 字段            | 类型                                                                                 | 必选 | 说明               |
| ------------- | ---------------------------------------------------------------------------------- | -- | ---------------- |
| `name`        | string                                                                             | 是  | 非空环境名称           |
| `description` | string                                                                             | 否  | 环境描述             |
| `config`      | [Environment config](/zh/cloud-agents/api/environments/schemas#environment-config) | 是  | Environment 配置   |
| `metadata`    | [Metadata 对象](/zh/cloud-agents/api/conventions/schemas#metadata-对象)                | 否  | 自定义元数据，省略时为 `{}` |

## 示例请求

```bash theme={null}
curl -s -X POST "https://api.qoder.com/api/v1/cloud/environments" \
  -H "Authorization: Bearer $QODER_PAT" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "doc-test-env",
    "config": {
      "type": "cloud",
      "networking": {
        "type": "limited"
      },
      "packages": {
        "apt": ["curl"]
      }
    }
  }'
```

## 示例请求：带启动脚本

```bash theme={null}
curl -s -X POST "https://api.qoder.com/api/v1/cloud/environments" \
  -H "Authorization: Bearer $QODER_PAT" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "doc-test-env-with-setup",
    "config": {
      "type": "cloud",
      "networking": {"type": "unrestricted"},
      "packages": {
        "npm": ["pnpm@9"]
      },
      "setup_script": "set -euo pipefail\n[ -d /workspace/.git ] || git clone https://github.com/me/repo /workspace\ncd /workspace && pnpm install --frozen-lockfile"
    }
  }'
```

## 示例响应

**HTTP 200 OK**

```json theme={null}
{
  "id": "env_019e3bb39b6774d8878cd0b9d237574b",
  "type": "environment",
  "name": "doc-test-env",
  "description": "",
  "config": {
    "type": "cloud",
    "networking": {
      "type": "limited",
      "allowed_hosts": [],
      "allow_package_managers": false,
      "allow_mcp_servers": false
    },
    "packages": {
      "type": "packages",
      "apt": ["curl"],
      "npm": [],
      "pip": []
    }
  },
  "metadata": {},
  "archived_at": null,
  "created_at": "2026-05-18T15:28:07.017808Z",
  "updated_at": "2026-05-18T15:28:07.017808Z"
}
```

## 响应字段

| 字段            | 类型                                                                                 | 说明                          |
| ------------- | ---------------------------------------------------------------------------------- | --------------------------- |
| `id`          | string                                                                             | 环境唯一标识，前缀为 `env_`           |
| `type`        | string                                                                             | 资源类型，固定值 `"environment"`    |
| `name`        | string                                                                             | 环境名称                        |
| `description` | string                                                                             | 环境描述                        |
| `config`      | [Environment config](/zh/cloud-agents/api/environments/schemas#environment-config) | Environment 配置              |
| `metadata`    | [Metadata 对象](/zh/cloud-agents/api/conventions/schemas#metadata-对象)                | 自定义元数据                      |
| `archived_at` | string\|null                                                                       | 归档时间（ISO 8601），未归档时为 `null` |
| `created_at`  | string                                                                             | 创建时间（ISO 8601 格式）           |
| `updated_at`  | string                                                                             | 最后更新时间（ISO 8601 格式）         |

## 错误码

| HTTP | type                    | 说明                                     |
| ---- | ----------------------- | -------------------------------------- |
| 400  | `invalid_request_error` | 缺少必填字段 `name`                          |
| 400  | `invalid_request_error` | 缺少必填字段 `config`                        |
| 400  | `invalid_request_error` | `config` 或 `metadata` 字段值不合法           |
| 400  | `invalid_request_error` | `config.setup_script` 不是字符串或长度超过 64 KB |
| 401  | `authentication_error`  | 认证失败，PAT 无效或过期                         |
| 403  | `permission_error`      | 无权执行此操作                                |

## 相关

<CardGroup cols={2}>
  <Card title="云端环境" icon="server" href="/zh/cloud-agents/environments">
    选择 Agent 运行的容器、网络与依赖。
  </Card>
</CardGroup>
