Home
/
Framework Examples
/
Node.js
Node.js JSON API examples (fetch, https, axios)
Last updated 2026-05-20
When to use this
Use this for server-side calls, CLI tools, or scheduled jobs. Node 18+ ships native fetch; for older versions, use the built-in https module or axios. Examples below use async/await throughout.
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
Native fetch (Node 18+)
https module (no deps)
POST JSON
Streaming a large JSON
try/catch + status
Copy
// Node 18+
const user = await fetch('https://jsonexamples.com/users/1').then(r => r.json());
console.log(user);
Copy
const https = require('https');
function getJson(url) {
return new Promise((resolve, reject) => {
https.get(url, res => {
let body = '';
res.on('data', c => (body += c));
res.on('end', () => {
try { resolve(JSON.parse(body)); } catch (e) { reject(e); }
});
}).on('error', reject);
});
}
const user = await getJson('https://jsonexamples.com/users/1');
Copy
const res = await fetch('https://jsonexamples.com/users/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ firstName: 'Ada', lastName: 'Lovelace' })
});
const user = await res.json();
Copy
const res = await fetch('https://jsonexamples.com/sample-json-files/large');
const reader = res.body.getReader();
let bytes = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
bytes += value.byteLength;
}
console.log('downloaded', bytes, 'bytes');
Copy
try {
const res = await fetch(url);
if (!res.ok) throw new Error('HTTP ' + res.status);
return await res.json();
} catch (err) {
console.error('request failed', err.message);
throw 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
AbortSignal for timeouts
{
"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/node