API Specification Diff Tool
raw JSON →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 Error: Could not resolve reference: 'some-external-file.yaml' ↓
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 }); error TypeError: ApiSmartDiff.apiCompare is not a function ↓
ApiSmartDiff.apiCompare(before, after). If using a module bundler, verify your import statement for apiCompare is correct. error Unexpected token '{' in JSON at position X ↓
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 wrong
const { apiCompare } = require('api-smart-diff')correctimport { apiCompare } from 'api-smart-diff' - Diff wrong
import { Diff } from 'api-smart-diff'correctimport type { Diff } from 'api-smart-diff' - ApiSmartDiff wrong
import { apiCompare } from 'api-smart-diff'; apiCompare(before, after)correctvar { 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));