Skip to content

Fetch API

In one line

The Fetch API is a modern JavaScript interface for making HTTP requests to retrieve or send data from servers.

In simple words

The Fetch API is the modern way to make HTTP requests in JavaScript. It replaces the older XMLHttpRequest (XHR) with a cleaner, Promise-based interface. You use fetch() to request data from a URL, and it returns a Promise that resolves to the Response object.

A typical fetch call looks like: const res = await fetch(url); const data = await res.json();. The first await gets the response headers, and the second reads and parses the body. You can configure the method (GET, POST, etc.), headers, and body via a second argument.

Fetch only rejects on network errors — HTTP error status codes (404, 500) still resolve. You must check response.ok or response.status manually to handle HTTP errors.

Example

javascript
async function getUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error("User not found");
  return await res.json();
}

A fetch call that checks response.ok and throws on HTTP errors, then parses the JSON body.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary