API Specification Diff Tool
api-smart-diff is a JavaScript/TypeScript library designed to compute detailed differences between two JSON-based API specifications. It currently supports OpenAPI 3.0, AsyncAPI 2.x, JSON Schema, and GraphQL via GraphApi. Version 1.0.6 is the current stable release, offering a robust set of features for API versioning and changelog generation. Key differentiators include its ability to classify changes into 'breaking', 'non-breaking', 'deprecated', and 'annotation' types, generate human-readable descriptions for each change, and resolve `$ref` pointers (with external bundling). It provides extensive customization options for comparison rules, classification, and output annotation, making it highly adaptable for various CI/CD pipelines and documentation generation tasks. The library works seamlessly in both Node.js and browser environments and ships with full TypeScript support.
Common errors
-
Error: Could not resolve reference: 'some-external-file.yaml'
cause The API specification passed to `apiCompare` contains external `$ref` pointers that have not been pre-resolved or bundled.fixInstall and use `api-ref-bundler` to resolve all external references in your `before` and `after` specification objects before passing them to `apiCompare`. Example: `const bundledSpec = await bundle({ spec: originalSpec });` -
TypeError: ApiSmartDiff.apiCompare is not a function
cause This error typically occurs in a browser environment when attempting to call `apiCompare` directly without referencing the global `ApiSmartDiff` object, or if the CDN script failed to load.fixEnsure the CDN script has loaded correctly in your HTML. Access the function via `ApiSmartDiff.apiCompare(before, after)`. If using a module bundler, verify your import statement for `apiCompare` is correct. -
Unexpected token '{' in JSON at position Xcause One of the input API specifications (`before` or `after`) is not valid JSON or contains syntax errors, which prevents the library from parsing it correctly.fixValidate your API specification files (OpenAPI, AsyncAPI, JsonSchema) against their respective schemas or use a linter to identify and correct any JSON syntax errors before passing them to `apiCompare`.
Warnings
- gotcha For API specifications containing external `$ref` pointers, these references must be bundled and resolved *before* passing the specification objects to `apiCompare`. The `api-smart-diff` library itself does not resolve external references.
- breaking Support for Swagger 2.0 has been removed or was never fully implemented and is now explicitly unsupported. If your project relies on Swagger 2.0 diffing, this library will not work as expected.
- gotcha AsyncAPI 3.x and gRPC specification comparisons are currently on the roadmap and are not yet supported. Attempting to diff these specification types will likely result in incorrect or incomplete output.
- gotcha When using `api-smart-diff` in a browser environment via the CDN script, the library exposes a global variable `ApiSmartDiff`. Attempting to use ES module `import` syntax without a proper module bundler will fail.
Install
-
npm install api-smart-diff -
yarn add api-smart-diff -
pnpm add api-smart-diff
Imports
- apiCompare
const { apiCompare } = require('api-smart-diff')import { apiCompare } from 'api-smart-diff' - Diff
import { Diff } from 'api-smart-diff'import type { Diff } from 'api-smart-diff' - ApiSmartDiff
import { apiCompare } from 'api-smart-diff'; apiCompare(before, after)var { diffs, merged } = ApiSmartDiff.apiCompare(before, after)
Quickstart
import { apiCompare } from 'api-smart-diff';
const beforeSpec = {
openapi: '3.0.0',
info: { title: 'My API', version: '1.0.0' },
paths: {
'/users': {
get: { summary: 'Get all users', operationId: 'getUsers' }
}
}
};
const afterSpec = {
openapi: '3.0.0',
info: { title: 'My API', version: '1.0.1', description: 'Added new endpoint' },
paths: {
'/users': {
get: { summary: 'Retrieve users', operationId: 'getUsers' }
},
'/products': {
post: { summary: 'Create a product', operationId: 'createProduct' }
}
}
};
const { diffs, merged } = apiCompare(beforeSpec, afterSpec);
console.log('Detected Diffs:', JSON.stringify(diffs, null, 2));
console.log('Merged Specification with Metadata:', JSON.stringify(merged, null, 2));