A REST API backend built with Node.js and Express to interact with the Google Gemini large language model (LLM).
|
Node.js |
Express.js |
Gemini AI |
- Direct Google Gemini Integration – Leverages Gemini's generative capabilities via official API.
- Real-Time UTC Context – Injects live time/date data into prompts when needed.
- API Rate Limiting – Uses
express-rate-limitto prevent abuse. - Modular Structure – Clean separation of concerns (routes, controllers, services, middleware).
This backend uses Google's gemini-1.5-flash – a fast, multimodal model ideal for low-latency, cost-effective tasks.
Follow these instructions to run the project locally.
- Node.js (v18.x or newer)
- NPM (comes with Node.js)
- Google Gemini API Key from Google AI Studio
- Clone the repository
git clone https://github.com/your-username/gemini-api-backend.git
cd gemini-api-backend- Install dependencies
npm install- Create environment file
Create a .env file in the root directory:
GEMINI_API_KEY=your_google_api_key_here- Run the development server
npm run devSend a question to Gemini and receive a response.
Send a JSON object containing a question field:
{
"question": "What are the main benefits of using Node.js for a backend?"
}A successful response returns a JSON object with the AI-generated answer:
{
"answer": "Node.js is great for backends because it's non-blocking and event-driven, making it highly efficient for handling many simultaneous connections. Its large npm ecosystem provides free packages for almost any functionality you can imagine, and using JavaScript on both the front-end and back-end simplifies development."
}├── config/ # Environment variables & configuration
├── controllers/ # Request handling logic
├── middleware/ # Custom Express middlewares (rate limiting, etc.)
├── prompts/ # Base prompt templates
├── routes/ # API route definitions
├── services/ # External API integrations (Gemini)
└── server.js # App entry pointSuvojit Modak