Skip to content

Todo List API with JWT Authentication A simple Node.js and Express API for managing todo tasks, featuring user authentication with JWT. Includes routes for user signup, login, and CRUD operations on todos. Secured with MongoDB for data storage and bcrypt for password hashing.

Notifications You must be signed in to change notification settings

LeelaPrasadMaturu/REST-todo-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Todo API Restful Todo API

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.

Table of Contents

πŸ“œ Introduction

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.

Node.js Express MongoDB

πŸ“œ Why This API is RESTful

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
  • 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.

πŸ”§ Prerequisites

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.

βš™οΈ Installation

  1. Clone the repository:

    git clone https://github.com/lokeshleela04/todo-api.git
    cd todo-api
  2. Install dependencies:

    npm install
  3. Create a .env file and add your MongoDB URI and JWT secret:

    PORT=3000
    MONGO_URI=your_mongodb_uri
    JWT_SECRET=your_jwt_secret
    
  4. Start the server

    npm start

Alternatively, you can directly use the hosted API at this link without cloning the entire project:

https://todo-api-yry4.onrender.com

πŸš€ Usage

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

πŸ“ Sign Up


or
https://localhost:3000/api/auth/signup (if you have cloned the project)

πŸ” Log In

  • URL: /api/auth/login
  • Method: POST
  • Body:
    {
      "username": "testuser",
      "password": "password123"
    }
  • Response:
    {
      "token": "your_jwt_token_here"
    }

βž• Create Todo

  • URL: /api/todos
  • Method: POST
  • Headers:
    • Authorization: Bearer your_jwt_token_here
  • Body:
    {
      "title": "New Todo"
    }

πŸ“‹ Get Todos

  • 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

πŸ“œ Cache-based API

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


πŸ”„ Update Todo

  • URL: /api/todos/:id
  • Method: PATCH
  • Headers:
    • Authorization: Bearer your_jwt_token_here
  • Body:
    {
      "completed": true
    }
    or ( if you wish to update the title)
    {
    "title": "Updated title",
    "completed": true
    }

❌ Delete Todo

  • URL: /api/todos/:id
  • Method: DELETE
  • Headers:
    • Authorization: Bearer your_jwt_token_here

πŸ› οΈ API Routes

  • POST /api/auth/signup - Sign up a new user
  • POST /api/auth/login - Log in and get a token
  • POST /api/todos - Create a new todo
  • GET /api/todos - Get all todos for the logged-in user
  • PATCH /api/todos/:id - Update a todo (partial update)
  • DELETE /api/todos/:id - Delete a todo

⚠️ Error Codes

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.

🚦 Rate Limiting

You can make up to 100 requests per hour. Exceeding this limit will result in a 429 Too Many Requests response.

πŸ” Authentication

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.

πŸ“š Glossary

  • 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).

βœ‰οΈ Feedback

We value your feedback! Please Email us with any questions or suggestions.

🀝 Contributing

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.

πŸ“œ License

This project is licensed under the MIT License.

πŸ“¬ Contact

If you have any questions or need help, feel free to reach out:

Feel free to use this API in your projects and let us know if you encounter any issues. Happy coding!

About

Todo List API with JWT Authentication A simple Node.js and Express API for managing todo tasks, featuring user authentication with JWT. Includes routes for user signup, login, and CRUD operations on todos. Secured with MongoDB for data storage and bcrypt for password hashing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published