Build Your First RESTful API with Node.js, Express, MongoDB, and TypeScript
A step-by-step tutorial to create a Todo Task Manager API—complete with detailed examples and GitHub repository
Introduction
Have you ever wondered if you could build a RESTful API?
Have you heard about Node.js and thought about trying it but didn’t know where to start?
With this article, I hope to illuminate these common questions we encounter when exploring new technologies.
We'll build a simple and foundational Node.js RESTful API to manage to-do tasks.
I’ve provided the source code on GitHub so you can follow along and experiment on your own:
https://github.com/daniosoriov/todo-manager
But first, let’s clarify some key concepts:
RESTful API
A RESTful API (Application Programming Interface) is an architectural style for designing APIs using Representational State Transfer (REST) principles.
A RESTful API uses standard HTTP methods (like GET, POST, PUT, DELETE) and clearly defined URLs to manage and interact with data resources.
An API interface enables different software systems to interact and exchange data.
What are the REST principles?
Six core principles guide REST architecture:
Uniform Interface: Provides a consistent, standardized way for clients and servers to communicate.
Client-Server: Separates frontend (client) from backend (server) responsibilities.
Stateless: Each request from the client includes all necessary information for processing, avoiding dependency on server-side sessions.
Cacheable: Allows responses to be cached for performance optimization.
Layered System: Structures systems into layers to simplify complexity and enable scalability.
Code on Demand (optional): Servers may send clients executable code (scripts).
If you want more details about a RESTful API, I recommend you check these resources: https://restfulapi.net/ and https://www.ibm.com/think/topics/rest-apis.
Why are RESTful APIs important?
APIs help decouple software applications, allowing different systems to interact seamlessly and independently, regardless of the technologies they use.
This separation of concerns improves scalability, maintainability, and flexibility.
You can build robust, flexible, and future-proof applications with RESTful APIs.
The Tech Stack Used
I think it is worth stating the tech stack used for this API so you have a better idea of what to expect.
Node.js: A powerful JavaScript runtime environment that's event-driven, asynchronous, non-blocking, and single-threaded, making it ideal for building backend APIs.
Express.js (v5): A lightweight and flexible Node.js web framework that simplifies API development by providing easy-to-use middleware, routing mechanisms, and additional utilities.
MongoDB (v6): A document-oriented NoSQL database that is easy to set up and use in your projects. It is known for its low latency and easy-to-use design, which makes it the perfect option for a boilerplate API such as this.
TypeScript (v5): A superset of JavaScript that adds static typing, making code more robust, maintainable, and easier to debug.
This guide will walk you through setting up your first RESTful API using these technologies and frameworks.
Prerequisites
To follow along with this guide, I do expect you to know a few things:
To have some basic knowledge of JavaScript/TypeScript.
You need to have npm and Node.js already installed on your local machine.
A MongoDB database should be set up for this example.
I hope my explanations and code examples are simple enough to tag along for the rest.
Project Setup
You can download the code from my GitHub repository:
https://github.com/daniosoriov/todo-manager
You can then move it to your preferred location on your computer.
Then, we need to install the dependencies for our project.
You will find the dependencies in the package.json
file:
"dependencies": {
"dotenv": "^16.4.7",
"express": "^5.1.0",
"express-validator": "^7.2.1",
"helmet": "^8.1.0",
"mongodb": "^6.15.0",
"mongoose": "^8.13.2"
},
"devDependencies": {
"@tsconfig/node22": "^22.0.1",
"@types/express": "^5.0.1",
"@types/node": "^22.14.0",
"@types/supertest": "^6.0.3",
"supertest": "^7.1.0",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.8.3",
"vitest": "^3.1.1"
}
To install the dependencies, navigate to your project's directory in the console and type:
npm install
This command reads the package.json
and installs all required dependencies.
Folder Structure
The folder structure is quite simple, but let’s go through it so you can better understand what it means.
Keep reading with a 7-day free trial
Subscribe to Daniel’s Substack to keep reading this post and get 7 days of free access to the full post archives.