A RESTful API for managing customers with full CRUD operations, built with Node.js and Express.
- Create new customers
- Read all customers or specific customer
- Update existing customers
- Delete customers
- Search customers by name, email, or company
- Filter customers by status
- Validation for required fields and duplicate emails
- Error handling with proper HTTP status codes
customer-api/
βββ customer_server.js # Main API server
βββ test_customer_api.js # Automated test script
βββ package.json # Dependencies and scripts
βββ README.md # This file
βββ Customer_API.postman_collection.json # Postman collection
Make sure Node.js is installed:
node --version
npm --versioncd customer-api
npm installThis will install:
express- Web framework for the API servernodemon- Auto-restart during development (optional)
npm start
# or
node customer_server.jsYou'll see:
ββββββββββββββββββββββββββββββββββββββββββ
β Customer Management API Started π β
ββββββββββββββββββββββββββββββββββββββββββ
π Server running at: http://localhost:3001
π API Endpoint: http://localhost:3001/api/customers
npm run dev
# or
nodemon customer_server.js| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Health check and API info |
| GET | /api/customers |
Get all customers |
| GET | /api/customers/:id |
Get specific customer |
| POST | /api/customers |
Create new customer |
| PUT | /api/customers/:id |
Update customer |
| DELETE | /api/customers/:id |
Delete customer |
| GET | /api/customers/status/:status |
Get customers by status |
| GET | /api/customers/search/:query |
Search customers |
Run the comprehensive test suite:
npm test
# or
node test_customer_api.jsThis will test all CRUD operations, validation, error handling, and edge cases.
- Method:
POST - URL:
http://localhost:3001/api/customers - Body (JSON):
{
"name": "Ahmed Mohamed",
"email": "ahmed@example.com",
"phone": "+218-91-123-4567",
"address": "Tripoli, Libya",
"company": "Tech Solutions"
}- Expected:
201 Created
- Method:
GET - URL:
http://localhost:3001/api/customers - Expected:
200 OKwith customer list
- Method:
GET - URL:
http://localhost:3001/api/customers/CUST-123... - Expected:
200 OKwith customer details
- Method:
PUT - URL:
http://localhost:3001/api/customers/CUST-123... - Body (JSON):
{
"name": "Ahmed Mohamed Updated",
"phone": "+218-91-999-8888"
}- Expected:
200 OK
- Method:
GET - URL:
http://localhost:3001/api/customers/search/ahmed - Expected:
200 OKwith search results
- Method:
DELETE - URL:
http://localhost:3001/api/customers/CUST-123... - Expected:
200 OK
# Create customer
curl -X POST http://localhost:3001/api/customers \
-H "Content-Type: application/json" \
-d '{"name":"Ahmed Mohamed","email":"ahmed@example.com","phone":"+218-91-123-4567"}'
# Get all customers
curl http://localhost:3001/api/customers
# Get specific customer
curl http://localhost:3001/api/customers/CUST-123...
# Update customer
curl -X PUT http://localhost:3001/api/customers/CUST-123... \
-H "Content-Type: application/json" \
-d '{"name":"Updated Name"}'
# Search customers
curl http://localhost:3001/api/customers/search/ahmed
# Delete customer
curl -X DELETE http://localhost:3001/api/customers/CUST-123...{
"id": "CUST-1234567890-1",
"name": "Ahmed Mohamed",
"email": "ahmed@example.com",
"phone": "+218-91-123-4567",
"address": "Tripoli, Libya",
"company": "Tech Solutions",
"status": "active",
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z"
}name(string) - Customer's full nameemail(string) - Customer's email address (must be unique)
phone(string) - Customer's phone numberaddress(string) - Customer's addresscompany(string) - Customer's company name
id(string) - Unique customer IDstatus(string) - Customer status (default: "active")createdAt(string) - Creation timestampupdatedAt(string) - Last update timestamp
Search by name, email, or company:
GET /api/customers/search/ahmed
Get customers with specific status:
GET /api/customers/status/active
- Name and email are required
- Email must be unique
- Phone, address, and company are optional
- Email format validation (basic)
The API returns appropriate HTTP status codes:
200 OK- Successful GET, PUT, DELETE201 Created- Successful POST400 Bad Request- Missing required fields404 Not Found- Customer not found409 Conflict- Duplicate email500 Internal Server Error- Server error
{
"success": false,
"message": "Error description",
"requestedId": "CUST-123..." // (for 404 errors)
}After using this API, you'll understand:
- β RESTful API design principles
- β HTTP methods (GET, POST, PUT, DELETE)
- β HTTP status codes (200, 201, 400, 404, 409, 500)
- β JSON request/response format
- β Input validation and error handling
- β CRUD operations in practice
- β API testing strategies
npm install# Find and kill the process
lsof -ti:3001 | xargs kill -9Install Node.js from: https://nodejs.org/
- Check if server is running:
npm start - Verify port 3001 is available
- Check console for error messages
After mastering this API, consider:
- Adding authentication (JWT tokens)
- Database integration (MySQL, PostgreSQL)
- Input validation middleware (Joi, express-validator)
- API documentation (Swagger/OpenAPI)
- Rate limiting and security
- Unit testing with Jest
- Docker containerization
Feel free to:
- Add new features
- Improve error handling
- Add more test cases
- Enhance documentation
- Optimize performance
MIT License - feel free to use this code for learning and development!
Happy coding! π