If you’ve spent any time looking into web development or programming, you’ve probably heard the term API thrown around. People will say things like, “Just connect to their API,” or “We need to build a RESTful API for the frontend.”
But what exactly is an API? And more specifically, what is a REST API?
In this guide, we’re going to break down these concepts without the heavy jargon. By the end, you’ll understand exactly how the modern web communicates, and you’ll even know how to interact with real-world APIs yourself.
What is an API?
API stands for Application Programming Interface.
Simply put, an API is a messenger that takes your request, tells a system what you want to do, and then returns the response back to you.
The Restaurant Analogy
Imagine you are sitting at a table in a restaurant with a menu of choices to order from. The kitchen is the part of the “system” that will prepare your order.
What is missing is the critical link to communicate your order to the kitchen and deliver your food back to your table. That’s where the waiter comes in.
The waiter is the API.
- You (The Client): You tell the waiter what you want (e.g., “I’d like a burger”).
- The Waiter (The API): Takes your request, runs to the kitchen, and tells the chefs exactly what to make.
- The Kitchen (The Server/Database): Cooks the burger.
- The Waiter (The API): Brings the finished burger back to your table.
In the digital world, if an app on your phone needs to fetch today’s weather, it sends a request to a Weather API. The API grabs the data from the weather station’s servers and delivers it back to your phone.
What makes an API “RESTful”?
REST stands for Representational State Transfer. It’s not a tool or a library; it’s a set of rules (an architectural style) for how APIs should be built.
When an API follows these rules, we call it a REST API (or RESTful API).
Here are the core concepts of REST:
1. Everything is a “Resource”
In REST, data is treated as resources. A resource can be anything: a user, a blog post, a tweet, or a weather report. Every resource is identified by a specific URL (called an Endpoint).
For example, if you want to access a list of users, the endpoint might look like this:
https://api.example.com/users
2. Statelessness
“Stateless” means that the server doesn’t remember anything about you between requests. Every single time you ask the waiter (the API) for something, you have to provide all the information they need to fulfill the request. The server treats every request as brand new.
The Four Main HTTP Methods (CRUD)
When you interact with a REST API, you do so using HTTP methods. These methods correspond to basic database operations, often referred to as CRUD (Create, Read, Update, Delete).
1. GET (Read)
Used to retrieve data. It’s like asking the waiter to bring you the menu.
- Example:
GET /users(Fetches a list of all users) - Example:
GET /users/123(Fetches data specifically for the user with ID 123)
2. POST (Create)
Used to send new data to the server. It’s like giving the waiter your order.
- Example:
POST /users(Creates a brand new user using the data you attached)
3. PUT / PATCH (Update)
Used to modify existing data.
- PUT replaces the entire resource. (e.g., Overwriting your entire user profile).
- PATCH updates only a specific part of a resource. (e.g., Changing just your email address).
4. DELETE (Delete)
Used to remove data.
- Example:
DELETE /users/123(Deletes the user with ID 123).
How APIs Send Data: JSON
When the waiter brings your food, it usually comes on a plate. When an API brings your data, it usually comes packaged in JSON (JavaScript Object Notation).
JSON is incredibly popular because it is very easy for humans to read and for computers to parse. Here is what a JSON response from a Weather API might look like:
{
"city": "London",
"temperature": 22,
"condition": "Cloudy",
"humidity": 65
}
It’s just a simple list of “Keys” (like city) paired with “Values” (like "London").
Real-World API Examples
Let’s look at how famous companies use REST APIs.
1. The Twitter (X) API
If you want to build a bot that automatically tweets, you don’t need to physically open the Twitter app and click buttons. Instead, your code talks to the Twitter REST API:
- To get a user’s tweets:
GET https://api.twitter.com/2/users/123/tweets - To post a new tweet:
POST https://api.twitter.com/2/tweets(with your tweet text in JSON format)
2. The Spotify API
Whenever a music app shows you the lyrics or album art for a song, it’s making a request to the Spotify API:
- To search for a track:
GET https://api.spotify.com/v1/search?q=coldplay - To create a new playlist:
POST https://api.spotify.com/v1/users/suresh/playlists
Understanding HTTP Status Codes
When the API returns your request, it always includes a Status Code—a three-digit number that tells you how things went.
- 200 OK: Success! The waiter brought your food.
- 201 Created: Success! You successfully created a new resource (like registering a new account).
- 400 Bad Request: You made a mistake. (e.g., You tried to order something that isn’t on the menu).
- 401 Unauthorized: You forgot your wallet. You need to log in or provide an API key to access this data.
- 404 Not Found: The resource doesn’t exist. You tried to look for a user that has been deleted.
- 500 Internal Server Error: The kitchen caught on fire. Something went wrong on the server’s end, and it’s not your fault.
Conclusion
APIs are the invisible glue that holds the internet together. They allow your phone to talk to your bank, your smart speaker to play Spotify, and your weather widget to know if it’s going to rain.
To quickly recap:
- An API is a messenger between a client and a server.
- A REST API is an API built using strict URL structures and standard HTTP methods.
- You use GET, POST, PUT, and DELETE to interact with data.
- Data is usually sent back and forth using JSON.
Now that you understand the basics, you’re ready to start exploring public APIs and even building your own!
Discussion
Loading comments...