How APIs Really Work: A Beginner’s Guide to Building and Understanding Backend Endpoints
Source: Dev.to
What Exactly Is an API?
API stands for Application Programming Interface. It acts as a bridge that allows two systems to communicate.
Think of a waiter at a restaurant: you (the user) place an order, the waiter (the API) takes that order to the kitchen (the server). Once the kitchen prepares the food, the waiter brings it back to you.
In technical terms, the frontend (e.g., React, Vue, HTML) makes a request, the API carries that request to the backend to process the logic, and then the API returns a response. Almost all APIs communicate using JSON, a lightweight data format.
How API Requests Actually Work
When you click “Login” on Instagram, Snapchat, or any app, your browser compiles the data (usually as JSON) and sends it across the internet.
- DNS lookup – The Domain Name System translates the website’s name into an IP address so the request can locate the correct server.
- Middleware layers – Security checkpoints handle logging, authentication, and other preprocessing before the request reaches the backend controller.
- Controller – The brain of the operation; it processes business logic and runs queries against the database.
- Response – The backend gathers results into a structured JSON response, sends it back across the network, and the frontend unpacks the information to update the UI (e.g., log you in or show an error).
All of this round‑trip typically happens in a few milliseconds, making the interaction feel instantaneous.
What is a REST API
REST (Representational State Transfer) is the standard architecture used by most modern APIs. REST operates using endpoints such as:
GET /api/users
POST /api/login
GET /api/products/:id
DELETE /api/posts/12
HTTP Methods
- GET – Fetch data
- POST – Send data
- PUT – Update data
- DELETE – Remove data
Common Status Codes
200– OK201– Created400– Bad Request404– Not Found500– Server Error
What is an Endpoint
An endpoint is simply a URL + HTTP method that performs an action.
Examples
| Action | Example Endpoint |
|---|---|
| Fetch all users | GET /api/users |
| Get a product | GET /api/products/:id |
| Create account | POST /api/register |
| Login | POST /api/login |
Types of Endpoint Inputs
Route params: /users/:id
Query params: /search?keyword=api
Body data: { "email": "test@gmail.com" }
Building Your First API Endpoint
Setup
npm init -y
npm install express
Create a file named server.js and add the following code:
// server.js
const express = require('express');
const app = express();
const PORT = 3000;
// Simple GET Endpoint
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello, world!' });
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
How it works
app.get()– Listens for GET requests/api/hello– Endpoint URLreq– Incoming request (data coming in)res– Server response (data going out)
POST Endpoint (Sending Data)
app.use(express.json()); // To read JSON body
app.post('/api/register', (req, res) => {
const { name, email } = req.body;
res.status(201).json({
message: 'User created successfully',
data: { name, email }
});
});
What Are Middlewares? (Beginner‑Friendly)
Middleware = functions that run before your main logic.
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
Common uses
- Logging
- Authentication
- Data validation
- Handling CORS
Middleware powers a large portion of a backend system.
How APIs Talk to Databases
Endpoint → Controller → Database → Response
Simplified example
app.get('/api/user/:id', async (req, res) => {
try {
const user = await User.findById(req.params.id);
res.json(user);
} catch (error) {
res.status(500).json({ message: 'Server error' });
}
});
Even if you’re not using a database yet, this pattern appears in every backend.
Testing APIs with Postman or Thunder Client
Example tests
GET request
- Select GET
- Enter
http://localhost:3000/api/hello - Click Send
POST request
- Select POST
- Enter the endpoint (e.g.,
http://localhost:3000/api/register) - Go to Body → JSON and add data:
{
"name": "Kelvin",
"email": "kelvin@example.com"
}
Testing tools let you verify APIs without building a frontend.
To Round This Up
APIs are the invisible glue that connects frontends, databases, and third‑party services. By mastering the core lifecycle—understanding how requests trigger logic at endpoints, how middleware secures the pipeline, and how structured responses close the loop—you move from writing isolated functions to architecting robust, scalable systems. This foundational knowledge transforms the backend from a “black box” into a logical, manageable workflow where data flows predictably.