High-level REST API to interact with Google Gemini (Generative AI), with optional persistent conversation context, enhanced security, and live API documentation.
This repository is an opinionated starter for building serverless-friendly or containerized services that wrap a LLM (Gemini) with session/context support and production-ready middleware.
Highlights
- Optional persistent conversation context (MongoDB)
- Secure defaults: Helmet, CORS, rate limiting
- Interactive OpenAPI docs (Swagger UI)
- Chat-optimized endpoints and conversational helpers
- Input validation with celebrate/Joi (migrated from express-validator)
- Minimal external footprint to ease serverless deployments (includes pre-generated
public/swagger.json)
Table of contents
- Features
- Requirements
- Quick start
- Environment variables
- Scripts
- API endpoints (summary)
- Examples
- Security & operational notes
- Deployment
- Project structure
- Changelog
- Contributing
Features
- Context persistence: optional session storage (MongoDB) to keep conversation history up to a configured size.
- Rate limiting: general and route-level limits to reduce abuse.
- Input validation: migrate to
celebrate+Joifor robust request validation. - OpenAPI: static
public/swagger.jsonis included for serverless-friendly docs.
Requirements
- Node.js 16+
- Google Gemini API key (or configured equivalent)
- MongoDB (optional, only if you enable persistent sessions)
Quick start
- Clone the repository:
git clone "https://github.com/DANK1775/gemini-rest-api"
cd gemini-rest-api- Install dependencies:
npm install- Copy and edit environment variables:
copy .env.example .env
# then open .env and set values (GEMINIKEY, MONGODB_URI, etc.)- Run initialization script and start (development):
npm run devOr in production:
npm startScripts
npm run init— initialize runtime artifacts (data folders, sample files)npm run build:swagger— regeneratepublic/swagger.jsonfrom internal spec (kept static for serverless)npm run build— alias forbuild:swaggernpm run dev— run init + nodemon servernpm start— run init + production server
API Endpoints (summary)
Note: full OpenAPI specification available at /api-docs (Swagger UI) and shipped in public/swagger.json.
- GET / — basic info (legacy)
- GET /health — health check
- GET /api-docs — interactive Swagger UI
AI endpoints
- GET /api/ai?prompt=...&sessionId=...&useContext=... — generate response (query)
- POST /api/ai — generate response (JSON body)
Chat
- POST /api/chat — chat endpoint with optional sessionId to persist or resume context
Context management
- GET /api/context — list sessions (if persistence enabled)
- GET /api/context/:sessionId — get session context
- GET /api/context/:sessionId/stats — session stats
- DELETE /api/context/:sessionId — delete session context
Examples
Generate a response (POST):
curl -X POST http://localhost:3000/api/ai \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain reinforcement learning in simple terms",
"sessionId": "session_123",
"useContext": true
}'Send a chat message:
curl -X POST http://localhost:3000/api/chat \
-H "Content-Type: application/json" \
-d '{"message":"Hello","sessionId":"user_1"}'Security & operational notes
- Rate limiting defaults (configurable): general and specialized limits on AI routes.
- Helmet is enabled with CSP and other secure headers.
- CORS is configurable via environment variables.
- Input validation migrated from
express-validatortocelebrate+Joifor clearer schemas and better integration. - The repository includes a pre-generated
public/swagger.jsonto make static documentation available in serverless environments (Vercel, Netlify, etc.).
Environment variables
Set these in .env or in your deployment environment:
| VARIABLE | REQUIRED | DESCRIPTION | DEFAULT |
|---|---|---|---|
| GEMINIKEY | yes | Google Gemini API key or equivalent | - |
| PORT | no | Server port | 3000 |
| NODE_ENV | no | Node environment | development |
| MONGODB_URI | no | MongoDB connection string (if enabling persistence) | - |
| RATE_LIMIT_WINDOW_MS | no | Rate limiting window (ms) | 900000 |
| RATE_LIMIT_MAX_REQUESTS | no | Max requests per window | 100 |
| MAX_CONTEXT_MESSAGES | no | Max messages per session | 100 |
| TRUST_PROXY | no | Set to true if behind a proxy | false |
| CORS_ORIGIN | no | Comma-separated list of allowed origins | * |
Project structure
gemini-rest-api/
├── api/ # Production entrypoint and server
├── public/ # Static assets, includes swagger.json
├── src/
│ ├── config/ # config.js, swagger spec (static)
│ ├── middleware/ # error handling, rate limiting, validators
│ ├── routes/ # express routes (ai, chat, context)
│ ├── services/ # business logic (gemini & context)
│ └── scripts/ # init and swagger generation helpers
├── data/ # optional local json store
├── package.json
└── README.md
Deployment
Vercel
- Add environment variables in the Vercel dashboard (GEMINIKEY, MONGODB_URI, etc.)
- Deploy using Vercel CLI or GitHub integration. Static
public/swagger.jsonwill be served automatically.
Docker
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Local development
npm run devTesting & debugging
- The project has no automated tests configured by default. Add unit/integration tests for critical services.
- Use
NODE_ENV=developmentfor verbose logs (morgan) and easier debugging.
Changelog
- Replaced dynamic Swagger generation with a static OpenAPI spec and included
public/swagger.jsonto support serverless deployments. - Migrated request validation from
express-validatortocelebrate+Joifor clearer schemas and safer parsing. - Added celebrate error handler integration into the Express app to return well-formed validation errors.
- Removed
swagger-jsdocandexpress-validatorfrom dependencies; addedcelebrateandjoi. - Updated
package-lock.jsonand audited dependencies;npm auditreports no remaining vulnerabilities after the changes. - Updated
.gitignoreto allow committingpublic/swagger.jsonandpackage-lock.jsonfor reproducible installs and serverless docs.
- Initial feature set: optional context persistence, security middleware, Swagger docs, chat endpoints, rate limiting and error handling.
Contributing
- Fork the project
- Create a feature branch:
git checkout -b feature/YourFeature - Commit your changes and push
- Open a pull request and describe the changes
License
ISC
Author
DANK1775 — https://github.com/DANK1775