JavaScript client library for consuming OpenAPI-enabled APIs with axios. Types included.
- Create API clients from OpenAPI v3 definitions
- Client is configured in runtime. No generated code!
- Generate TypeScript definitions (.d.ts) for your APIs with full IntelliSense support
- Easy to use API to call API operations using JavaScript methods
client.getPet(1)
client.searchPets()
client.searchPets({ ids: [1, 2, 3] })
client.updatePet(1, payload)
- Built on top of the robust axios JavaScript library
- Isomorphic, works both in browser and Node.js
See full README.md
See DOCS.md
npm install --save axios openapi-client-axios
yarn add axios openapi-client-axios
With promises / CommonJS syntax:
const OpenAPIClientAxios = require('openapi-client-axios').default;
const api = new OpenAPIClientAxios({ definition: 'https://example.com/api/openapi.json' });
api.init()
.then(client => client.getPetById(1))
.then(res => console.log('Here is pet id:1 from the api', res.data));
With async-await / ES6 syntax:
import OpenAPIClientAxios from 'openapi-client-axios';
const api = new OpenAPIClientAxios({ definition: 'https://example.com/api/openapi.json' });
api.init();
async function createPet() {
const client = await api.getClient();
const res = await client.createPet(null, { name: 'Garfield' });
console.log('Pet created', res.data);
}
The openapi-client-axios
library works both in Node.js and browsers.
However, you may need to add NodeJS polyfills in your build configuration:
// webpack.config.js
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
module.exports = {
plugins: [new NodePolyfillPlugin()],
};
// rollup.config.js
import nodePolyfills from 'rollup-plugin-node-polyfills';
export default {
plugins: [nodePolyfills()],
}
// vite.config.js
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
export default defineConfig({
plugins: [nodePolyfills()],
})
OpenAPI Client Axios is Free and Open Source Software. Issues and pull requests are more than welcome!