A RESTful API starter to perform basic CRUD operations on a database
All available routes for Data
for CRUD operations:
- POST
/api/v1/data
: Create a new data entry. - GET
/api/v1/data
- Retrieve all data entries, optionally filtered by type. - GET
/api/v1/data/:id
- Retrieve a specific data entry by ID. - PATCH
/api/v1/data/:id
- Update a data entry by ID. - DELETE
/api/v1/data/:id
- Delete a data entry by ID.
Create a new data entry on the Database
- Method: POST
- URL:
http://localhost:9000/api/v1/data
- Body:
- JSON data containing
type
,data
, andmetadata
(all fields are required).
- JSON data containing
Example request body:
{
"type": "user",
"data": {
"name": "John Doe",
"email": "[email protected]"
},
"metadata": {
"created_at": "2025-01-27T00:00:00Z"
}
}
- Status Code: 201 (Created)
- Body:
{ "message": "Data Creation: SUCCESS π", "data": { "_id": "60ccf1b2d5c4d051c8d4e20c", "type": "user", "data": { "name": "John Doe", "email": "[email protected]" }, "metadata": { "created_at": "2025-01-27T00:00:00Z" }, "__v": 0 } }
Retrieve all data by
type
, i.e., "?type=user
" from the database
- Method: GET
- URL:
http://localhost:9000/api/v1/data
- Query Parameters (optional):
type
: Filter the data by type (e.g.,type=user
).
Example request URL:
http://localhost:9000/api/v1/data?type=user
- Status Code: 200 (OK)
- Body:
{ "message": "Data Retrieval By Type: SUCCESS π", "data": [ { "_id": "60ccf1b2d5c4d051c8d4e20c", "type": "user", "data": { "name": "John Doe", "email": "[email protected]" }, "metadata": { "created_at": "2025-01-27T00:00:00Z" }, "__v": 0 } ] }
Retrieve data by id from the database
- Method: GET
- URL:
http://localhost:9000/api/v1/data/:id
- Replace
:id
with the ID of the data entry you want to retrieve.
- Replace
Example request URL:
http://localhost:9000/api/v1/data/60ccf1b2d5c4d051c8d4e20c
- Status Code: 200 (OK)
- Body:
{ "message": "Data Retrieval By Id: SUCCESS π", "data": { "_id": "60ccf1b2d5c4d051c8d4e20c", "type": "user", "data": { "name": "John Doe", "email": "[email protected]" }, "metadata": { "created_at": "2025-01-27T00:00:00Z" }, "__v": 0 } }
Update data by id from the database
- Method: PATCH
- URL:
http://localhost:9000/api/v1/data/:id
- Replace
:id
with the ID of the data entry you want to update.
- Replace
- Body: JSON data with the updated values for
type
,data
, andmetadata
.
Example request body:
{
"type": "user",
"data": {
"name": "John Doe",
"email": "[email protected]"
},
"metadata": {
"updated_at": "2025-01-27T12:00:00Z"
}
}
- Status Code: 200 (OK)
- Body:
{ "message": "Data Update By Id: SUCCESS π", "data": { "_id": "60ccf1b2d5c4d051c8d4e20c", "type": "user", "data": { "name": "John Doe", "email": "[email protected]" }, "metadata": { "updated_at": "2025-01-27T12:00:00Z" }, "__v": 0 } }
Delete data by id from the database
- Method: DELETE
- URL:
http://localhost:9000/api/v1/data/:id
- Replace
:id
with the ID of the data entry you want to delete.
- Replace
Example request URL:
http://localhost:9000/api/v1/data/60ccf1b2d5c4d051c8d4e20c
- Status Code: 200 (OK)
- Body:
{ "message": "Data Deletion By Id: SUCCESS π", "data": { "_id": "60ccf1b2d5c4d051c8d4e20c", "type": "user", "data": { "name": "John Doe", "email": "[email protected]" }, "metadata": { "updated_at": "2025-01-27T12:00:00Z" }, "__v": 0 } }
If an error occurs, the API will return a response with a 400
or 500
status code, along with a message explaining the issue.
- 400 - Bad Request (e.g., missing required fields).
- 404 - Not Found (e.g., trying to retrieve or delete data by a non-existing ID).
- 500 - Internal Server Error (e.g., database connection issue).
Example error response:
{
"message": "Data Creation: FAILED π¨",
"error": "Unable to create data π’"
}
Method | Endpoint | Description | Request Body Example | Expected Response |
---|---|---|---|---|
POST | /api/v1/data |
Create new data entry | { "type": "user", "data": {...}, "metadata": {...}} |
201 Created with data details |
GET | /api/v1/data |
Retrieve all data entries | ?type=user (optional) |
200 OK with an array of data entries |
GET | /api/v1/data/:id |
Retrieve data by ID | : id` |
200 OK with a single data entry |
PATCH | /api/v1/data/:id |
Update data by ID | { "type": "user", "data": {...}, "metadata": {...}} |
200 OK with updated data |
DELETE | /api/v1/data/:id |
Delete data by ID | N/A | 200 OK with deleted data |
This repository is designed to be a forkable starting point for developers to:
- Quickly connect to a NoSQL database using MongoDB Atlas.
- Build and deploy a RESTful API without setting up boilerplate code from scratch.
- Easily deploy the API to production via platforms as AWS or Render.
This starter API provides a simple yet scalable foundation for creating RESTful APIs, making it easy to customize and extend according to your needs. By default, you can CREATE any types of data (i.e., users, posts, comments, products), READ that data, UPDATE that data and of course, DELETE that data.
- Go to the GitHub repository page.
- Click the Fork button at the top right corner to create your own copy of the repository.
- Clone the forked repository to your local machine using:
git clone <your-forked-repo-url>
- Go to MongoDB Atlas and sign up for a free account if you don't already have one.
- Create a new database cluster by following these steps:
- Click Build a Cluster and choose a cloud provider and region (free tier available).
- Once the cluster is created, click Connect and follow the steps to:
- Whitelist your IP address (or choose "Allow access from anywhere").
- Create a new database user with a username and password.
- Copy the connection string (URI) provided by MongoDB Atlas.
At the root of your project, create a new file named .env.development
and replace the placeholders with your actual values:
PORT="9000"
DATABASE="mongodb"
MONGO_URI="<your-mongodb-connection-uri>"
NODE_ENV="development" # Optional, defaults to 'development'
Note, you can create additional .env variable files based on the environment such as .env.test
or .env.production
, that way you can use different databases for different env as well.
For example, if your MongoDB Atlas username is admin
and password is password123
, your URI might look like this:
MONGO_URI="mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority"
Important: Do not hardcode sensitive information like passwords directly into your codebase. For production, use environment secrets provided by your deployment platform.
To start the development server locally:
- Install the dependencies:
npm install
- Run the development server:
npm run dev
- The server should now be running at
http://localhost:9000
. You can test the API endpoints using tools like Postman or cURL.
You can deploy the API to a production environment using either Render or AWS:
- Go to Render and create an account.
- Create a new Web Service:
- Connect your forked GitHub repository.
- Add the environment variables (
PORT
,DATABASE
,MONGO_URI
,NODE_ENV
) under the Environment tab. - Set the Start Command to
npm start
.
- Render will automatically build and deploy your API.
- Set up an AWS account if you donβt already have one.
- Use AWS Elastic Beanstalk or AWS Lambda to deploy your API:
- For Elastic Beanstalk:
- Package your app, upload it, and configure the environment variables (
PORT
,DATABASE
,MONGO_URI
,NODE_ENV
). - AWS will handle the deployment and scaling.
- Package your app, upload it, and configure the environment variables (
- For Lambda + API Gateway:
- Use tools like Serverless Framework or AWS SAM to deploy your API.
- Set up your MongoDB Atlas credentials securely in AWS Secrets Manager.
- For Elastic Beanstalk:
Tip: Always test the deployment on a staging environment before pushing to production.
- Feel free to customize the API routes, models, and middleware to fit your use case.
- Regularly review and update dependencies for security.
- Add automated tests to ensure the API works as expected during development and deployment.
This repository gives you the quickest path to creating and deploying a scalable RESTful API. Hope it helps... enjoy coding! π