Intermediate · Async JavaScript, modules and tools · topic 41 of 64 · 35 min
fetch and APIs
Explanation
What is an API?
API means Application Programming Interface: a fixed set of requests one program can make to another. On the web, an API is often a URL (an endpoint) that sends back data instead of a page. Most APIs answer in JSON, the text format for objects and arrays you met with localStorage.
{
"from": "NDLS",
"to": "BCT",
"trains": [
{ "number": "12952", "name": "Rajdhani Express", "departs": "16:55" },
{ "number": "12954", "name": "August Kranti Rajdhani", "departs": "17:15" }
]
}GET data with fetch
fetch('/api/trains?from=NDLS&to=BCT')
.then((response) => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json(); // reads the body and parses JSON (also a promise)
})
.then((data) => console.log(data.trains.length, 'trains'))
.catch((error) => console.error('Could not load trains:', error.message));| Status | Meaning | What your page should do |
|---|---|---|
| 200 OK | Success, data is in the body | Show the data |
| 201 Created | A new item was saved (after POST) | Show "Saved" and the new item |
| 400 Bad Request | The data you sent is wrong | Show which field to fix |
| 401 Unauthorized | Not logged in, or the login expired | Send the user to log in |
| 404 Not Found | No such item or URL | Show "Not found" |
| 429 Too Many Requests | You asked too often; slow down | Wait and try again later |
| 500 Internal Server Error | The server broke | Show "Something went wrong, try again" |
Query strings with URLSearchParams
A query string is the part after ? in a URL, like ?q=rice&page=2. Do not glue it together by hand: spaces, & and other symbols must be encoded. URLSearchParams does this for you.
const params = new URLSearchParams({ q: 'basmati rice', page: 2 });
console.log(params.toString()); // q=basmati+rice&page=2
const url = `/api/products?${params}`;
// Reading the query string of the current page:
const current = new URLSearchParams(location.search);
console.log(current.get('q'));Sending data with POST
const response = await fetch('/api/orders', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ itemId: 'p7', qty: 2, pincode: '560001' }),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const order = await response.json(); // e.g. { id: 991, status: 'placed' }CORS in simple words
An origin is the protocol + domain + port of a page, like https://myshop.in. By default, the browser does not let your page read responses from a different origin. The other server must say "this origin is allowed" with a header called Access-Control-Allow-Origin. This rule is CORS (Cross-Origin Resource Sharing). A CORS error in the Console is fixed on the server (or by calling through your own server), not in your fetch code.
Loading, empty and error states
- Loading: show a spinner or skeleton, and disable the button.
- Success with data: draw the list.
- Empty: a friendly message such as "No trains found for this date".
- Error: a clear message and a "Try again" button. Do not leave a blank page.
Watch the lesson
fetch and APIs · 1/7
Most apps get their data from APIs. An API is a URL that answers with data, usually JSON.
Prefer a video by a teacher? Find free videos on “javascript fetch api json tutorial for beginners” on YouTube ↗
In real life: A PIN code lookup on a delivery site
When you type a 6-digit PIN code on a delivery site, the page calls an API like /api/pincode?code=560001 with fetch. While it waits, the box shows "Checking...". A 200 response fills in the city and state and says "Delivery in 2 days". A 404 shows "We do not deliver here yet". If the network fails, it shows "Could not check, try again". The secret key for the courier's API stays on the site's own server.
Practice (0/3 done)
Write buildUrl(base, params). It returns base followed by ? and a query string made with URLSearchParams. If params has no keys, return base without the ?. Examples: buildUrl("https://api.example.com/trains", { from: "NDLS", to: "BCT" }) gives "https://api.example.com/trains?from=NDLS&to=BCT", and buildUrl("https://api.example.com/search", { q: "rice & dal" }) gives "https://api.example.com/search?q=rice+%26+dal".
Write async function getJson(fetchFn, url). It calls fetchFn(url) (in a real page fetchFn is fetch; the tests pass a fake one, so there is no network). If response.ok is false, throw new Error("HTTP " + response.status), for example "HTTP 404". Otherwise return the parsed body from response.json().
Build a "user list" page that loads data from a free practice API with no key, such as https://jsonplaceholder.typicode.com/users. Show "Loading..." first, then draw one card per user (name, email, city) with textContent. Add a search box that filters by name. Then break things on purpose: change the URL to a wrong path to see your error state, and make the filter match nothing to see your empty state.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.