# Fetch API

> The way code in a web page asks a server for data.

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** fetch(), JavaScript Fetch, window.fetch

## Think of it like

Think of it like sending a friend to the shop. You keep playing, and they come back with the bag.

## Definition

The Fetch API is the browser's built-in way to ask a server for data from JavaScript. You call fetch with a web address. It sends the request and hands back a promise. The page keeps working while it waits. When the answer comes, you read it, often as JSON. Node.js has fetch built in too.

## Example

Lucy's weather page calls fetch("/api/weather"). Soon it shows 18°C, and the page does not reload.

## Example code

```javascript
const res = await fetch("/api/weather");
if (!res.ok) throw new Error(res.status);
const data = await res.json();
```

Ask for the data, check it worked, then read the JSON.

## Why it matters

Most modern pages load data this way after they open. It is why a feed can update without a full reload.

## Common mistake

Thinking fetch fails on a 404 or 500. It does not. You have to check res.ok yourself.

## Three principles

- **Built in.** No library is needed in the browser.
- **Works with promises.** The page keeps running while it waits.
- **Check the status.** An error page still counts as an answer.

## Related terms

- [Promise (JavaScript)](https://learn.benibix.com/term/development/promise)
- [Async/Await](https://learn.benibix.com/term/development/async-await)
- [JavaScript Object Notation (JSON)](https://learn.benibix.com/term/development/json)
- [REST API](https://learn.benibix.com/term/development/rest-api)
- [HTTP Client](https://learn.benibix.com/term/development/http-client)
- [JavaScript](https://learn.benibix.com/term/development/javascript)
- [Axios](https://learn.benibix.com/term/development/axios)

## Taught in

- [Fetch API and JSON](https://learn.benibix.com/learn/development-web/fetch-api-and-json): HTML, CSS and JS, 13/14

![A web page in the browser runs fetch("/api/weather"). An arrow marked request goes to a server, which holds { "temp": 18 }. An arrow marked response, JSON, comes back, and the page shows 18°C in London. A note says the page still scrolls and clicks still work while it waits. Under it: a 404 or 500 still comes back as a response, so check response.ok.](https://learn.benibix.com/diagrams/dev-fetch.svg)

---

Source: https://learn.benibix.com/term/development/fetch-api
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
