@bahmutov/cy-api

中文文档
2.3.0发布于 2026/04/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 5万

Custom command cy.api

原始使用文档
文档目录9 节
  1. Install
  2. Configuration
  3. TypeScript
  4. Examples
  5. Courses
  6. More info
  7. Development
  8. Small print
  9. MIT License

@bahmutov/cy-api

renovate-app badge cypress version

Cypress custom command "cy.api" for end-to-end API testing

This command makes HTTP requests to external servers, then renders the input and output where the web application usually is in the Cypress Test Runner. If there are server-side logs using @bahmutov/all-logs, this command fetches them and renders too. Here is typical output:

cy.api in action

Install

npm install --save-dev @bahmutov/cy-api

or

yarn add -D @bahmutov/cy-api

Add the following line to your Cypress support file

// usually cypress/support/index.js
import '@bahmutov/cy-api'

This will add a new command cy.api for making API requests.

Configuration

var exposedefault valuedescription
CYPRESS_API_MESSAGEStrueShow and make call to api server logs
CYPRESS_API_SHOW_CREDENTIALSfalseShow authentication password

By default cy.api print response in the browser. To have the same behaviour as cy.request and use cy.visit normally, you need to desactivate apiDisplayRequest :

it('my test without displaying request', { apiDisplayRequest: false }, () => {
  cy.api({
    url: '/',
  })
})

TypeScript

If your using TypeScript with Cypress, you can add type in your tsconfig.json

{
  "compilerOptions": {
    "types": ["cypress", "@bahmutov/cy-api"]
  }
}

Examples

Courses

More info

Development

  • install all dependencies using npm install
  • start watching and compiling the TS source using npm run build:watch
  • from another terminal launch the local server and open Cypress using npm run dev
  • open the desired Cypress spec file

Small print

Author: Gleb Bahmutov <[email protected]> © 2019

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2019 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

@bahmutov/cy-api 共发布 33 个版本,首次发布于 2019/07/09;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.3.0latest2026/04/096 个月前
2.2.102026/01/229 个月前
2.2.92026/01/099 个月前
2.2.82024/12/211.9 年前
2.2.72024/12/191.9 年前
2.2.62023/09/093 年前

关于 @bahmutov/cy-api 的常见问题

@bahmutov/cy-api 是什么?

Custom command cy.api

如何安装 @bahmutov/cy-api?

在项目目录运行 npm install @bahmutov/cy-api,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cypress(>=15.10.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bahmutov/cy-api 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bahmutov/cy-api 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bahmutov/cy-api 可以商用吗?

@bahmutov/cy-api 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bahmutov/cy-api 的下载量有多少?

根据 npm 官方下载统计,@bahmutov/cy-api 在 2026/10/08 – 2026/11/06 这 30 天内被下载 50,228 次,日均约 1,674 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bahmutov/cy-api 还在维护吗?

最新版本 2.3.0 发布于 2026/04/09(6 个月前);至今共发布 33 个版本,其中过去 12 个月发布了 3 个;首次发布于 2019/07/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。