Glossary · Web Development

Fetch API

The Fetch API is the modern browser interface for making HTTP requests from JavaScript, returning promises instead of using older callback-based XMLHttpRequest.

Updated October 6, 2026

The Fetch API is a built-in JavaScript interface, exposed as the fetch() function, for sending HTTP requests and reading responses. It returns a promise that resolves to a Response object.

Why it matters

Fetch is how most front ends talk to APIs. Its main surprise is error handling: the promise only rejects on network failure, not on HTTP errors. A 404 or 500 response still resolves, so you must check response.ok. Cross-origin requests also depend on CORS headers from the server.

Example

const res = await fetch('/api/status', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ site: 'example.com' })
});
if (!res.ok) throw new Error('HTTP ' + res.status);
const data = await res.json();

Common mistakes

  • Assuming a failed status code throws an exception.
  • Forgetting credentials: 'include' when cookies must be sent cross-origin.
  • Not setting a timeout; use an AbortController to cancel slow requests.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.