@apidevtools/swagger-parser

中文文档
13.1.0发布于 2026/09/21MITTypeScriptCommonJS构建来源已验证近 30 天下载 2414.2万

Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers

原始使用文档
文档目录11 节
  1. Features
  2. Related Projects
  3. Example
  4. Installation
  5. Usage
  6. Browser support
  7. API Documentation
  8. Security
  9. Contributing
  10. License
  11. Big Thanks To

Swagger 2.0 and OpenAPI 3.0 parser/validator

Build Status Coverage Status Tested on APIs.guru

npm Dependencies License Buy us a tree

OS and Browser Compatibility

Features

  • Parses Swagger specs in JSON or YAML format
  • Validates against the Swagger 2.0 schema or OpenAPI 3.0 Schema
  • Resolves all $ref pointers, including external files and URLs
  • Can bundle all your Swagger files into a single file that only has internal $ref pointers
  • Can dereference all $ref pointers, giving you a normal JavaScript object that's easy to work with
  • Tested in Node.js and all modern web browsers on Mac, Windows, and Linux
  • Tested on over 1,500 real-world APIs from Google, Microsoft, Facebook, Spotify, etc.
  • Supports circular references, nested references, back-references, and cross-references
  • Maintains object reference equality — $ref pointers to the same value always resolve to the same object instance

Example

SwaggerParser.validate(myAPI, (err, api) => {
  if (err) {
    console.error(err);
  } else {
    console.log("API name: %s, Version: %s", api.info.title, api.info.version);
  }
});

Or use async/await or Promise syntax instead. The following example is the same as above:

try {
  let api = await SwaggerParser.validate(myAPI);
  console.log("API name: %s, Version: %s", api.info.title, api.info.version);
} catch (err) {
  console.error(err);
}

For more detailed examples, please see the API Documentation

Installation

Install using npm:

npm install @apidevtools/swagger-parser

Node.js 22.19 or later is required.

Usage

When using Swagger Parser in Node.js apps, you'll probably want to use CommonJS syntax:

const SwaggerParser = require("@apidevtools/swagger-parser");

When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:

import * as SwaggerParser from "@apidevtools/swagger-parser";

Browser support

Swagger Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.

To use Swagger Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

API Documentation

Full API documentation is available right here

Security

The library, by default, attempts to resolve any files referenced using $ref, without considering file extensions or the location of the files. This can result in Local File Inclusion (LFI), thus, potentially sensitive information disclosure. Developers must be cautious when working with documents from untrusted sources. See here for more details and information on how to mitigate LFI.

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Use the current Node.js LTS release for development. TypeScript tests use the TypeScript 7 compiler, while ESLint uses the TypeScript 6 compatibility API via Microsoft's recommended package aliases.

To test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/swagger-parser.git

  2. Install dependencies
    pnpm install

  3. Run the tests
    pnpm test

  4. Check the code coverage
    pnpm run coverage

License

Swagger Parser is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

GitHub NPM Coveralls

版本与发布记录

@apidevtools/swagger-parser 共发布 15 个版本,首次发布于 2020/03/14;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
13.1.0latest2026/09/211 个月前
13.0.02026/09/032 个月前
12.1.02025/10/141.1 年前
12.0.02025/06/171.4 年前
11.0.12025/06/071.4 年前
11.0.02025/06/041.4 年前

关于 @apidevtools/swagger-parser 的常见问题

@apidevtools/swagger-parser 是什么?

Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers

如何安装 @apidevtools/swagger-parser?

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

@apidevtools/swagger-parser 支持 TypeScript 吗?

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

@apidevtools/swagger-parser 支持 ESM 还是 CommonJS?

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

@apidevtools/swagger-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apidevtools/swagger-parser 可以商用吗?

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

@apidevtools/swagger-parser 的下载量有多少?

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

@apidevtools/swagger-parser 还在维护吗?

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