A simple and efficient Todo API built with Node.js, Express, MongoDB, and JWT authentication.This API follows RESTful principles, ensuring stateless communication, use of standard HTTP methods, and clear resource-based URL structures.
- π Introduction
- π§ Prerequisites
- βοΈ Installation
- π Usage
- π Update Todo
- β Delete Todo
- π οΈ API Routes
β οΈ Error Codes- π¦ Rate Limiting
- π Authentication
- π Glossary
- βοΈ Feedback
- π€ Contributing
- π License
- π¬ Contact
Welcome to the Todo API! This API allows you to manage your todos with features such as creating, reading, updating, and deleting todos. It also includes user authentication using JSON Web Tokens (JWT).The API is designed following RESTful principles to ensure stateless, scalable, and efficient interaction with clients.
This API adheres to RESTful principles in the following ways:
- Resource-Based URLs : Each resource (todos and authentication) is accessed using standard HTTP methods (GET, POST, PATCH, DELETE) with clear and meaningful URLs (
/api/todos
,/api/auth/signup
, etc.). - HTTP Methods : Different HTTP methods are used to perform different actions on resources. For example:
- GET
/api/todos
: Retrieves all todos. - POST
/api/todos
: Creates a new todo. - PATCH
/api/todos/:id
: Updates a specific todo. - DELETE
/api/todos/:id
: Deletes a specific todo
- GET
- Statelessness : The API is stateless, meaning each request from a client to the server must contain all the information necessary to understand and fulfill the request. This simplifies server implementation and improves scalability.
- Use of HTTP Status Codes: The API uses appropriate HTTP status codes to indicate the success or failure of an API request (e.g., 200 OK, 201 Created, 400 Bad Request, 401 Unauthorized, 404 Not Found). - JSON Format: Data is exchanged in JSON format, which is lightweight and easy to parse by client applications.
- Authentication: Authentication is handled using JWT (JSON Web Tokens), providing a secure way to authenticate API requests without needing to maintain session state on the server.
Before you begin, ensure you have met the following requirements:
- You have Node.js and npm installed.
- You have MongoDB installed or access to MongoDB Atlas.
- You have a code editor like Visual Studio Code.
-
Clone the repository:
git clone https://github.com/lokeshleela04/todo-api.git cd todo-api
-
Install dependencies:
npm install
-
Create a .env file and add your MongoDB URI and JWT secret:
PORT=3000 MONGO_URI=your_mongodb_uri JWT_SECRET=your_jwt_secret
-
Start the server
npm start
https://todo-api-yry4.onrender.com
You can use Thunder Client (VS Code extension) or Postman to interact with the API. Here are the available routes and how to use them
- URL:
/api/auth/signup
- Method:
POST
- Body:
{ "username": "testuser", "password": "password123" }
- Example: https://todo-api-yry4.onrender.com/api/auth/signup (if you are using the hosted link)
or
https://localhost:3000/api/auth/signup (if you have cloned the project)
- URL:
/api/auth/login
- Method:
POST
- Body:
{ "username": "testuser", "password": "password123" }
- Response:
{ "token": "your_jwt_token_here" }
- URL:
/api/todos
- Method:
POST
- Headers:
Authorization: Bearer your_jwt_token_here
- Body:
{ "title": "New Todo" }
- URL:
/api/todos
- Method:
GET
- Headers:
Authorization: Bearer your_jwt_token_here
- Response:
[
{
"_id": "60c72b2f9b1d4c001e9c5e6b",
"title": "New Todo",
"completed": false,
"createdAt": "2023-06-01T10:00:00.000Z",
"user": "60c72b2f9b1d4c001e9c5e6a"
}
]
The _id
field in each todo object is the unique ID for that todo task.
Get _id
from here to do further tasks
This API employs caching to improve performance and reduce response times for frequently requested data. When a GET request is made for todos, the API caches the response for a certain duration, typically 60 seconds. Subsequent requests for the same resource within this period retrieve data from the cache, significantly reducing response times and server load.
For Eg : Send a Get Request to api/todos
, Frist time , it takes around 190ms , send a one more get request to same Route within 60 sec( we designed cache-storage for 60 sec) Now observe the time taken it 10 times lesser than frist request , it probably around 15 ms
- URL:
/api/todos/:id
- Method:
PATCH
- Headers:
Authorization: Bearer your_jwt_token_here
- Body:
or ( if you wish to update the title)
{ "completed": true }
{ "title": "Updated title", "completed": true }
- URL:
/api/todos/:id
- Method:
DELETE
- Headers:
Authorization: Bearer your_jwt_token_here
POST /api/auth/signup
- Sign up a new userPOST /api/auth/login
- Log in and get a tokenPOST /api/todos
- Create a new todoGET /api/todos
- Get all todos for the logged-in userPATCH /api/todos/:id
- Update a todo (partial update)DELETE /api/todos/:id
- Delete a todo
Below are some common error codes you might encounter
- 400 Bad Request : The request could not be understood or was missing required parameters.
- 401 Unauthorized : Authentication failed or user does not have permissions for the desired action.
- 404 Not Found : The requested resource could not be found.
- 500 Internal Server Error : An error occurred on the server.
You can make up to 100 requests per hour. Exceeding this limit will result in a 429 Too Many Requests response.
All endpoints require a valid API key. Include the API key in the Authorization header of your requests:
Authorization: Bearer {api_key}
To obtain an API key, register on our website and navigate to the API keys section of your account settings.
- API Key : A unique key used to authenticate requests.
- Endpoint : A specific URL where API requests are sent.
- Rate Limiting : Restricting the number of API requests within a time period.
- Versioning : Managing changes to the API by releasing different versions.
- HTTP Method : The action to be performed on the resource (GET, POST, PATCH, DELETE).
We value your feedback! Please Email us with any questions or suggestions.
Contributions are welcome! Please fork the repository and create a pull request with your changes. Feel free to open issues for any bugs or feature requests.
This project is licensed under the MIT License.
If you have any questions or need help, feel free to reach out:
- Email: [email protected]
- GitHub: lokeshleela04
Feel free to use this API in your projects and let us know if you encounter any issues. Happy coding!