{"id":15742,"library":"openapi-client-axios","title":"OpenAPI Client Axios","description":"openapi-client-axios is a JavaScript and TypeScript library designed to create dynamic API clients from OpenAPI v3 definitions at runtime. It leverages the popular axios library for HTTP requests, offering a powerful and flexible alternative to code generation for API consumption. The current stable version is 7.9.0. The library features automatic TypeScript type generation for API operations, full IntelliSense support, and a consistent API for calling operations using JavaScript methods based on operationIds. It operates isothermically in both browser and Node.js environments. Key differentiators include its no-code-generation approach, strong TypeScript integration, and reliance on existing, robust HTTP client infrastructure (axios), providing a predictable release cadence tied to upstream OpenAPI spec updates and axios releases.","status":"active","version":"7.9.0","language":"javascript","source_language":"en","source_url":"https://github.com/openapistack/openapi-client-axios","tags":["javascript","openapi","swagger","client","axios","frontend","browser","mock","typescript"],"install":[{"cmd":"npm install openapi-client-axios","lang":"bash","label":"npm"},{"cmd":"yarn add openapi-client-axios","lang":"bash","label":"yarn"},{"cmd":"pnpm add openapi-client-axios","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for making HTTP requests to the API.","package":"axios","optional":false},{"reason":"Needed for parsing YAML-formatted OpenAPI definitions.","package":"js-yaml","optional":false}],"imports":[{"note":"The primary class constructor is exported as a default export. For CommonJS environments, use `require('openapi-client-axios').default`.","wrong":"import { OpenAPIClientAxios } from 'openapi-client-axios';","symbol":"OpenAPIClientAxios","correct":"import OpenAPIClientAxios from 'openapi-client-axios';"},{"note":"Type definition for the dynamically generated API client instance returned by `api.getClient()` or resolved by `api.init()`.","symbol":"Client","correct":"import type { Client } from 'openapi-client-axios';"},{"note":"Type definition for the main OpenAPIClientAxios instance itself, useful when working with the API configuration.","symbol":"OpenAPIClient","correct":"import type { OpenAPIClient } from 'openapi-client-axios';"}],"quickstart":{"code":"import OpenAPIClientAxios from 'openapi-client-axios';\nimport type { Client } from 'openapi-client-axios';\n\nconst PETSTORE_URL = 'https://petstore.swagger.io/v2/swagger.json';\n\n// Initialize the client with the OpenAPI definition URL\nconst api = new OpenAPIClientAxios({ definition: PETSTORE_URL });\n\nasync function fetchAndCreatePet() {\n  try {\n    // Initialize the API client and get the generated operations\n    const client: Client = await api.getClient();\n\n    // Example 1: Fetch all pets (using a method derived from operationId 'findPetsByStatus')\n    console.log('Fetching available pets...');\n    const availablePetsResponse = await client.findPetsByStatus({ status: ['available'] });\n    console.log('Available pets:', availablePetsResponse.data.slice(0, 3)); // Log first 3\n\n    // Example 2: Create a new pet (using operationId 'addPet')\n    console.log('\\nAdding a new pet...');\n    const newPet = {\n      id: 987654321,\n      name: 'Buddy',\n      status: 'available',\n      category: { id: 1, name: 'Dogs' },\n      photoUrls: ['http://example.com/buddy.jpg']\n    };\n    const addPetResponse = await client.addPet(newPet);\n    console.log('New pet added:', addPetResponse.data);\n\n  } catch (error: any) {\n    console.error('An error occurred:', error.message);\n    if (error.response) {\n      console.error('API Response Data:', error.response.data);\n    }\n  }\n}\n\nfetchAndCreatePet();","lang":"typescript","description":"Initializes an OpenAPI client with a public Petstore definition, then demonstrates fetching existing data and creating new resources using the dynamically generated client methods."},"warnings":[{"fix":"Ensure your OpenAPI definition is accessible via a URL (e.g., `https://example.com/api/openapi.json`) or loaded into memory as a JavaScript object before being passed to the `definition` option. Resolve all `$refs` internally or manually if they point to remote locations.","message":"Since v7.0.0, `openapi-client-axios` no longer supports resolving remote `$refs` or loading OpenAPI definitions from local file paths. Definitions must be provided as local URLs or inline JavaScript/JSON objects.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"If you relied on specific features or behaviors of `swagger-parser`, you may need to pre-process your OpenAPI definition externally before passing it to `openapi-client-axios`.","message":"Version 7.0.0 removed the `@apidevtools/swagger-parser` dependency, replacing it with an internal, simpler dereferencer. This change significantly reduces bundle size but means custom parsing or complex `$ref` resolution strategies previously relying on `swagger-parser` may no longer work.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Upgrade your `axios` dependency to version 1.x or later. Ensure your project's `package.json` reflects a compatible `axios` version (e.g., `\"axios\": \">=1.0.0\"`).","message":"Version 6.0.0 introduced support for `axios` v1 and above. Older versions of `axios` might lead to compatibility issues or unexpected behavior.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Change your import statement from `const OpenAPIClientAxios = require('openapi-client-axios');` to `const OpenAPIClientAxios = require('openapi-client-axios').default;`.","message":"When using `openapi-client-axios` in a CommonJS environment (e.g., Node.js without ESM enabled), the `OpenAPIClientAxios` class is exposed as a default export, requiring `.default` to access.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Modify your import to `const OpenAPIClientAxios = require('openapi-client-axios').default;`.","cause":"Attempting to use `require('openapi-client-axios')` directly in CommonJS without accessing the default export of the module.","error":"Cannot read properties of undefined (reading 'default')"},{"fix":"Provide a URL for the OpenAPI definition, or read the file content yourself and pass the parsed JSON/YAML object directly: `new OpenAPIClientAxios({ definition: yourParsedObject })`.","cause":"Passing a local file path (e.g., `./openapi.json`) as the `definition` option, which is no longer supported for file system resolution since v7.0.0.","error":"Error: definition must be a string (URL) or an object"},{"fix":"Replace `require()` with the standard ES Module `import` syntax: `import OpenAPIClientAxios from 'openapi-client-axios';`.","cause":"Using CommonJS `require()` syntax in an environment configured for ES Modules (e.g., Node.js with `\"type\": \"module\"` in `package.json` or in a browser bundling context expecting ESM).","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm"}