# axios — Promise-Based HTTP Client for Browser & Node.js > axios is the most popular HTTP client for JavaScript — promise-based, isomorphic (browser + Node), with request/response interceptors, automatic JSON transforms, CSRF protection, and built-in cancellation. Battle-tested since 2014. ## Install Save in your project root: ## Quick Use ```bash npm i axios ``` ```ts import axios from "axios"; // GET const { data } = await axios.get("https://api.github.com/users/williamwangai"); // POST with JSON body await axios.post("/api/users", { name: "William", email: "w@example.com" }); // Create instance with defaults const api = axios.create({ baseURL: "https://api.tokrepo.com", headers: { Authorization: `Bearer ${token}` }, timeout: 10000, }); api.interceptors.response.use( (res) => res, (err) => { if (err.response?.status === 401) location.href = "/login"; return Promise.reject(err); } ); ``` ## Intro axios is the most popular HTTP client for JavaScript — promise-based, isomorphic (works in browser and Node.js with the same API), battle-tested since 2014. Despite the modern `fetch()` API being available everywhere, axios still ships in 100M+ weekly npm installs for its superior DX. - **Repo**: https://github.com/axios/axios - **Stars**: 108K+ - **License**: MIT ## What axios Does - **Promise API** — async/await friendly - **Isomorphic** — same code browser + Node.js - **Interceptors** — transform requests/responses globally - **Auto JSON** — serializes request body, parses response - **Timeouts** — `timeout` option - **Cancellation** — `AbortController` support - **Progress events** — upload/download progress - **XSRF protection** — cookie-based CSRF token - **Instance config** — `axios.create({ baseURL, headers })` ## Architecture Wraps `XMLHttpRequest` in browsers and `http`/`https` modules in Node.js. Request goes through interceptor chain → adapter → transformer → promise. Error is a normal rejected promise with `err.response`/`err.request` for inspection. ## Self-Hosting Client library only. ## Key Features - Isomorphic (browser + Node) - Request/response interceptors - Automatic JSON transforms - Form data & multipart support - Streaming in Node.js - Cancellation via AbortController - Progress events (uploads) - TypeScript types built in - AbortController and signal support ## Comparison | Client | Isomorphic | Interceptors | Auto JSON | Bundle | |---|---|---|---|---| | axios | Yes | Yes | Yes | ~14KB | | fetch | Yes (modern) | No (manual) | No (manual) | 0 | | ky | Yes (modern) | Yes | Yes | ~5KB | | ofetch | Yes | Limited | Yes | ~4KB | | got | Node only | Yes | Yes | ~40KB | ## FAQ **Q: fetch is good enough — why still use axios?** A: fetch doesn't auto-parse JSON, doesn't auto-throw on HTTP errors, has no interceptors, and isn't supported in older Node versions. axios saves time out of the box. **Q: How do I cancel a request?** A: `const controller = new AbortController(); axios.get(url, { signal: controller.signal }); controller.abort();` **Q: How do I handle CSRF?** A: axios reads the `XSRF-TOKEN` cookie by default and puts it in the `X-XSRF-TOKEN` header, matching the CSRF mechanisms used by Rails/Laravel. ## Sources & Credits - Docs: https://axios-http.com - GitHub: https://github.com/axios/axios - License: MIT --- Source: https://tokrepo.com/en/workflows/axios-promise-based-http-client-browser-node-js-b3e550af Author: AI Open Source