# gt: General Translation CLI tool: gt api
URL: https://generaltranslation.com/zh/docs/cli/reference/commands/api.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 向 General Translation API 发起经过认证的原始请求，或打印其内置的 OpenAPI 规范。gt api 命令的 API 参考。

当你需要直接访问某个端点，又不想为项目引入 HTTP 客户端时，可以使用 `gt api`。该命令会将未经修改的响应体写入标准输出，因此适合在脚本和 shell 流水线中使用。

## 概览 [#overview]

```bash
npx gt api [endpoint] [options]
```

传入端点 path，例如 `/v2/project/info/PROJECT_ID`；使用 `--spec` 时可省略端点。

## 工作原理 [#how-it-works]

* 从 `--api-key` 或 `GT_API_KEY` 读取 API Key。
* 从 `--project-id`、`gt.config.json` 或 `GT_PROJECT_ID` 解析项目 ID。
* 将请求发送到配置的 `baseUrl`，其默认值为 `https://api.gtx.dev`。
* 将原始响应体写入标准输出，不会重试该请求。
* 若响应状态码非 2xx，则将摘要写入标准错误并以状态码 1 退出。

如果 `gt.config.json` 提供的项目 ID 与 `--project-id` 或环境变量中的不一致，命令会在发送请求前中止。若只有命令行参数和环境变量提供了不同的 ID，则以命令行参数为准。

## 选项 [#flags]

| 参数                      | 说明                                                              | 类型        | 可选 | 默认值                   |
| ----------------------- | --------------------------------------------------------------- | --------- | -- | --------------------- |
| `-X, --method <method>` | HTTP 方法：`GET`、`HEAD`、`POST`、`PUT`、`PATCH`、`DELETE` 或 `OPTIONS`。 | `string`  | 是  | `GET`                 |
| `--input <file>`        | 从文件读取请求体；设为 `-` 时从标准输入读取。                                       | `string`  | 是  | —                     |
| `-H, --header <header>` | 添加一个 `Key: Value` 请求头。可重复使用以添加多个请求头。                            | `string`  | 是  | —                     |
| `-i, --include`         | 在响应体之前输出响应状态和响应头。                                               | `boolean` | 是  | `false`               |
| `--spec`                | 打印内置的 OpenAPI 3.1 规范。                                           | `boolean` | 是  | `false`               |
| `-c, --config <path>`   | `gt.config.json` 的路径。可省略 `.json` 后缀。                            | `string`  | 是  | 自动解析                  |
| `--api-key <key>`       | 覆盖 API key。                                                     | `string`  | 是  | `GT_API_KEY`          |
| `--project-id <id>`     | 覆盖项目 ID。                                                        | `string`  | 是  | 配置文件或 `GT_PROJECT_ID` |

`--spec` 不需要端点、API key 或项目 ID。

## 示例 [#examples]

```bash
# 保存随已安装的 CLI 依赖项一起打包的 API 快照
npx gt api --spec > openapi.json

# 使用环境中的凭据读取项目信息
npx gt api /v2/project/info/PROJECT_ID

# 发送 JSON 请求体，并包含响应元数据
npx gt api /v2/project/info/PROJECT_ID \
  --method POST \
  --header "Content-Type: application/json" \
  --input update.json \
  --include

# 从标准输入读取请求体
printf '{"defaultLocale":"fr"}' |
  npx gt api /v2/project/info/PROJECT_ID \
    --method POST \
    --header "Content-Type: application/json" \
    --input -
```

## 其他说明 [#notes]

* `gt api` 自 `gt` 2.20.0 起可用。
* `--spec` 会打印已安装的规范快照，而不会获取当前托管的规范。
* 该命令会跳过 CLI 对文件和区域设置的校验，因此即使翻译设置无效，也不会阻止原始 API 请求的发送。
* 可通过 [OpenAPI 参考](/docs/platform/openapi/overview) 查找端点路径、权限、请求字段和响应 schema。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
