Simple API Tutorial: Build and Call Your First REST Endpoint in 5 Minutes
Learn how to build and call a simple API in under 5 minutes using Node.js or browser fetch(). Beginner-friendly guide with complete runnable code.
Building a simple api does not require complicated server configurations, cloud infrastructure, or enterprise frameworks. With just a few lines of JavaScript, you can spin up an HTTP server that listens for incoming client requests and responds with formatted JSON.
Building a Simple API in Node.js (Zero Dependencies)
You don't even need external packages like Express to create a simple api. Node.js has a built-in node:http module:
// server.js - A minimal, production-valid Simple API
import http from 'node:http';
const server = http.createServer((req, res) => {
// Set CORS and JSON response headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
if (req.method === 'GET' && req.url === '/api/quote') {
const quotes = [
{ text: "Simplicity is the soul of efficiency.", author: "Austin Freeman" },
{ text: "Make it work, make it right, make it fast.", author: "Kent Beck" }
];
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
res.writeHead(200);
return res.end(JSON.stringify({ status: 'ok', data: randomQuote }));
}
// Fallback 404 handler
res.writeHead(404);
res.end(JSON.stringify({ error: 'Endpoint not found' }));
});
server.listen(3000, () => {
console.log('Simple API running on http://localhost:3000/api/quote');
});
Calling Your Simple API with Fetch
Once running, any browser or frontend app can consume your simple api with native fetch():
const response = await fetch('http://localhost:3000/api/quote');
const json = await response.json();
console.log(json.data.text); // "Simplicity is the soul of efficiency."
That is all it takes! A simple api boils down to receiving a URL route, checking the HTTP method, and writing back a response stream.