Skip to content
AgileLogics
5 min read

Working with APIs and Fetch

Prerequisites: Promises

fetch() is the browser's (and modern Node's) built-in way to call a web API. It returns a Promise — see the next lesson if that's new — which resolves once the network request completes.

const response = await fetch("https://api.github.com/users/octocat");
console.log(response.status);
const data = await response.json();
console.log(data.login);

Expected output

200
octocat

.status tells you whether the request succeeded (200) or failed (404 not found, 500 server error); .json() parses a JSON response body into a JavaScript object.

Common mistakes

  • Not checking response.ok/status before assuming .json() will contain useful data — a failed request can return an error page instead of the expected JSON.