Getting started
valifetch is a type-safe HTTP client built on native fetch with Valibot schema validation. It is like ky, but with built-in request and response validation.
Try it live
Open the demo in StackBlitz to run valifetch in your browser, no install needed. It shows response validation, request-body validation and typed errors against a real API.
Installation
npm install valifetch valibotdeno add jsr:@haihv/valifetchbunx jsr add @haihv/valifetchIn Deno you can also import directly:
import valifetch from 'jsr:@haihv/valifetch';
import { bearerAuth } from 'jsr:@haihv/valifetch/auth';
import { createMock } from 'jsr:@haihv/valifetch/mock';The JSR package publishes the main entry plus the auth and mock subpaths. valifetch/error and valifetch/types are npm-only; on JSR, import ValifetchError and the core types from the main entry, JwtRefreshOptions from the auth subpath, and MockCall, MockHandler and ValifetchMock from the mock subpath. See Subpath imports.
Requirements
- Node.js >= 22.0.0 (uses native
fetch) - valibot >= 1.0.0 (a peer dependency)
- TypeScript
moduleResolution: "node16"(or"bundler"/"nodenext"). This is required to resolve subpath imports likevalifetch/types,valifetch/error,valifetch/authandvalifetch/mock.
Your first request
import valifetch from 'valifetch';
import * as v from 'valibot';
const UserSchema = v.object({
id: v.number(),
name: v.string(),
email: v.pipe(v.string(), v.email()),
});
const user = await valifetch.get('https://api.example.com/users/1', {
responseSchema: UserSchema,
});
// user is fully typed: { id: number; name: string; email: string }There is no .json() call. JSON responses are parsed automatically, then validated against responseSchema when one is given.
Three ways to get a result type:
// 1. From a schema: validated at runtime, type inferred
const a = await valifetch.get('https://api.example.com/users/1', {
responseSchema: UserSchema,
});
// 2. From a generic: no runtime validation
type User = { id: number; name: string };
const b = await valifetch.get<User>('https://api.example.com/users/1');
// 3. Nothing: the result is unknown
const c = await valifetch.get('https://api.example.com/data');HTTP methods
valifetch.get(url, options);
valifetch.post(url, options);
valifetch.put(url, options);
valifetch.patch(url, options);
valifetch.delete(url, options);
valifetch.head(url, options);
valifetch.options(url, options);Bodyless methods
head() always resolves void and does not accept responseSchema or responseType. get() and head() do not accept json, form or body. delete() and options() use the same bodyless option type as get(), so they reject those options too; send bodies with post, put or patch.
Configure once, reuse everywhere
Most apps create an instance with shared defaults:
const api = valifetch.create({
prefixUrl: 'https://api.example.com',
timeout: 10_000,
headers: { Accept: 'application/json' },
});
const user = await api.get('/users/1', { responseSchema: UserSchema });Continue with Instances, or jump to Requests and Validation.
Where to go next
- Requests: bodies, path params, search params, timeouts, cancellation and deduplication.
- Errors: the error codes you can catch.
- Recipes: authentication, SSE streaming, mocking, pagination.
- API reference: every exported symbol, generated from the source.