-
This project is developed as a demonstration of technical capabilities and is not affiliated with, endorsed by, or connected to any commercial entity or service with a similar name
-
The tech stack used in this project does not limit my expertise in other technologies. I am proficient in various programming languages, frameworks, and tools beyond what is demonstrated here and willing to master any other tool or framework as well.
-
I have added comprehensive documentation and code comments throughout the project. If you have any questions or need clarification, please feel free to contact me at [email protected].
A robust REST API service that provides phone number lookup, contact management, and spam detection functionality.
This API service allows users to:
- Register and authenticate with phone numbers
- Search for contacts by name or phone number
- Get detailed information about phone numbers
- Manage personal contacts
- Report and identify spam numbers
- Backend: Node.js with Express.js
- Language: TypeScript
- Database: PostgreSQL with Prisma ORM
- Caching: Redis
- API Documentation: Swagger/OpenAPI
- Containerization: Docker
- Authentication: Custom token-based authentication
- Rate Limiting: Redis-based rate limiting
- Validation: Zod for request validation
- CI / CD: GitHub Actions.
- Version Control: Git
- User registration with phone number
- Secure login with password
- Session management
- Add, update, list, and delete contacts
- Search contacts by name
- Search contacts by phone number
- Get detailed information about a phone number
- Report numbers as spam
- Check spam likelihood for numbers
- Spam score calculation based on community reports
- Redis caching for frequently accessed data
- Rate limiting to prevent abuse
- Pagination for search results
The API is fully documented using Swagger. When the server is running, you can access the interactive API documentation at:
http://localhost:8080/api-docs
All API endpoints are prefixed with /api.
| Method | Endpoint | Description | Authentication Required |
|---|---|---|---|
| POST | /auth/register |
Register a new user with name, phone number, and password | No |
| POST | /auth/login |
Login with phone number and password | No |
| POST | /auth/logout |
Logout and invalidate token | Yes |
All user routes require authentication with a valid JWT token.
| Method | Endpoint | Description | Authentication Required |
|---|---|---|---|
| GET | /user/profile |
Get user profile information | Yes |
| PATCH | /user/profile |
Update user profile information | Yes |
| GET | /user/contacts |
Get all contacts with pagination | Yes |
| GET | /user/contacts/all |
Get all contacts | Yes |
| POST | /user/contacts/new |
Add a new contact | Yes |
| GET | /user/contacts/:id |
Get a specific contact by ID | Yes |
| PATCH | /user/contacts/:id |
Update a specific contact | Yes |
| DELETE | /user/contacts/:id |
Delete a specific contact | Yes |
| POST | /user/report-spam |
Report a phone number as spam | Yes |
| Method | Endpoint | Description | Authentication Required |
|---|---|---|---|
| GET | /lookup/search/name |
Search for contacts by name | No |
| GET | /lookup/search/phone |
Search for contacts by phone number | No |
| GET | /lookup/phone/:phoneNumber |
Get detailed information about a phone number | No |
| GET | /lookup/premium/search/name |
Premium feature: Search for contacts by name | Yes |
All API endpoints return responses in JSON format with the following structure:
{
"success": true/false,
"message": "Optional message",
"data": {
/* Response data */
},
"pagination": {
/* Pagination info if applicable */
}
}Authentication is implemented using JWT (JSON Web Tokens). To access protected routes:
- Obtain a token by registering or logging in
- Include the token in the Authorization header of subsequent requests:
Authorization: Bearer <your_token>
The API implements rate limiting to prevent abuse:
- Authentication routes: 10 requests per minute
- Lookup routes: 100 requests per minute
- User routes: 100 requests per minute
Create a .env file in the root directory with the following variables:
# Application environment: DEVELOPMENT or PRODUCTION
# Controls various optimizations and security features
NODE_ENV=DEVELOPMENT
# Port on which the server will run
PORT=8080
# Session token validity period in seconds (30 days)
# Controls how long a user stays logged in before requiring re-authentication
SESSION_TTL=2592000
# PostgreSQL database connection string
# Format: postgresql://[user]:[password]@[host]:[port]/[database]
DATABASE_URL=postgresql://admin:password@localhost:5432/truecaller_db
# Redis connection string for caching and rate limiting
# Format: redis://[host]:[port]
REDIS_URL=redis://localhost:6379
# Redis cache time-to-live in seconds (1 hour)
# Controls how long cached data remains valid
REDIS_TTL=3600
# Rate limiting window in seconds (1 minute)
# Defines the time period for rate limiting
REDIS_RATE_LIMIT_WINDOW=60
# Maximum number of requests allowed per window
# Controls how many API calls a user can make within the rate limit window
REDIS_RATE_LIMIT_MAX=100
The project includes Docker configuration for setting up the development environment with PostgreSQL, Redis, and pgAdmin.
- Start the development environment:
cd docker
docker-compose up -dThis will start:
- PostgreSQL database on port 5432
- Redis on port 6379
- pgAdmin web interface on port 5050
For production deployment, you can build and run the application container:
- Build the Docker image:
docker build -t truecaller-api -f docker/Dockerfile .- Run the container:
docker run -p 8080:8080 --env-file .env truecaller-api- Node.js (v20 or later)
- npm (v7 or later)
- PostgreSQL (if running without Docker)
- Redis (if running without Docker)
- Clone the repository:
git clone <repository-url>
cd truecaller-api- Install dependencies:
npm install- Set up environment variables:
cp example.env .env
# Edit .env with your configuration- Generate Prisma client, run migrations, and seed the database:
npm run setupnpm run devThis starts the application with hot reloading using tsx.
npm run build
npm startThe application uses the following data models:
- Stores registered user information
- Contains name, phone number, email, and password hash
- Represents a user's contacts
- Links to User model if the contact is also a registered user
- Tracks spam reports submitted by users
- Used to calculate spam likelihood for phone numbers
- Manages authentication sessions
- Stores tokens with expiration times
- API responses are cached using Redis to improve performance
- Rate limiting is applied to prevent abuse
- Database queries are optimized with appropriate indexes
- Pagination is implemented for all list endpoints
- Password hashing for secure storage
- Token-based authentication
- Rate limiting to prevent brute force attacks
- Input validation using Zod
- Helmet middleware for HTTP security headers
The project implements several design patterns to ensure maintainability, scalability, and code organization:
- Error Handling Middleware: Centralized error handling through custom middleware
- Authentication Middleware: JWT token validation and user authentication
- Caching Middleware: Redis-based response caching for improved performance
- Rate Limiting Middleware: Protection against API abuse
- Separation of data access logic using Prisma ORM
- Abstraction of database operations from business logic
TryCatchdecorator for wrapping controller functions with error handling- Reduces boilerplate code and ensures consistent error management
- Creation of response objects with consistent structure
- Error handler factory for generating standardized error responses
- Database connection management
- Redis service implementation ensuring single instance
- Cache implementation acting as a proxy to database access
- Intercepts and caches API responses
- Event-based cache invalidation when data changes
- Clear separation of Models, Views (API responses), and Controllers
- Organized code structure following REST principles
Note: This project is not publicly available on GitHub and not hosted on a live server. The CI/CD pipeline is for demonstration purposes only.
This project uses GitHub Actions for continuous integration and deployment:
- Continuous Integration: Automated testing and code quality checks on each push
- Continuous Deployment: Automated deployment to staging (main branch) and production (prod branch) environments
- Docker Integration: Builds and pushes Docker images to Docker Hub
- EC2 Deployment: Automatically deploys the latest image to EC2 instances
The CI/CD pipeline ensures that code changes are automatically tested, built, and deployed to the appropriate environment.
If given more time, the following features and improvements could be implemented:
- Set up comprehensive integration test suite using Jest and Supertest
- Implement database mocking for test environment
- Add CI/CD pipeline for automated testing
- Create test coverage reports
- Implement end-to-end API testing
- Implement SMS-based OTP verification for user registration
- Enable password reset via OTP
- Implement comprehensive application monitoring.
- Add health check endpoints for all services (API, database, Redis)