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.

๐Ÿ›’
UrbanCart Example
How the product page loads

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.

How APIs Work
Browser
UrbanCart frontend
โ†’
HTTP Request
GET /products
โ†’
API Server
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.

fetch-basics.js The Two-Step Pattern
1๏ธโƒฃ

await fetch(url)

Sends the request. Returns a Response object with status, headers, and a body stream.

2๏ธโƒฃ

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.

get-products.js GET Request โ€” Fetch Products
Response properties: 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.

post-product.js POST Request โ€” Add Product
Three required parts for POST: (1) 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.

crud.js PUT & DELETE Requests
CRUD = the four operations: Create (POST) ยท Read (GET) ยท Update (PUT) ยท Delete (DELETE). Every REST API uses these four HTTP methods.

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.

error-handling.js Robust Fetch Pattern
Key insight: 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.

loading.js Loading State Pattern
Three states to handle: (1) Loading โ€” spinner or text. (2) Success โ€” render data. (3) Error โ€” friendly message. Every fetch-based UI needs all three.

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.js JSON Conversion
๐Ÿ“ค

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: *.

Quick fixes during development: (1) Use APIs that allow CORS (like dummyjson.com). (2) Use a proxy in your development server. (3) When you build the backend (later in this track), you'll set CORS headers yourself.

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 โ€” Fetch Products from API
Output

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.

๐Ÿ› ๏ธ Try It Yourself โ€” UrbanCart Product Listing
Output

Test your knowledge

Question 1 of 5
What are the two steps to get JSON data from a fetch request?
Question 2 of 5
When does fetch() reject its Promise (throw an error)?
Question 3 of 5
Which three things must you include in a POST request with JSON data?
Question 4 of 5
What does JSON.stringify() do?
Question 5 of 5
What is CORS and why might you get a CORS error?
0
/5
Great job!

Up Next

Lesson 13: Introduction to React โ†’
You've completed JavaScript Fundamentals! ๐ŸŽ‰ Next up: React โ€” the most popular UI framework in the world.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’