Dev Dissection — Week 1: Building Your First API with Node.js & TypeScript
Welcome to the first session of Dev Dissection, a weekly community lesson for developers. This week, we’ll dive into the basics of APIs and get hands-on by building a simple TODO API using Node.js, Express, and TypeScript.
Prerequisites
Before we begin, make sure you have the following set up on your machine:
- Node.js (v20 or above recommended)
- TypeScript installed globally
npm install -g typescript
- A code editor (VSCode recommended)
- Basic familiarity with command line and JavaScript
What is an API? (The Waiter Analogy)
Imagine you’re at a restaurant. You (the user) want to order food (data). The kitchen (database/backend) has the food but you can’t talk to it directly. So you speak to the waiter — that’s the API.
- You place your request: “I’d like a pizza.”
- The waiter takes it to the kitchen.
- The kitchen prepares the food and gives it to the waiter.
- The waiter responds back to you with your pizza.
In tech terms:
- Your browser/mobile app makes a request to an API
- The API fetches or modifies data
- It sends back a response
APIs allow apps, servers, and services to talk to each other in a structured, predictable way.
What is TypeScript?
TypeScript is a superset of JavaScript that adds static typing to your code. This means you can define the shape of your data (like what a TODO looks like), and catch bugs early — before running your code.
Here’s why we use TypeScript:
- It helps avoid silly bugs by letting your editor warn you about errors.
- It makes your code more readable and self-documenting.
- It improves the developer experience with better auto-completion and tooling support.
If you already know JavaScript, learning TypeScript is easy — and totally worth it.
Learn more at the official site: typescriptlang.org
Getting Started with Node.js + TypeScript
Node.js is a runtime environment that lets you run JavaScript/TypeScript outside the browser. It’s perfect for building APIs.
Let’s scaffold our project:
mkdir todo-api
cd todo-api
npm init -y
npm install express cors
npm install -D typescript ts-node-dev @types/express @types/node
Add a tsconfig.json:
npx tsc --init
Update it for simplicity:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"esModuleInterop": true
}
}
Set up your folder:
mkdir src
Inside package.json, add a dev script:
"scripts": {
"dev": "ts-node-dev --respawn src/index.ts"
}
Build Your First CRUD API with Express
What is CRUD?
- Create — add a new TODO
- Read — fetch existing TODOs
- Update — modify a TODO
- Delete — remove a TODO
Let’s Build It
Create a file src/index.ts:
import express from 'express';
import cors from 'cors';
const app = express();
const PORT = 3000;
app.use(cors());
app.use(express.json());
interface Todo {
id: number;
task: string;
completed: boolean;
}
let todos: Todo[] = [];
// Create
app.post('/todos', (req, res) => {
const { task } = req.body;
const newTodo: Todo = {
id: Date.now(),
task,
completed: false,
};
todos.push(newTodo);
res.status(201).json(newTodo);
});
// Read
app.get('/todos', (_req, res) => {
res.json(todos);
});
// Update
app.put('/todos/:id', (req, res) => {
const { id } = req.params;
const { task, completed } = req.body;
const todo = todos.find(t => t.id === Number(id));
if (!todo) {
res.status(404).json({ error: 'Not found' });
return;
}
if (task !== undefined) todo.task = task;
if (completed !== undefined) todo.completed = completed;
res.json(todo);
});
// Delete
app.delete('/todos/:id', (req, res) => {
todos = todos.filter(t => t.id !== Number(req.params.id));
res.status(204).send();
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Run your API locally:
npm run dev
Test endpoints with Postman or Hoppscotch
Conclusion
In just 30 minutes, you:
- Understood what an API is
- Set up a Node.js + TypeScript project
- Built a complete CRUD API using Express
This simple TODO API will become the foundation for the next few weeks where we’ll:
- Connect it to MongoDB
- Build a React frontend
- Add authentication
- Deploy it to the cloud
Coming up next: Connect your API to MongoDB and learn the difference between SQL and NoSQL.