A comprehensive NestJS backend that provides unified access to multiple AI models and third-party services including OpenAI, Anthropic Claude, Perplexity, Grok, Deepthink, and Notion integration.
- Multi-Provider AI Integration: Support for OpenAI, Anthropic Claude, Perplexity, Grok, and Deepthink
- Notion Integration: Read, write, and AI-powered processing of Notion pages
- Streaming Support: Real-time streaming responses for long AI outputs
- Authentication: JWT-based authentication with per-user API key management
- Rate Limiting: Built-in rate limiting and throttling
- Comprehensive Logging: Structured logging with Winston
- API Documentation: Auto-generated Swagger/OpenAPI documentation
- Error Handling: Global exception handling and validation
- Security: Helmet, CORS, and input validation
src/
βββ common/ # Shared utilities and DTOs
β βββ dto/ # Data Transfer Objects
β βββ filters/ # Global exception filters
β βββ interceptors/ # Logging and other interceptors
β βββ logger/ # Winston logger service
βββ modules/
β βββ auth/ # Authentication module
β βββ ai/ # AI providers integration
β β βββ providers/ # Individual AI service providers
β βββ notion/ # Notion API integration
β βββ health/ # Health check endpoints
βββ main.ts # Application bootstrap
- Clone and install dependencies:
cd ai-backend
npm install- Environment Setup:
cp env.example .env- Configure environment variables:
# Application Configuration
NODE_ENV=development
PORT=3501
API_PREFIX=api/v1
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here
JWT_EXPIRES_IN=24h
# AI Provider API Keys
OPENAI_API_KEY=your-openai-api-key
ANTHROPIC_API_KEY=your-anthropic-api-key
PERPLEXITY_API_KEY=your-perplexity-api-key
NOTION_API_KEY=your-notion-api-key
# Optional: Grok API (if available)
GROK_API_KEY=your-grok-api-key
GROK_API_URL=https://api.grok.com/v1
# Optional: Deepthink API (if available)
DEEPTHINK_API_KEY=your-deepthink-api-key
DEEPTHINK_API_URL=https://api.deepthink.ai/v1
# Rate Limiting
THROTTLE_TTL=60
THROTTLE_LIMIT=100
# CORS Configuration
CORS_ORIGIN=http://localhost:3501,http://localhost:5173
# Logging
LOG_LEVEL=info- Start the application:
# Development (runs on port 3501)
npm run start:dev
# Production (runs on port 6100)
npm run build
npm run start:prod# Development with Docker
docker-compose -f docker-compose.dev.yml up --build
# Production with Docker
docker-compose -f docker-compose.prod.yml up --build
# Or use the main docker-compose.yml
docker-compose up --buildOnce the server is running, visit:
- Swagger UI: http://localhost:3501/api/docs
- Health Check: http://localhost:3501/api/v1/health
curl -X POST http://localhost:3501/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123",
"name": "John Doe"
}'curl -X POST http://localhost:3501/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123"
}'curl -X POST http://localhost:3501/api/v1/auth/api-keys \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"name": "OpenAI Key",
"key": "sk-your-openai-key",
"provider": "openai"
}'curl -X POST http://localhost:3501/api/v1/ai/chat \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"provider": "openai",
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Hello, how are you?"
}
],
"maxTokens": 1000,
"temperature": 0.7
}'curl -X POST http://localhost:3501/api/v1/ai/chat/stream \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"provider": "anthropic",
"model": "claude-3-sonnet-20240229",
"messages": [
{
"role": "user",
"content": "Write a short story about a robot"
}
],
"stream": true
}'curl -X POST http://localhost:3501/api/v1/ai/embedding \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"provider": "openai",
"text": "This is a sample text for embedding",
"model": "text-embedding-ada-002"
}'curl -X POST http://localhost:3501/api/v1/notion/query \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"id": "your-notion-page-id",
"action": "read"
}'curl -X POST http://localhost:3501/api/v1/notion/summarize \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"pageId": "your-notion-page-id",
"provider": "openai",
"model": "gpt-4",
"length": "medium"
}'curl -X POST http://localhost:3501/api/v1/notion/ai-task \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"pageId": "your-notion-page-id",
"task": "extract key insights and create action items",
"provider": "anthropic",
"model": "claude-3-sonnet-20240229"
}'- Models: gpt-4, gpt-4-turbo, gpt-3.5-turbo, gpt-4o, gpt-4o-mini
- Features: Chat completion, embeddings, streaming
- API Key: Required
- Models: claude-3-opus, claude-3-sonnet, claude-3-haiku, claude-3-5-sonnet
- Features: Chat completion, streaming
- API Key: Required
- Models: llama-3.1-sonar variants
- Features: Chat completion, streaming, web search
- API Key: Required
- Models: grok-beta, grok-1
- Features: Chat completion, streaming
- API Key: Required (when available)
- Models: deepthink-1, deepthink-2
- Features: Chat completion, streaming
- API Key: Required (when available)
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:covFROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 6100
CMD ["node", "dist/main"]- Development: Port 3501
- Production: Port 6100
- Docker: Exposes both ports for flexibility
- Set
NODE_ENV=production - Use strong JWT secrets
- Configure proper CORS origins
- Set up proper logging levels
- Use environment-specific API keys
- API Keys: Store securely, never commit to version control
- JWT Secrets: Use strong, random secrets in production
- Rate Limiting: Configure appropriate limits for your use case
- CORS: Restrict origins to your frontend domains
- Input Validation: All inputs are validated using class-validator
- Error Handling: Sensitive information is not exposed in error messages
See the frontend-guides/ directory for specific integration examples:
- React integration guide
- Vue.js integration guide
- Cursor IDE usage examples
- Structured Logging: Winston with JSON format
- Request Logging: All requests logged with timing and status
- Error Tracking: Comprehensive error logging with stack traces
- Health Checks: Built-in health and readiness endpoints
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
For issues and questions:
- Check the API documentation at
/api/docs - Review the logs for error details
- Ensure all required environment variables are set
- Verify API keys are valid and have proper permissions