Installation
Install
bash
npm install @vafast/api-clientbash
pnpm add @vafast/api-clientbash
yarn add @vafast/api-clientbash
bun add @vafast/api-clientRequirements
- Node.js 18+ / Bun 1+ / modern browsers (Chrome 88+, Firefox 85+, Safari 14+, Edge 88+)
- TypeScript 5+ (recommended)
Quick Check
typescript
import { createClient, eden } from '@vafast/api-client'
// In real projects, generate types with the CLI or define the contract yourself
type Api = {
users: {
get: { query?: { page?: number }; return: { users: string[] } }
}
}
const api = eden<Api>(createClient('http://localhost:3000'))
const { data, error } = await api.users.get({ page: 1 })
if (error) {
console.error(`${error.code}: ${error.message}`)
} else {
console.log(data.users)
}Sync Types from the Server (Recommended)
When your backend is Vafast, generate a type-safe client with the CLI:
bash
npx vafast sync --url http://localhost:3000 --out src/api.generated.tstypescript
import { createClient } from '@vafast/api-client'
import { createApiClient } from './api.generated'
const api = createApiClient(createClient({ baseURL: '/api', timeout: 30_000 }))
const { data, error } = await api.users.get()
if (error) return
console.log(data)See CLI Tool for details.
Environment Variables
typescript
import { createClient, eden } from '@vafast/api-client'
const client = createClient({
baseURL: import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3000',
timeout: Number(import.meta.env.VITE_API_TIMEOUT ?? 30_000),
})Next Steps
- Basic Usage — chained calls, error handling, middleware
- Advanced Usage — multi-tenancy, refresh queues, multiple services
- Overview — feature overview and SSE
- Testing — test the client with
server.fetch/ mocks