Getting Started
Installation
With fetch (built into core — zero extra dependencies):
npm install @routar/coreWith Axios:
npm install @routar/core @routar/axios axiosQuick Start
1. Define your schema
import { z } from 'zod'
const TodoSchema = z.object({
id: z.number(),
title: z.string(),
completed: z.boolean(),
})2. Define endpoints and router
import { endpoint, defineRouter } from '@routar/core'
const todoRouter = defineRouter('/todos', {
getList: endpoint({
method: 'GET',
path: '/',
response: z.array(TodoSchema),
}),
getDetail: endpoint({
method: 'GET',
path: '/:id',
request: z.object({ path: z.object({ id: z.number() }) }),
response: TodoSchema,
}),
create: endpoint({
method: 'POST',
path: '/',
request: z.object({ body: z.object({ title: z.string() }) }),
response: TodoSchema,
}),
})3. Create executor and API client
import { createApi } from '@routar/core'
import { createFetchExecutor } from '@routar/core'
const executor = createFetchExecutor('https://api.example.com')
const todoApi = createApi(executor, todoRouter)4. Make requests
const todos = await todoApi.getList({})
const todo = await todoApi.getDetail({ path: { id: 1 } })
const next = await todoApi.create({ body: { title: 'buy milk' } })All return types are fully inferred — no type annotations needed. In your IDE, hovering over todos shows Todo[] and todo shows Todo.
AbortSignal
Pass an AbortSignal as the second argument to cancel in-flight requests:
const controller = new AbortController()
const todos = await todoApi.getList({}, controller.signal)
controller.abort() // cancels the requestWith Plugins
import { createFetchExecutor, logger } from '@routar/core'
const executor = createFetchExecutor('https://api.example.com', {
plugins: [logger()],
retry: 3,
timeout: 8_000,
})With Axios
import axios from 'axios'
import { createAxiosExecutor } from '@routar/axios'
const executor = createAxiosExecutor(
axios.create({ baseURL: 'https://api.example.com' })
)Next Steps
- API Reference — full documentation for every export
- Executors — fetch vs. axios, SSR/CSR patterns
- Guides — SSR/CSR, custom executors, error handling
Last updated on