swagger-client

A JavaScript library that turns any Swagger 2.0 or OpenAPI 3.x document into a fully callable API client at runtime.

Library
npm
v3.38.0
2,694stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
94/100Excellent
Development Activity92
Maintenance96
Community88
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture88
Code Quality85
Innovation80
Learning Curve75

Swagger Client is a JavaScript library that turns a Swagger 2.0 or OpenAPI 3.0/3.1/3.2 document into a fully callable API client at runtime. Point it at a spec URL or pass a spec object, and it resolves all $ref pointers, normalizes the definition across spec versions, and exposes every operation as a promise-based function grouped by tag, with no code-generation step required.

Under the hood it layers a dedicated resolver (built on Swagger’s own ApiDOM parser for OpenAPI 3.1/3.2, plus purpose-built strategies for 2.0 and 3.0), a request/response serialization layer, and an execute engine that builds and fires HTTP requests with the correct parameters, security schemes, and content types. It ships as ES modules, CommonJS, and a browser UMD bundle, and is maintained by the Swagger/SmartBear team as the client engine behind Swagger UI.

What You Get

  • Dynamic API client generation from any Swagger 2.0 or OpenAPI 3.0/3.1/3.2 document, with no build-time codegen step
  • A $ref-resolving engine (resolver) that dereferences and normalizes specs across all supported OpenAPI/Swagger versions
  • A promise-based .execute() call and an .apis[tag][operationId]() interface for invoking any operation directly
  • Distribution as ES modules, CommonJS, and a minified browser UMD bundle usable straight from unpkg
  • Configurable request/response interceptors for injecting auth, logging, or custom fetch behavior

Common Use Cases

  • Building interactive API explorers and testing consoles (it’s the call engine behind Swagger UI)
  • Generating a working JS client for an internal or partner API directly from its OpenAPI spec
  • Validating and resolving OpenAPI documents in CI to catch broken $ref pointers before publishing
  • Prototyping calls against a third-party API that only publishes an OpenAPI/Swagger definition

Under The Hood

Architecture swagger-client is organized as a small set of composable layers: src/index.js wires together a resolver, an execute engine, and an HTTP layer into the Swagger constructor, which returns a thenable whose resolved value is the client instance with .apis[tag][operationId]() methods attached via interfaces.js. Spec resolution is handled by a strategy pattern (src/resolver/strategies/) with dedicated implementations for OpenAPI 2.0, 3.0, and ApiDOM-based 3.1/3.2 parsing, so version-specific quirks are isolated from the rest of the pipeline. src/execute/index.js builds concrete HTTP requests (path/query/header/body parameter serialization, security scheme application) from a resolved spec plus an operation reference, and hands them to src/http/index.js, a thin fetch wrapper that normalizes request/response serialization and supports request/response interceptors. A separate subtree-resolver supports resolving just part of a large spec. This separation means changing how one OpenAPI version is parsed doesn’t touch execution or transport code.

Tech Stack The library is written in modern ES6+ JavaScript (no TypeScript) and transpiled with Babel into three distribution targets: CommonJS and ES module builds via babel, and a minified browser UMD bundle via Webpack. For OpenAPI 3.1/3.2 it depends on Swagger’s own @swagger-api/apidom-* parser and dereferencing packages rather than hand-rolled parsing, and uses ramda/ramda-adjunct for functional-style spec transformations, js-yaml for YAML parsing, and fast-json-patch for patch operations. It ships its own small polyfills (abortcontroller-polyfill, btoa) to keep behavior consistent between Node.js (native fetch, Node >=22 required) and browser environments.

Code Quality The test suite is extensive: roughly 90 spec files under test/ covering the resolver, executor, HTTP layer, and both OpenAPI 2.0 and 3.x execution paths, run through Jest with separate unit and coverage configs. Unusually, it also runs dedicated “artifact” tests that build and exercise each of the three published bundle formats (UMD/browser, ES, CommonJS) to catch regressions specific to a distribution target, not just the source. ESLint plus Husky pre-commit hooks and commitlint enforce style and commit conventions, and CodeQL security scanning runs in CI alongside semantic-release for automated versioning. There is no TypeScript or .d.ts typing layer, so type safety relies entirely on tests and runtime checks rather than the compiler.

API Design Getting started is a single call: new SwaggerClient(specUrl) returns a promise that resolves to a client whose .apis[tag][operationId]() methods mirror the tags and operationIds already defined in the spec, so there’s no manual endpoint wiring. requestInterceptor/responseInterceptor hooks give consumers a clean seam for auth and logging without needing to monkey-patch internals, and the resolver’s version differences (Swagger 2.0 vs OpenAPI 3.x) are fully hidden behind one consistent constructor and .execute() call.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search