Skip to main content

API 配置

🌐 API configuration

Page summary:

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

API 调用的一般设置可以在 ./config/api.js(或 ./config/api.ts)文件中设置。restdocuments 选项都在这个单一的配置文件中。

🌐 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一组全局定义的属性,应被视为私有。字符串数组[]
restREST API 配置对象-
rest.prefixAPI 前缀字符串/api
rest.defaultLimit在 API 调用中使用的默认 limit 参数(参见 REST API 文档整数25
rest.maxLimit可以作为 limit 请求的最大允许数量(参见 REST API 文档)。整数100
rest.strictParamstrue 时,Content API 路径上只接受允许的查询和主体参数;未知的顶层键将被拒绝。在 register 中通过 自定义 Content API 参数 添加允许的参数。布尔值-
documents文档服务配置对象-
documents.strictParamstrue 时,文档服务方法会拒绝具有未识别根级键的参数(例如,无效的 statuslocale)。当 false 或未设置时,未知参数将被忽略。请参阅 文档服务 API布尔值-
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() 的参数。你可以在同一个配置文件中启用其中之一或两者。

示例:

./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
},
});