Create an auto-generated JS client for any REST API

Posted on Sun Jun 12 2022
javascript api REST

openapi-client-axios is an NPM package which can create a JS client for open OpenAPI enabled APIs.

This is extremely useful for interacting with services which do not provide an out of the box JS SDK. For example I’m working with Printful which is doesn’t provide any language specific SDKs out-the-box. However their REST API complies with the OpenAPI standard which means it will work perfectly with open openapi-client-axios . Here’s how I was able to use it.

Firstly we need to install openapi-client-axios into our project using :

npm install --save axios openapi-client-axios

Or with Yarn:

yarn add axios openapi-client-axios

Now we can create a utility which configures the client use printfuls rest api. To do this I like to organise my files like so src/lib/printful/printfulClient.ts . Yes I’m using TypeScript, however this will work with plain .js too.

inside printfulClient.ts we can configure out client like so:

import OpenAPIClientAxios from "openapi-client-axios";

export const printfulClient = () => {

	const api = new OpenAPIClientAxios({
		definition: "src/lib/printful/open-api.json",
	});
	  
	const client = api.initSync();
	
	return client;
};

The OpenAPIClientAxios class requires parameter object, the minium it needs is for the definition property to be populated with a valid path to a valid open-api schema object. The documentation for all valid parameter can be found here.

Note I’ve configured the definition parameter to point to a open-api.json which is co-located with my printfulClient.ts file. I have pre-downloaded Printfuls open api spec here.