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.
What it is
axios is a promise-based HTTP client for JavaScript that works in both browsers and Node.js. It provides request and response interceptors, automatic JSON transformation, CSRF protection, request cancellation, and timeout handling. Battle-tested since 2014, axios is the most widely used HTTP client in the JavaScript ecosystem.
Frontend and backend JavaScript developers who need a consistent HTTP interface across environments benefit most. Whether you are calling REST APIs from a React app or making server-to-server requests in Node.js, axios provides the same API surface.
How it saves time or tokens
axios eliminates the boilerplate of native fetch calls: no manual JSON.stringify for request bodies, no response.json() calls, no separate error handling for network errors vs HTTP errors. Interceptors let you inject auth tokens, logging, or retry logic in one place rather than at every call site. The isomorphic design means you write one HTTP layer that works in SSR and client-side rendering without conditional imports.
How to use
- Install axios:
npm install axios
- Make a GET request:
import axios from 'axios';
const response = await axios.get('https://api.example.com/users');
console.log(response.data); // Already parsed JSON
- Configure a reusable instance with interceptors:
const api = axios.create({
baseURL: 'https://api.example.com',
timeout: 5000
});
api.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${getToken()}`;
return config;
});
Example
// POST with automatic JSON serialization
await axios.post('/api/users', {
name: 'Jane',
email: 'jane@example.com'
});
// Concurrent requests
const [users, posts] = await Promise.all([
axios.get('/api/users'),
axios.get('/api/posts')
]);
// Error handling with response details
try {
await axios.get('/api/protected');
} catch (error) {
if (error.response) {
console.log(error.response.status); // 401
console.log(error.response.data); // Error body
}
}
// Cancel a request
const controller = new AbortController();
axios.get('/api/slow', { signal: controller.signal });
controller.abort();
Related on TokRepo
- AI Coding Tools -- Developer libraries and utilities on TokRepo
- API Integration Tools -- API clients and integration frameworks
Common pitfalls
- axios does not throw on HTTP 4xx/5xx status codes by default in older versions. In current versions (1.x), it does throw. Check your version's behavior and configure
validateStatusif needed. - The default timeout is 0 (no timeout). Always set an explicit timeout in production to avoid hanging requests.
- axios instances share interceptors added after creation. If you modify interceptors dynamically, each API call may behave differently depending on timing.
Frequently Asked Questions
axios provides automatic JSON parsing, request/response interceptors, timeout support, and consistent error handling out of the box. fetch requires manual JSON handling, has no interceptor pattern, and does not reject on HTTP errors. axios works identically in Node.js and browsers; fetch needs a polyfill in older Node versions.
Yes. axios ships with built-in TypeScript type definitions. You can type request and response data using generics: axios.get<User[]>('/api/users') returns a typed response. The interceptor types are also fully typed.
Yes. axios is framework-agnostic. Import it in any React, Vue, Angular, or Svelte component. For server-side rendering frameworks like Next.js or Nuxt, axios works on both server and client sides with the same API.
Use the axios-retry plugin or implement retry logic in a response interceptor. axios-retry adds automatic retries with exponential backoff for network errors and 5xx responses. Install it alongside axios and configure the retry count and delay.
Yes. axios continues to receive updates and security patches. The 1.x line is stable and widely used. The library has a large contributor base and remains the most downloaded HTTP client on npm.
Citations (3)
- axios GitHub Repository— axios is the most popular HTTP client for JavaScript
- axios Official Documentation— axios provides interceptors, automatic JSON, and CSRF protection
- axios API Documentation— axios is isomorphic for browser and Node.js environments
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.