React Introduction to APIs
Table of Contents + −
In the last lesson you learned about React Hook Best Practices, so you’re comfortable with state and effects. Now let’s meet APIs, which are how your React app asks a server for real, live data instead of the hardcoded lists you’ve used so far.
🤔 Why do we need APIs?
Here’s the pain. Your app runs in the browser, but the real data lives far away on a server, so the two need a way to talk.
- Your app can’t reach into a database directly. That would be unsafe and messy.
- The data changes all the time, like new posts or live prices, so you can’t just hardcode it.
- Many different apps, a website and a phone app, need the same data from the same place.
- You need a clear, agreed way to ask for data and get an answer back.
So an API is that agreed way. It’s the doorway the server opens so apps can ask for data and send data, using a set of rules both sides understand.
🐍 What is an API?
API stands for Application Programming Interface. That sounds heavy, so let’s make it plain.
- An API is a set of rules that lets one program ask another program for something.
- For web apps, it usually means: your app sends a request to a web address, and the server sends back data.
- You don’t see how the server works inside. You just know what to ask for and what you’ll get back.
Think of it like ordering food by phone. You don’t go into the kitchen, you just call, say what you want, and the food comes out. The phone line and the menu are the “interface”, so you only need to know how to order, not how the kitchen runs.
You already use APIs constantly
Every app that shows live data is calling an API: a weather app, a chat app, a maps app. Your React app will do the same thing, just from JavaScript code instead of by hand.
🔁 How a request and response work
Every API call is the same simple back-and-forth. Your app asks, the server answers. Two steps, every time.
- Your app sends a request to a web address, called an endpoint, like
https://api.example.com/users. - The server does its work, maybe looks something up in its database.
- The server sends back a response, usually some data plus a status code.
- Your app reads that data and shows it on the screen.
Here’s the flow as a picture.
So the whole job of API code in React is just this: send the request, wait for the response, then put the data into state so it shows up.
📦 The data comes back as JSON
When the server answers, it needs a format both sides understand. On the web that format is almost always JSON.
- JSON stands for JavaScript Object Notation. It’s a simple text format for data.
- It looks almost exactly like a JavaScript object, with keys and values.
- It’s easy for your code to turn into a real JavaScript object you can use.
Here’s what a JSON response with a couple of users might look like.
[ { "id": 1, "name": "Alex", "email": "alex@example.com" }, { "id": 2, "name": "Riya", "email": "riya@example.com" }]Read what’s there.
- It’s an array of two objects, one per user.
- Each object has the same keys:
id,name, andemail. - Once your app receives this, you can put it in state and map over it, exactly like the arrays you rendered before.
So JSON is the bridge. The server speaks it, your JavaScript understands it, and you turn it into a list on the screen.
🌍 What you can do with an API
APIs aren’t only for reading data. There’s a small set of common actions, and each has a name you’ll see everywhere.
The common request types
GET - read data (get the list of users)POST - create data (add a new user)PUT - update data (change a user)DELETE - remove data (delete a user)So the four words map to the four things you do with data.
GETis by far the most common, and it’s where you’ll start: just reading data to show it.POSTsends new data to the server, like submitting a form.PUT(orPATCH) changes something that already exists.DELETEremoves something.
For now we’ll focus on GET, because showing live data is the first thing almost every app needs.
🔢 Status codes tell you what happened
Along with the data, the server sends a status code, a small number that says how the request went. You’ll meet a few again and again.
200means OK, it worked, here’s your data.404means not found, that address has nothing.500means the server hit an error on its side.- Codes in the
200range are success,400range are your request’s problem,500range are the server’s problem.
So before trusting the data, good code checks the status. A 404 or 500 means there’s no real data to show, and you should display an error instead.
A response can fail
Getting a response back does not always mean success. The server might answer with a 404 or 500. Always check the status before using the data, which you’ll learn to do when we handle errors.
✅ Best Practices
A few ideas to keep in mind before you write your first request.
- Start with
GET. Reading and showing data covers most of what apps need. - Expect JSON. Plan to turn the response into a JavaScript object and store it in state.
- Always think about three states: loading, success, and error. A request takes time and can fail.
- Use public practice APIs while learning, like JSONPlaceholder, so you can focus on the React side.
Great APIs to practice with
Free, no-signup APIs are perfect for learning: JSONPlaceholder for fake users and posts, and the PokéAPI for fun sample data. You can call them right away without any keys or setup.
🧩 What You’ve Learned
- ✅ An API is an agreed set of rules that lets your app ask a server for data and send data
- ✅ Every call is a request from your app and a response from the server
- ✅ An endpoint is the web address you send the request to
- ✅ Data comes back as JSON, a text format that maps neatly to JavaScript objects
- ✅ Common actions are
GET(read),POST(create),PUT(update), andDELETE(remove) - ✅ Status codes like
200,404, and500tell you whether the request succeeded
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is an API in the context of a web app?
Why: An API is the doorway a server opens so apps can request and send data using rules both sides understand. Your app asks, the server answers.
- 2
What format does web API data usually come back in?
Why: Web APIs almost always respond with JSON. It looks like a JavaScript object and is easy to turn into one your code can use.
- 3
Which request type is used just to read data?
Why: GET reads data, like fetching a list of users. POST creates, PUT updates, and DELETE removes. GET is where most apps start.
- 4
What does a 404 status code mean?
Why: 404 means not found, the address has nothing. 200 is success, and 500 means the server itself hit an error. Always check the status before using the data.
🚀 What’s Next?
Now you understand what an API is and how the request-response flow works. Time to actually do it. Next you’ll write your first real request in React using the browser’s built-in fetch function.