OpenAPI SDK Generator
raw JSON →The `@readme/api` package provides a robust command-line interface and programmatic API for generating SDKs from OpenAPI (formerly Swagger) definitions. It takes an OpenAPI specification file and outputs a fully-typed JavaScript SDK, designed to be consistent across CommonJS, ES Modules, and TypeScript environments. The current stable version is `6.1.3`, with active development towards `v7.0.0`, which introduces significant overhauls to the SDK generation strategy, notably streamlining the process by removing complex configuration questions and enhancing type and module resolution. This package differentiates itself by offering a complete SDK generation pipeline that handles schema imports and type safety, aiming to provide a 'just works' experience for developers integrating with OpenAPI-driven APIs. Its release cadence is active, with minor and patch updates regularly addressing fixes and quality-of-life improvements, while major versions introduce breaking changes and significant architectural shifts.
Common errors
error Error: The '@readme/api' package requires Node.js version X or higher. ↓
error SyntaxError: Cannot use import statement outside a module ↓
v6.x, use const { symbol } = require('@readme/api'); for CommonJS environments. For modern setups or v7.x aiming for ESM consistency, ensure your package.json specifies "type": "module" or use a .mjs file extension. Warnings
breaking The minimum required Node.js version for `v7.x` has been bumped to `v20.10.0`. ↓
breaking Version `6.0.0` dropped support for Node.js 14. ↓
breaking The SDK generation strategy has been overhauled in `v7.x`. This release streamlines the process by removing previous configuration questions and aiming for a 'just works' experience with consistent types and module resolution. ↓
gotcha A security vulnerability related to the `oas` dependency was resolved in `v6.1.3`. ↓
Install
npm install api yarn add api pnpm add api Imports
- generate wrong
const { generate } = require('@readme/api');correctimport { generate } from '@readme/api'; - APIOptions
import type { APIOptions } from '@readme/api'; - * wrong
const api = require('@readme/api');correctimport * as api from '@readme/api';
Quickstart
import { generate } from '@readme/api';
import path from 'node:path';
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function createDummyOpenApiSpec(specPath: string) {
const dummyOpenApiSpec = {
openapi: '3.0.0',
info: { title: 'My API', version: '1.0.0' },
paths: {
'/users': {
get: {
summary: 'Get all users',
responses: {
'200': {
description: 'A list of users',
content: { 'application/json': { schema: { type: 'array', items: { $ref: '#/components/schemas/User' } } } }
}
}
}
}
},
components: {
schemas: {
User: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
name: { type: 'string' }
}
}
}
}
};
await fs.writeFile(specPath, JSON.stringify(dummyOpenApiSpec, null, 2));
}
async function runSdkGeneration() {
const outputBaseDir = path.resolve(__dirname, 'temp-sdk-output');
await fs.mkdir(outputBaseDir, { recursive: true });
const openApiPath = path.join(outputBaseDir, 'openapi.json');
await createDummyOpenApiSpec(openApiPath);
const sdkOutputDir = path.join(outputBaseDir, 'generated-sdk');
await fs.mkdir(sdkOutputDir, { recursive: true });
console.log(`Starting SDK generation...`);
console.log(` OpenAPI Spec: ${openApiPath}`);
console.log(` Output Dir: ${sdkOutputDir}`);
// The '@readme/api' package provides a `generate` function to create SDKs
// from an OpenAPI definition. It expects the path to the OpenAPI
// file and the desired output directory for the generated SDK.
// In v7, the generation strategy is overhauled for consistent module
// resolution and types across CJS, ESM, and TypeScript.
await generate(openApiPath, sdkOutputDir);
console.log('SDK generation complete!');
console.log(`You can now find the generated SDK in: ${sdkOutputDir}`);
console.log(`Example: const { default: sdk } = await import('${sdkOutputDir}');`);
// Clean up temporary files
await fs.rm(outputBaseDir, { recursive: true, force: true });
console.log(`Cleaned up temporary directory: ${outputBaseDir}`);
}
runSdkGeneration().catch(console.error);