Skip to content
Home / Migrations / AxiosNative fetch

Axios Native fetch

easy

Native fetch is available in all modern runtimes (Node 18+, browsers, Deno, Bun). Removes a dependency and works seamlessly with Next.js caching.

Estimated: 1-3h · 5 steps
Progress0%
Step 1: Remove Axios
npm uninstall axios
Step 2: Replace GET requests

axios.get(url).then(r => r.data) → fetch(url).then(r => r.json()). Note: fetch doesn't throw on 4xx/5xx by default.

const data = await fetch('/api/users').then(r => { if (!r.ok) throw new Error(r.statusText); return r.json() })
Step 3: Replace POST requests

axios.post(url, data) → fetch with method, headers, and JSON.stringify body.

await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) })
Step 4: Replace interceptors

Axios interceptors → middleware function wrapping fetch. Create a typed fetchApi() helper.

Step 5: Add error handling

Create a wrapper that throws on non-2xx responses and parses errors consistently.

All HTTP calls work. Error handling tested. TypeScript types correct.