GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • openapi-stack
  • Documentation
  • Backend
  • Client
  • CLI
  • Examples
  • GitHub
  • Why API First?
  • Operation Methods
  • Typegen
  • Bundling
  • Reference
  • Utility Libraries
  • Examples

This view

  • Original page
  • New address

GeezerVison is showing openapistack.co — Quick Start: Client | openapi-stack

openapi-client-axios logo

openapi-client-axiosGitHub

CI npm version npm downloads GitHub stars bundle size License Buy me a coffee

JavaScript client library for consuming OpenAPI-enabled APIs with axios. Types included.

Battle-tested in production. See who uses openapi-stack →

Features​

  • 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

Quick Start​

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);
}

Typesafe Clients​

TypeScript IntelliSense

Use the openapicmd typegen command to generate typescript type files (.d.ts) for OpenAPIClient instances using an OpenAPI definition file.

npx openapicmd typegen ./openapi.yaml > src/types/openapi.d.ts

The output file exports a type called Client, which can directly be used with instances created with OpenAPIClientAxios.

import type { Client as PetStoreClient } from "./openapi.d.ts";

const client = await api.getClient<PetStoreClient>();

Contributors​

openapi-client-axios is built by dozens of contributors across the openapi-stack project.

openapi-client-axios contributors
  • Features
  • Quick Start
  • Typesafe Clients
  • Contributors
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.