Home
/
Framework Examples
/
Axios
Axios JSON API examples (GET, POST, PUT, DELETE, errors)
Last updated 2026-05-20
When to use this
Use this when you want automatic JSON parsing, interceptors, transformers, and a status-aware error path out of the box. Same API in Node and the browser.
Example JSON
Reference JSON payload returned by /users/1 (used in every snippet below).
{
"id": 1,
"firstName": "Emily",
"lastName": "Johnson",
"email": "emily.johnson@x.example",
"username": "emilys",
"image": "https://jsonexamples.com/image/200?text=User+1",
"address": {
"address": "626 Main Street",
"city": "Phoenix",
"state": "OK",
"postalCode": "29920",
"country": "United States"
}
}
Request examples
GET single
GET list with params
POST create
PUT update
DELETE
Auth interceptor
Error interceptor
Copy
import axios from 'axios';
const api = axios.create({ baseURL: 'https://jsonexamples.com' });
async function getUser(id) {
const { data } = await api.get('/users/' + id);
return data;
}
Copy
const { data } = await api.get('/users', {
params: { limit: 10, skip: 0, select: 'firstName,lastName,email' }
});
Copy
const { data } = await api.post('/users/add', { firstName: 'Ada', lastName: 'Lovelace' });
Copy
const { data } = await api.put('/users/1', { firstName: 'Updated' });
Copy
const { data } = await api.delete('/users/1');
Copy
api.interceptors.request.use(cfg => {
const token = localStorage.getItem('accessToken');
if (token) cfg.headers.Authorization = 'Bearer ' + token;
return cfg;
});
Copy
api.interceptors.response.use(
res => res,
err => {
if (err.response?.status === 401) redirectToLogin();
return Promise.reject(err);
}
);
Try the live endpoint
Click below to call /users/1 from your browser.
Call /users/1
// click the button to populate this block
Common variations
Cancel a request
{
"snippet": "const controller = new AbortController();\napi.get('/products', { signal: controller.signal });\ncontroller.abort();"
}
Copy link to this example
https://jsonexamples.com/framework-examples/axios