API Building for Beginners: My Simple MERN Stack Story

When I first heard the term API, I thought it sounded super serious — like something only big tech companies should worry about.
But as I started building my first real MERN app, I realized an API is just a friendly way for your frontend and backend to talk to each other.
And building one? Way easier (and way cooler) than it sounds.
Here’s the story of how I built my first API — with no fancy magic, just clear steps.


First Things First: What Even Is an API?

Before touching the keyboard, I needed to really get what I was building.
Here’s how it clicked for me:

Think of your app as a restaurant.
The frontend (React) is the waiter taking orders.
The backend (Node/Express) is the kitchen making the food.
The API is the menu — it tells the waiter what can be ordered (aka requested).

Suddenly, APIs felt simple and... even kinda fun!


Setting Up the Backend: Starting Small

I already had a MERN stack project ready (MongoDB, Express, React, Node).
Here’s what I did next:

1. Made a Basic Express Server


// server.js
import express from 'express';

const app = express();
const port = 5000;

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

The server was up and running!


2. Built My First API Route

This was my first real “backend moment” — a basic route that sends data back.

app.get('/api/message', (req, res) => {
  res.json({ message: 'Hello from the backend!' });
});

When I visited http://localhost:5000/api/message,
I saw:

{ "message": "Hello from the backend!" }

🎉 My first working API!


Connecting Frontend and Backend

Time for my React frontend to talk to the backend.

In my React app:

import { useEffect, useState } from 'react';

function App() {
  const [message, setMessage] = useState('');

  useEffect(() => {
    fetch('/api/message')
      .then(res => res.json())
      .then(data => setMessage(data.message));
  }, []);

  return (
    <div>
      <h1>{message}</h1>
    </div>
  );
}

export default App;

Boom — the backend’s message showed up on my frontend! 🚀
Honestly, it felt like some secret hacker magic at first.


Common Mistakes I Made (And How You Can Skip Them)

  • Forgetting to parse JSON data
    Add this line in your server setup:

      app.use(express.json());
    
  • CORS issues
    If your React app runs separately (like on port 3000), install and use CORS:

      npm install cors
    

    Then:

      javascriptCopyEditimport cors from 'cors';
      app.use(cors());
    
  • Messy routes
    Don't jam all routes into one file once your app grows — it'll drive you crazy later.


Pro Tip: Organising Routes

Later, I learned it’s better to separate routes into different files.
Example:

routes/messageRoutes.js

javascriptCopyEditimport express from 'express';

const router = express.Router();

router.get('/', (req, res) => {
  res.json({ message: 'Hello from the backend!' });
});

export default router;

Then in your main server file:

server.js

javascriptCopyEditimport express from 'express';
import cors from 'cors';
import messageRoutes from './routes/messageRoutes.js';

const app = express();
const port = 5000;

app.use(cors());
app.use(express.json());

// All message APIs
app.use('/api/message', messageRoutes);

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Way cleaner.
Way easier to maintain.


Final Thoughts

Building my first API wasn’t about memorizing fancy concepts.
It was about understanding one simple thing:

👉 Frontend asks. Backend answers.

Once you get that, everything else (routes, APIs, requests, responses) starts making sense.

If you're just starting out — don’t overthink it.

  • Create one tiny API.

  • Fetch it from React.

  • Celebrate the small win.

Because every big app starts with one simple API call. 🚀

You’re closer than you think!


What Next?

If you found this helpful, why not try building a tiny CRUD (Create, Read, Update, Delete) API next?
It's the natural next step — and trust me, it's way more fun when you already have this basic flow nailed!

And hey — if you get stuck or have any questions, drop them in the comments!
Let’s build and learn together. 💬🚀

10
Subscribe to my newsletter

Read articles from Aryan Shrivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aryan Shrivastava
Aryan Shrivastava