This is a RESTful API for creating, managing, and processing invoices, implemented using Node.js, Express, and MongoDB. The API allows clients to create invoices, update existing ones, fetch invoices by ID, and delete them.
- Create invoices with multiple line items
- Update invoices
- Retrieve invoices by ID
- List all invoices
- Delete invoices
- Input validation and sanitization
- Centralized error handling
- Rate limiting
- Node.js
- Express
- MongoDB
- TypeScript
- Validator
- Node.js (v14+ recommended)
- npm
- Clone the repository:
git clone https://github.com/yourusername/invoice-processing-api.git
- Install dependencies:
cd invoice-processing-api
npm install
- Update the
src/database.ts
file with your MongoDB connection details:
const uri = "mongodb+srv://your_username:your_password@cluster0.mongodb.net/your_database_name?retryWrites=true&w=majority";
Replace your_username
, your_password
, and your_database_name
with the appropriate values for your MongoDB instance.
- Start the development server:
npm start
Your API will be running at http://localhost:3000
.
Fetch all invoices.
Fetch an invoice by ID.
Create a new invoice.
Request Payload:
{
"id": "sample-id-1",
"clientName": "Test Client",
"items": [
{
"name": "Item 1",
"price": 10,
"quantity": 2
},
{
"name": "Item 2",
"price": 20,
"quantity": 1
}
]
}
Update an existing invoice.
Request Payload:
{
"clientName": "Updated Client Name",
"items": [
{
"name": "Updated Item 1",
"price": 15,
"quantity": 3
}
]
}
Delete an invoice by ID.
-
Start your API server with
npm start
if it's not running already. -
In a new terminal, run the test script with
npm run test-api
.
This script will run a series of tests on your API, including fetching all invoices, creating a new invoice, fetching an invoice by ID, updating an invoice, and deleting an invoice. The results will be logged to the console.
You can modify this script to test different scenarios and validate the API functionality.
This project is licensed under the MIT License.