Skip to content

Repository files navigation

📱 Contact Manager API

A RESTful API for managing personal contacts with user authentication. Built with Node.js, Express.js, and MongoDB.

✨ Features

  • 🔐 User registration and authentication with JWT
  • 📇 Full CRUD operations for contacts
  • 🔒 Password hashing with bcrypt
  • 👤 User-specific contacts (each user can only access their own contacts)
  • ⚡ Async error handling

🛠️ Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB with Mongoose ODM
  • Authentication: JSON Web Tokens (JWT)
  • Password Hashing: bcrypt

📋 Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or Atlas)
  • npm or yarn

🚀 Getting Started

1. Clone the repository

git clone https://github.com/yourusername/contact-manager.git
cd contact-manager

2. Install dependencies

npm install

3. Set up environment variables

Create a .env file in the root directory:

PORT=5000
MONGO_URI=mongodb://localhost:27017/contact-manager
JWT_SECRET=your_super_secret_key_here

4. Run the application

Development mode (with auto-reload):

npm run dev

Production mode:

npm start

The server will start on http://localhost:5000

📚 API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /api/users/register Register a new user
POST /api/users/login Login user
GET /api/users/current Get current user info

Contacts

Method Endpoint Description Auth Required
GET /api/contacts Get all contacts
POST /api/contacts Create a contact
GET /api/contacts/:id Get a contact
PUT /api/contacts/:id Update a contact
DELETE /api/contacts/:id Delete a contact

📖 API Usage

Register a User

POST /api/users/register
Content-Type: application/json

{
  "username": "johndoe",
  "email": "john@example.com",
  "password": "password123"
}

Response:

{
  "_id": "64a1b2c3d4e5f6g7h8i9j0k1",
  "email": "john@example.com"
}

Login

POST /api/users/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "password123"
}

Response:

{
  "message": "Login Successfully",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Create a Contact

POST /api/contacts
Authorization: <your_jwt_token>
Content-Type: application/json

{
  "name": "Jane Doe",
  "email": "jane@example.com",
  "phone": "123-456-7890"
}

Response:

{
  "message": "Contact Created",
  "contact": {
    "_id": "64a1b2c3d4e5f6g7h8i9j0k2",
    "userId": "64a1b2c3d4e5f6g7h8i9j0k1",
    "name": "Jane Doe",
    "email": "jane@example.com",
    "phone": "123-456-7890",
    "createdAt": "2026-01-30T10:00:00.000Z",
    "updatedAt": "2026-01-30T10:00:00.000Z"
  }
}

Get All Contacts

GET /api/contacts
Authorization: <your_jwt_token>

Get Single Contact

GET /api/contacts/:id
Authorization: <your_jwt_token>

Update Contact

PUT /api/contacts/:id
Authorization: <your_jwt_token>
Content-Type: application/json

{
  "name": "Jane Smith",
  "phone": "987-654-3210"
}

Delete Contact

DELETE /api/contacts/:id
Authorization: <your_jwt_token>

📁 Project Structure

contact-manager/
├── config/
│   └── db.js              # Database connection
├── controllers/
│   ├── contactController.js   # Contact CRUD logic
│   └── userController.js      # User auth logic
├── middleware/
│   ├── auth.js            # JWT authentication
│   └── errorHandler.js    # Global error handler
├── models/
│   ├── contactModel.js    # Contact schema
│   └── userModel.js       # User schema
├── routes/
│   ├── contactRoutes.js   # Contact endpoints
│   └── userRoutes.js      # User endpoints
├── .env                   # Environment variables
├── constants.js           # HTTP status codes
├── package.json
├── README.md
└── server.js              # Entry point

🔒 Authentication

This API uses JWT (JSON Web Tokens) for authentication. After logging in, include the token in the Authorization header for all protected routes:

Authorization: <your_jwt_token>

Tokens expire after 1 day.

⚠️ Error Responses

The API returns consistent error responses:

{
  "title": "Error Type",
  "message": "Error description",
  "stackTrace": "..." 
}
Status Code Title Description
400 Validation Failed Invalid input data
401 Unauthorized Invalid or missing token
403 Forbidden Access denied
404 Not Found Resource not found
500 Server Error Internal server error

👤 Author

Deepraj Singh

📄 License

This project is licensed under the ISC License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages