This is a basic CRUD (Create, Read, Update, Delete) API for managing Todos.
Before running the API, make sure you have the following installed:
If you don't have MongoDB installed, you can use a cloud service like MongoDB Atlas or mLab.
-
Clone the repository:
git clone https://github.com/hardik-143/the-crud-api.git
-
Configure the database connection: Open the
config.js
file and update the database connection URL and port.const DATABASEURL = 'mongodb://localhost:27017/todos' const PORT = 9000; export { DATABASEURL, PORT };
Replace
mongodb://localhost:27017/todos
with your MongoDB connection URL.Using MongoDB Atlas:
const DATABASEURL = 'mongodb+srv://<username>:<password>@<host>/<database>?retryWrites=true&w=majority' const PORT = 9000; export { DATABASEURL, PORT };
Using mLab:
const DATABASEURL = 'mongodb://<username>:<password>@<host>:<port>/<database>' const PORT = 9000; export { DATABASEURL, PORT };
Using Environment Variables is recommended for storing sensitive information like database credentials.
-
Install the dependencies:
npm install
Above command will install all the dependencies required for the project.
To start the API in your local environment, run the following command:
npm start
The API will start on the port specified in the config.js
file. You can access the API at http://localhost:9000.
Introduction to the API:
a simple CRUD API for managing your todos.
- Create a new todo
- Get all todos
- Get a specific todo by ID
- Update an existing todo
- Delete an existing todo
- Password protected
- Secure API
Method | URL | Description |
---|---|---|
GET |
/todos |
Get all todos |
GET |
/todos/:id |
Get a specific todo by ID |
POST |
/todos |
Create a new todo |
PUT |
/todos/:id |
Update an existing todo |
DELETE |
/todos/:id |
Delete an existing todo |
Parameter | Type | Description |
---|---|---|
user |
string | The name of the user |
max |
number | The maximum number of todos to return |
page |
number | The page number of the results |
limit |
number | The number of todos to return per page |
Note:
- The
max
,page
, andlimit
parameters are optional. - Prefer using the
page
andlimit
parameters for pagination. - The
max
parameter will override thepage
andlimit
parameters.
Request examples for creating, updating, and deleting todos:
{
"todo": "Todo Title",
"created_by": "YOUR NAME",
"password" : "YOUR PASSWORD",
}
Note: Please remember your password. It is crucial for creating new todos and modify existing ones.
{
"todo": "Updated Todo Title",
"password" : "YOUR PASSWORD"
}
{
"password" : "YOUR PASSWORD",
}
Using the API via Postman or any other API client:
- Create a new todo:
- Send a
POST
request tohttp://localhost:9000/todos
with the request body as shown above.
- Send a
- Get all todos:
- Send a
GET
request tohttp://localhost:9000/todos
.
- Send a
- Get a specific todo by ID:
- Send a
GET
request tohttp://localhost:9000/todos/:id
where:id
is the ID of the todo.
- Send a
- Update an existing todo:
- Send a
PUT
request tohttp://localhost:9000/todos/:id
with the request body as shown above.
- Send a
- Delete an existing todo:
- Send a
DELETE
request tohttp://localhost:9000/todos/:id
where:id
is the ID of the todo.
- Send a
Here are some examples of using the API with curl
:
-
Create a new todo:
curl -X POST http://localhost:9000/todos -H "Content-Type: application/json" -d '{"todo": "Todo Title", "created_by": "YOUR NAME", "password": "YOUR PASSWORD"}'
-
Get all todos:
curl http://localhost:9000/todos
-
Get a specific todo by ID:
curl http://localhost:9000/todos/:id
-
Update an existing todo:
curl -X PUT http://localhost:9000/todos/:id -H "Content-Type: application/json" -d '{"todo": "Updated Todo Title", "password": "YOUR PASSWORD"}'
-
Delete an existing todo:
curl -X DELETE http://localhost:9000/todos/:id -H "Content-Type: application/json" -d '{"password ": "YOUR PASSWORD"}'
Replace
:id
with the ID of the todo.
The API is live at https://the-crud-api.vercel.app
- When you need a simple CRUD API for managing todos.
- When you need to create, read, update, and delete todos.
- When you need a secure API with password protection.
- When you need a rate-limited API to prevent abuse.
- When you need a CORS-protected API.
The API has rate limiting enabled to prevent abuse. The rate limit is set to 100 requests per hour. If you exceed the rate limit, you will receive a 429 Too Many Requests
response.
Here is an example of the rate limiting response:
HTTP/1.1 429 Too Many Requests
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 3600
Content-Type: application/json
Content-Length: 35
Date: Thu, 01 Jul 2021 12:00:00 GMT
{
"message": "Rate limit exceeded"
}
The API returns the following headers in the response:
Header | Description |
---|---|
X-RateLimit-Limit |
The maximum number of requests allowed in a given time frame. |
X-RateLimit-Remaining |
The number of requests remaining in the current time frame. |
X-RateLimit-Reset |
The time when the rate limit will reset. |
Content-Type |
The type of content in the response body. |
Content-Length |
The length of the response body in bytes. |
Date |
The date and time when the response was sent. |
Here are some security features implemented in the API:
- Password protection for creating, updating, and deleting todos.
- Secure API using HTTPS.
- CORS protection.
- Rate limiting to prevent abuse.
👤 Hardik desai