Registry / devops / openapi-fetch

openapi-fetch

JSON →
library0.17.0jsnpmunverified

Type-safe fetch client for OpenAPI schemas, version 0.17.0. Maintained actively on GitHub (openapi-ts/openapi-typescript) with frequent releases. Weighs 6 kB (minified) and has virtually zero runtime overhead. Differs from alternatives like axios or openapi-typescript-codegen by providing automatic type inference from generated TypeScript types, eliminating manual type assertions and reducing bundle size. Works with any framework or vanilla JS, and integrates with openapi-typescript for schema-to-TypeScript generation. Supports GET, PUT, POST, DELETE, OPTIONS, HEAD, PATCH, TRACE methods. ESM-only; no CJS support.

npm install openapi-fetch
INSTALL
IMPORT
SIG · OPENAPI-FETCH
O
openapi-fetch
devopsjavascriptv0.17.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import createClient from "openapi-fetch";
const createClient = require("openapi-fetch");
Package is ESM-only (since first release); CommonJS require() will fail.
createClient
import createClient from "openapi-fetch";
import { createClient } from "openapi-fetch";
createClient is the default export; named import will be undefined.
type paths
import type { paths } from "./my-schema";
import { paths } from "./my-schema";
Use 'import type' to avoid bundling runtime artifacts; paths is a type-only export from openapi-typescript-generated files.

Shows client creation with type parameter from generated schema and a typed GET request with path parameters, handling data and error responses.

import createClient from "openapi-fetch"; import type { paths } from "./api-schema"; // generated by openapi-typescript const client = createClient<paths>({ baseUrl: "https://api.example.com/v1", }); async function getPost(id: string) { const { data, error } = await client.GET("/posts/{postId}", { params: { path: { postId: id }, }, }); if (error) { console.error("Error:", error); return; } console.log("Post:", data); }
Debug
Known issues
breakingPackage is ESM-only. Using require() will throw a runtime error.
fix
Use import syntax or dynamic import(). If using CommonJS project, switch to ESM or use dynamic import.
affects: >=0.1.0
gotchaDefault export named createClient; named import { createClient } is undefined.
fix
Use default import: import createClient from 'openapi-fetch';
affects: >=0.1.0
gotchaMust generate TypeScript types from OpenAPI schema using openapi-typescript before using openapi-fetch; otherwise type safety is lost.
fix
Install openapi-typescript as dev dependency and run: npx openapi-typescript ./path/to/schema.yaml -o ./types.ts
affects: >0
deprecatedIn version <0.6, createClient accepted a second generic parameter for custom fetch; now use the 'fetch' option in the config object.
fix
Pass custom fetch as part of options object: createClient<paths>({ baseUrl, fetch: myFetch })
affects: <0.6
Errors
Common errors & fixes
Error: createClient is not a function
Using named import { createClient } instead of default import.
fix
Replace import { createClient } with import createClient.
TypeError: Cannot find module 'openapi-fetch'
Using require() in a CommonJS environment; package is ESM-only.
fix
Switch to ESM (type: "module" in package.json) or use dynamic import: const createClient = (await import('openapi-fetch')).default;
Type 'string' is not assignable to type 'never'
paths type not generated or incorrectly typed; likely missing openapi-typescript step or incorrect schema path.
fix
Run npx openapi-typescript on your OpenAPI schema and import the resulting .d.ts file.
Upgrade
Version history
0.17.0latest on npm
Audit
Dependencies
openapi-typescriptoptionalRequired to generate TypeScript types from an OpenAPI schema; openapi-fetch consumes these types for type safety.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
openapi-fetch — npm install openapi-fetch · libregistry