A robust RESTful API service for managing invoices with user authentication, PDF generation, and complete CRUD operations.
- Features
- Tech Stack
- Prerequisites
- Installation
- Environment Variables
- API Documentation
- Error Handling
- Security
- User authentication and authorization
- Invoice CRUD operations
- PDF invoice generation
- Data validation
- Swagger documentation
- Secure password hashing
- JWT-based authentication
- Node.js
- Express.js
- MongoDB
- JWT for authentication
- PDFKit for PDF generation
- Swagger for API documentation
- Node.js (v14 or higher)
- MongoDB
- npm or yarn
- Clone the repository:
git clone https://github.com/OcheOps/Invoice-api.git- Install dependencies:
cd Invoice-api
npm install-
Set up environment variables (see Environment Variables section)
-
Start the server:
npm run devCreate a .env file in the root directory with the following variables:
PORT=3000
MONGODB_URI=mongodb://localhost:27017/invoice-service
JWT_SECRET=your-secret-key-here- URL:
/api/users/register - Method:
POST - Request Body:
{
"email": "user@example.com",
"password": "password123",
"name": "John Doe"
}- Success Response:
201 Created
{
"token": "jwt-token-here"
}- URL:
/api/users/login - Method:
POST - Request Body:
{
"email": "user@example.com",
"password": "password123"
}- Success Response:
200 OK
{
"token": "jwt-token-here"
}All invoice endpoints require authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your-token>
- URL:
/api/invoices - Method:
POST - Request Body:
{
"clientName": "Acme Corp",
"items": [
{
"description": "Web Development",
"quantity": 1,
"price": 1000
}
],
"dueDate": "2024-12-31"
}- Success Response:
201 Created
{
"_id": "invoice-id",
"invoiceNumber": "INV-123456",
"clientName": "Acme Corp",
"items": [...],
"total": 1000,
"status": "draft",
"dueDate": "2024-12-31",
"createdAt": "2024-01-01T00:00:00.000Z"
}- URL:
/api/invoices - Method:
GET - Success Response:
200 OK
[
{
"_id": "invoice-id",
"invoiceNumber": "INV-123456",
"clientName": "Acme Corp",
"items": [...],
"total": 1000,
"status": "draft",
"dueDate": "2024-12-31",
"createdAt": "2024-01-01T00:00:00.000Z"
}
]- URL:
/api/invoices/:id - Method:
PUT - Request Body:
{
"clientName": "Updated Corp",
"status": "sent"
}- Success Response:
200 OK
{
"_id": "invoice-id",
"invoiceNumber": "INV-123456",
"clientName": "Updated Corp",
"status": "sent",
...
}- URL:
/api/invoices/:id - Method:
DELETE - Success Response:
200 OK
{
"message": "Invoice deleted"
}- URL:
/api/invoices/:id/download - Method:
GET - Success Response:
200 OK - Response Type:
application/pdf - Description: Returns a downloadable PDF file of the invoice
The API uses standard HTTP status codes:
200: Success201: Created400: Bad Request401: Unauthorized404: Not Found500: Server Error
Error responses follow this format:
{
"message": "Error description here"
}- All endpoints (except registration and login) require JWT authentication
- Passwords are hashed using bcrypt
- API is protected with various HTTP headers using helmet
- Input validation is performed using express-validator
- CORS is configured for secure cross-origin requests
API documentation is available at /api-docs when running the server locally.
npm testMIT