A Node.js backend API that integrates Google Gemini and OpenAI ChatGPT APIs. The API receives a prompt, sends it to Gemini, then combines the response with a configurable prepend prompt and sends it to ChatGPT for final processing.
- Bearer Token Authentication: Protected endpoints with secure token validation
- Input Validation: Zod schema validation for all requests
- Rate Limiting: Built-in rate limiting to prevent abuse
- Security: Helmet.js for security headers and CORS support
- Error Handling: Comprehensive error handling and logging
- Environment Configuration: Flexible configuration via environment variables
- Receive POST request with prompt (protected by bearer token)
- Send prompt to Google Gemini API
- Take Gemini's response and prepend a configurable prompt
- Send combined prompt/response to OpenAI ChatGPT API
- Return ChatGPT's final response to the client
- Node.js (v16 or higher) OR Docker
- npm or yarn (if not using Docker)
- Google Gemini API key
- OpenAI API key
- Clone the repository:
git clone <repository-url>
cd tav-backend- Install dependencies:
npm install- Copy the environment example file:
cp env.example .env- Configure your environment variables in
.env:
# Server Configuration
PORT=3000
NODE_ENV=development
# Authentication
BEARER_TOKEN=your-secure-bearer-token-here
# Google Gemini API
GEMINI_API_KEY=your-gemini-api-key-here
GEMINI_MODEL=gemini-pro
# OpenAI ChatGPT API
OPENAI_API_KEY=your-openai-api-key-here
OPENAI_MODEL=gpt-3.5-turbo
# Configuration
PREPEND_PROMPT=Please analyze and improve the following response:
# API Configuration
OPENAI_MAX_TOKENS=2000
OPENAI_TEMPERATURE=0.7Development mode:
npm run devProduction mode:
npm startProduction with Docker:
docker-compose up --buildDevelopment with Docker (hot reloading):
docker-compose -f docker-compose.dev.yml up --buildThe server will start on port 3000 (or the port specified in your .env file).
For detailed Docker instructions, see DOCKER.md.
For VPS deployment instructions, see VPS_DEPLOYMENT.md.
GET /health
Returns server status.
Response:
{
"status": "OK",
"timestamp": "2024-01-01T12:00:00.000Z"
}POST /api/prompt
Process a prompt through Gemini and ChatGPT.
Headers:
Authorization: Bearer your-bearer-token
Content-Type: application/json
Request Body:
{
"prompt": "Your prompt here"
}Response:
{
"response": "ChatGPT's final response",
"geminiResponse": "Gemini's original response",
"processingTime": 1500
}Error Responses:
400 Bad Request: Invalid request body or validation errors401 Unauthorized: Missing or invalid bearer token429 Too Many Requests: Rate limit exceeded500 Internal Server Error: Server or API errors
| Variable | Description | Required | Default |
|---|---|---|---|
PORT |
Server port | No | 3000 |
NODE_ENV |
Environment (development/production) | No | development |
BEARER_TOKEN |
Authentication token | Yes | - |
GEMINI_API_KEY |
Google Gemini API key | Yes | - |
GEMINI_MODEL |
Gemini model to use | No | gemini-pro |
OPENAI_API_KEY |
OpenAI API key | Yes | - |
OPENAI_MODEL |
OpenAI model to use | No | gpt-3.5-turbo |
OPENAI_MAX_TOKENS |
Maximum tokens for OpenAI responses | No | 2000 |
OPENAI_TEMPERATURE |
Temperature for OpenAI responses (0-2) | No | 0.7 |
PREPEND_PROMPT |
Text to prepend to Gemini response | No | "Please analyze and improve the following response:" |
- Bearer Token Authentication: All API endpoints are protected
- Rate Limiting: 100 requests per 15 minutes per IP
- Security Headers: Helmet.js for security headers
- CORS: Configurable CORS support
- Input Validation: Zod schema validation
- Error Sanitization: Errors don't expose sensitive information in production
The API includes comprehensive error handling:
- Input validation errors (400)
- Authentication errors (401)
- Rate limiting (429)
- API service errors (500)
- Generic error responses with appropriate status codes
src/
├── index.js # Main application entry point
├── middleware/
│ └── auth.js # Authentication middleware
├── routes/
│ └── prompt.js # Prompt processing routes
├── schemas/
│ └── prompt.js # Zod validation schemas
├── services/
│ ├── gemini.js # Gemini API service
│ └── chatgpt.js # ChatGPT API service
└── utils/
└── config.js # Configuration validation
# Docker files
Dockerfile # Production Docker image
Dockerfile.dev # Development Docker image
docker-compose.yml # Production Docker Compose
docker-compose.dev.yml # Development Docker Compose
.dockerignore # Docker ignore rules
Run tests:
npm testThe application logs important events:
- API requests and responses
- Error details
- Processing times
- Authentication attempts
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License