A modern, AI-powered accident reporting and emergency dispatch system built with NestJS, TypeScript, and OpenAI Vision API.
SmartResponse API is a comprehensive backend system designed to streamline accident reporting and emergency response coordination. The system leverages AI to analyze accident scene images, automatically assess severity, and dispatch appropriate emergency services in real-time.
- AI-Powered Image Analysis: Analyzes accident scenes using OpenAI GPT-4 Vision to determine severity, detect injuries, and assess vehicle damage
- Automated Emergency Dispatch: Intelligently routes emergency services (Police, Ambulance, Fire Department) based on AI-determined severity levels
- Secure Authentication: JWT-based authentication with OAuth2 (Google) integration and role-based access control
- Real-time Notifications: Automated alerts for users and emergency services
- Comprehensive Reporting: Track accidents, vehicles, locations, and media with detailed reports
- RESTful API: Well-documented endpoints with Swagger/OpenAPI integration
- Framework: NestJS 11.x
- Language: TypeScript
- Database: PostgreSQL (Neon)
- ORM: TypeORM
- AI Integration: OpenAI GPT-4 Vision API
- Authentication: Passport JWT & Google OAuth2
- File Upload: Multer
- Validation: class-validator & class-transformer
src/
βββ accidents/ # Accident reporting and analysis
βββ ai/ # AI image analysis service
βββ auth/ # Authentication & authorization
βββ dispatch/ # Emergency service dispatch
βββ emergency-services/ # Emergency service management
βββ locations/ # Location tracking
βββ media/ # Media file management
βββ notifications/ # Notification system
βββ reports/ # Report generation
βββ upload/ # File upload handling
βββ users/ # User management
βββ vehicles/ # Vehicle registration
- Node.js 18+
- pnpm package manager
- PostgreSQL database (Neon recommended)
- OpenAI API key (for AI features)
-
Clone the repository
git clone <repository-url> cd SmartResponse-api
-
Install dependencies
pnpm install
-
Configure environment variables
Create a
.envfile in the root directory:# Database DATABASE_URL=your_neon_database_url_here # JWT Configuration JWT_SECRET=your-super-secret-jwt-key JWT_EXPIRATION=15m JWT_REFRESH_SECRET=your-super-secret-refresh-key JWT_REFRESH_EXPIRATION=7d # OpenAI Configuration OPENAI_API_KEY=your_openai_api_key_here # File Upload UPLOAD_DIR=./uploads MAX_FILE_SIZE=10485760 # Application PORT=3000 NODE_ENV=development
-
Run database migrations
# Execute migrations in your database psql $DATABASE_URL -f migrations/001_create_users_table.sql
# Development mode with hot-reload
pnpm run start:dev
# Production mode
pnpm run start:prod
# Debug mode
pnpm run start:debugThe API will be available at http://localhost:3000
Once the application is running, access the interactive API documentation:
- Swagger UI:
http://localhost:3000/api
POST /auth/signup- Register new userPOST /auth/signin- User loginPOST /auth/refresh- Refresh access tokenGET /auth/google- Google OAuth login
POST /accidents/report- Report accident with AI analysisGET /accidents- List all accidentsGET /accidents/:id- Get accident detailsPATCH /accidents/:id- Update accidentDELETE /accidents/:id- Delete accident
GET /emergency-services- List servicesPOST /emergency-services- Create serviceGET /emergency-services/:id- Get service details
GET /users- List users (Admin only)GET /users/:id- Get user profilePATCH /users/:id- Update userDELETE /users/:id- Delete user (Admin only)
The system automatically dispatches emergency services based on AI-analyzed severity:
| Severity Level | Score Range | Dispatched Services |
|---|---|---|
| Critical | 70-100 | Police + Ambulance + Fire Department |
| High | 50-69 | Police + Ambulance |
| Medium | 30-49 | Police Only |
| Low | 0-29 | Notification Only |
The AI service provides:
- Severity score (0-100)
- Detailed scene analysis
- Injury detection and assessment
- Vehicle damage evaluation
- Recommended emergency response
# Unit tests
pnpm run test
# E2E tests
pnpm run test:e2e
# Test coverage
pnpm run test:cov
# Watch mode
pnpm run test:watch- JWT Authentication: Secure token-based authentication
- Role-Based Access Control (RBAC): Admin, Responder, and User roles
- Password Hashing: bcrypt encryption for user passwords
- Google OAuth2: Social login integration
- File Validation: Type and size validation for uploads
- CORS Protection: Configurable cross-origin resource sharing
SmartResponse-api/
βββ src/
β βββ accidents/ # Accident management
β βββ ai/ # AI analysis service
β βββ auth/ # Authentication & guards
β βββ dispatch/ # Emergency dispatch
β βββ database/ # Database configuration
β βββ emergency-services/ # Emergency services
β βββ locations/ # Location tracking
β βββ media/ # Media management
β βββ notifications/ # Notification system
β βββ reports/ # Report generation
β βββ upload/ # File upload
β βββ users/ # User management
β βββ vehicles/ # Vehicle registration
βββ migrations/ # Database migrations
βββ test/ # E2E tests
βββ uploads/ # Uploaded files (generated)
βββ docs/ # Documentation files
# Lint code
pnpm run lint
# Format code
pnpm run format
# Build for production
pnpm run buildSQL migrations are located in the migrations/ directory. Execute them in order:
001_create_users_table.sql
For production deployment:
- Set
NODE_ENV=production - Use strong JWT secrets
- Configure production database
- Set up proper CORS policies
- Enable HTTPS
- Configure file storage (AWS S3, Azure Blob, etc.)
- Set up monitoring and logging
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the UNLICENSED license.
For questions, issues, or feature requests, please open an issue on GitHub.
Built with β€οΈ using NestJS