Home
/
Framework Examples
/
fetch
fetch() JSON API examples (GET, POST, PUT, DELETE)
Last updated 2026-05-20
When to use this
Use this when you are in the browser or modern Node and want zero dependencies. fetch() ships natively and is great for simple JSON APIs, but you handle JSON parsing and non-2xx detection yourself.
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 (paginated)
POST create
PUT update
DELETE
With Bearer token
Error handling
Copy
async function getUser(id) {
const res = await fetch('https://jsonexamples.com/users/' + id);
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
}
Copy
async function listUsers(skip = 0, limit = 30) {
const params = new URLSearchParams({ skip, limit });
const res = await fetch('https://jsonexamples.com/users?' + params);
return res.json();
}
Copy
async function addUser(body) {
const res = await fetch('https://jsonexamples.com/users/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
return res.json();
}
Copy
async function updateUser(id, patch) {
const res = await fetch('https://jsonexamples.com/users/' + id, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(patch)
});
return res.json();
}
Copy
async function deleteUser(id) {
const res = await fetch('https://jsonexamples.com/users/' + id, { method: 'DELETE' });
return res.json();
}
Copy
async function getMe(token) {
const res = await fetch('https://jsonexamples.com/auth/me', {
headers: { Authorization: 'Bearer ' + token }
});
if (res.status === 401) throw new Error('unauthorized');
return res.json();
}
Copy
async function call(url) {
const res = await fetch(url);
if (!res.ok) {
let body = null;
try { body = await res.json(); } catch (e) {}
const err = new Error('HTTP ' + res.status);
err.status = res.status;
err.body = body;
throw err;
}
return res.json();
}
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
AbortController for timeouts
Cancel the request after 5 seconds.
{
"snippet": "const ac = new AbortController();\nsetTimeout(() => ac.abort(), 5000);\nawait fetch(url, { signal: ac.signal });"
}
Copy link to this example
https://jsonexamples.com/framework-examples/fetch