# API 调用配置

> Source: https://strapi.nodejs.cn/cms/configurations/api

🌐 API configuration

`/config/api` 集中管理响应隐私、REST 默认设置（前缀、分页限制、最大请求大小）以及对 REST 内容 API 和文档服务的严格参数验证。

API 调用的一般设置可以在 `./config/api.js`（或 `./config/api.ts`）文件中设置。`rest` 和 `documents` 选项都在这个单一的配置文件中。

🌐 General settings for API calls can be set in the `./config/api.js` (or `./config/api.ts`) file. Both `rest` and `documents` options live in this single config file.

| 属性 | 描述 | 类型 | 默认 |
| --- | --- | --- | --- |
| `responses` | 全局 API 响应配置 | 对象 | - |
| `responses.privateAttributes` | 一组全局定义的属性，应被视为私有。 | 字符串数组 | `[]` |
| `rest` | REST API 配置 | 对象 | - |
| `rest.prefix` | API 前缀 | 字符串 | `/api` |
| `rest.defaultLimit` | 在 API 调用中使用的默认 `limit` 参数（参见 [REST API 文档](/cms/api/rest/sort-pagination#pagination-by-offset)） | 整数 | `25` |
| `rest.maxLimit` | 可以作为 `limit` 请求的最大允许数量（参见 [REST API 文档](/cms/api/rest/sort-pagination#pagination-by-offset)）。 | 整数 | `100` |
| `rest.strictParams` | 当 `true` 时，Content API 路径上只接受允许的查询和主体参数；未知的顶层键将被拒绝。在 `register` 中通过 [自定义 Content API 参数](/cms/backend-customization/routes#custom-content-api-parameters) 添加允许的参数。 | 布尔值 | - |
| `documents` | 文档服务配置 | 对象 | - |
| `documents.strictParams` | 当 `true` 时，文档服务方法会拒绝具有未识别根级键的参数（例如，无效的 `status`、`locale`）。当 `false` 或未设置时，未知参数将被忽略。请参阅 [文档服务 API](/cms/api/document-service#configuration)。 | 布尔值 | - |

:::note 

如果 `rest.maxLimit` 的值小于 `rest.defaultLimit` 的值，`maxLimit` 将是使用的限制。

🌐 If the `rest.maxLimit` value is less than the `rest.defaultLimit` value, `maxLimit` will be the limit used.

:::

:::tip

`rest.strictParams` 适用于传入的 REST 内容 API 请求（查询和正文）。`documents.strictParams` 适用于传递给服务器端代码中 `strapi.documents()` 的参数。你可以在同一个配置文件中启用其中之一或两者。

使用 `create-strapi-app` 搭建的应用在生成的 `config/api.*` 文件中，默认情况下 `rest.strictParams` 和 `documents.strictParams` 都设置为 `true`。

🌐 Apps scaffolded with `create-strapi-app` have both `rest.strictParams` and `documents.strictParams` set to `true` by default in the generated `config/api.*` file.

:::

**示例：**

```js title="./config/api.js"

module.exports = ({ env }) => ({
  responses: {
    privateAttributes: ['_v', 'id', 'created_at'],
  },
  rest: {
    prefix: '/v1',
    defaultLimit: 100,
    maxLimit: 250,
    strictParams: true, // only allow parameters defined on routes or added via contentAPI.addQueryParams/addInputParams
  },
  documents: {
    strictParams: true, // reject unrecognized root-level parameters in strapi.documents() calls
  },
});
```

```ts title="./config/api.ts"

  responses: {
    privateAttributes: ['_v', 'id', 'created_at'],
  },
  rest: {
    prefix: '/v1',
    defaultLimit: 100,
    maxLimit: 250,
    strictParams: true, // only allow parameters defined on routes or added via contentAPI.addQueryParams/addInputParams
  },
  documents: {
    strictParams: true, // reject unrecognized root-level parameters in strapi.documents() calls
  },
});
```
