1. What is an API
An API (Application Programming Interface) is how apps talk to servers. When you open the UrbanCart app, the frontend doesn't have all the product data baked in โ it asks the API for products, and the API sends back the data as JSON.
When a customer visits UrbanCart's product page, the browser sends a request to the API: "Give me the list of products." The API responds with product data โ names, prices, images, ratings. The frontend then takes that JSON data and renders it as the cards you see on screen.
UrbanCart frontend
GET /products
Returns JSON data
2. fetch()
The fetch() function is built into every modern browser. It sends an HTTP request to a URL and returns a Promise. There are always two steps: first get the response, then parse the body.
await fetch(url)
Sends the request. Returns a Response object with status, headers, and a body stream.
await response.json()
Reads the full body and parses it as JSON. Returns a JavaScript object you can use.
3. GET requests
GET is the default HTTP method. When you call fetch(url) without any options, it sends a GET request. This is how UrbanCart loads its product list from the API.
response.status gives you the HTTP status code (200, 404, 500). response.ok is true if the status is 200-299. response.headers lets you read response headers like Content-Type.
4. POST requests
POST sends data to the server โ like when a customer adds a new product to UrbanCart's inventory. You pass a second argument to fetch() with the method, headers, and body.
method: 'POST' (2) headers: { 'Content-Type': 'application/json' } (3) body: JSON.stringify(data).
5. PUT & DELETE
Together with GET and POST, PUT (update) and DELETE (remove) complete the four CRUD operations. UrbanCart uses all four to manage products.
6. Error handling
Two types of errors can happen when fetching: network errors (server is down, no internet) and HTTP errors (404 Not Found, 500 Server Error). You need to handle both.
fetch() only rejects its Promise on network failures (no internet, DNS error). A 404 or 500 response is still a "successful" fetch โ you must check response.ok yourself. This surprises many beginners.
7. Loading states
Good UX means showing feedback while data is being fetched. The pattern: show "Loading...", fetch the data, then replace the loading indicator with the actual content.
8. JSON.parse & JSON.stringify
JSON (JavaScript Object Notation) is the standard format for API data. Two built-in methods convert between JS objects and JSON strings.
JSON.stringify(obj)
Object to string. Used when sending data in fetch() body. Required for POST/PUT requests.
JSON.parse(str)
String to object. Used when receiving data. response.json() does this automatically.
9. CORS intro
CORS (Cross-Origin Resource Sharing) is a browser security mechanism. When your UrbanCart frontend at urbancart.com fetches from api.example.com, the browser checks if that API allows cross-origin requests.
If you see Access to fetch has been blocked by CORS policy, it means the API server hasn't allowed your domain. This is a server-side setting โ you can't fix it from the frontend alone. The server must set headers like Access-Control-Allow-Origin: *.
Try It Yourself #1
Fetch products from the dummyjson.com API and log the results. Experiment with the URL โ try changing limit or fetching a single product by ID.
Try It Yourself #2
Build an UrbanCart product listing: fetch products from the API and render them as styled cards in the DOM. This combines everything โ fetch, error handling, loading states, and DOM manipulation.
Test your knowledge
fetch() reject its Promise (throw an error)?JSON.stringify() do?You just learned to talk to APIs.
Now build a full-stack career.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ