Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsApr 11, 2026·2 min de lecture

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.

Prêt pour agents

Staging sûr pour cet actif

Cet actif est d'abord staged. Le prompt copié demande à l'agent d'inspecter les fichiers staged avant d'activer scripts, config MCP ou config globale.

Stage only · 17/100Policy : staging
Surface agent
Tout agent MCP/CLI
Type
CLI Tool
Installation
Stage only
Confiance
Confiance : Established
Point d'entrée
step-1.md
Commande de staging sûr
npx -y tokrepo@latest install b3e550af-3588-11f1-9bc6-00163e2b0d79 --target codex

Stage les fichiers d'abord; l'activation exige la revue du README et du plan staged.

TL;DR
axios provides a promise-based HTTP client that works identically in browsers and Node.js with interceptors and automatic JSON handling.
§01

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.

§02

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.

§03

How to use

  1. Install axios:
npm install axios
  1. 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
  1. 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;
});
§04

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();
§05

Related on TokRepo

§06

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 validateStatus if 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.

Questions fréquentes

How does axios differ from fetch?+

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.

Does axios support TypeScript?+

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.

Can I use axios with React or Vue?+

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.

How do I retry failed requests with axios?+

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.

Is axios still maintained in 2026?+

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.

Sources citées (3)

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires