A Node.js Express API that integrates with Google's Gemini AI to generate content from text, images, documents, and audio files.
- Text Generation: Generate AI responses from text prompts
- Image Analysis: Upload images and get AI-generated descriptions or analysis
- Document Processing: Upload documents and get summaries or analysis
- Audio Transcription: Upload audio files and get transcriptions
- Node.js (v14 or higher)
- Google AI API Key (Gemini)
- Clone the repository:
git clone https://github.com/hanifalkauni/gemini-ai-api-project.git
cd gemini-flash-api- Install dependencies:
npm install- Set up environment variables:
cp .env.example .env- Add your Google AI API key to the
.envfile:
GEMINI_API_KEY=your_google_ai_api_key_here
PORT=3000
Start the server:
node index.jsThe server will run on http://localhost:3000 (or the port specified in your .env file).
POST /generate-text
Generate AI content from a text prompt.
Request Body:
{
"prompt": "Your text prompt here"
}Response:
{
"result": "AI generated response"
}POST /generate-from-image
Upload an image and get AI analysis or description.
Request:
- Content-Type:
multipart/form-data - Fields:
image: Image file (required)prompt: Text prompt for image analysis (optional)
Response:
{
"result": "AI analysis of the image"
}POST /generate-from-document
Upload a document and get AI summary or analysis.
Request:
- Content-Type:
multipart/form-data - Fields:
document: Document file (required)prompt: Text prompt for document analysis (optional)
Response:
{
"result": "AI summary or analysis of the document"
}POST /generate-from-audio
Upload an audio file and get transcription.
Request:
- Content-Type:
multipart/form-data - Fields:
audio: Audio file (required)prompt: Text prompt for audio processing (optional)
Response:
{
"result": "Audio transcription"
}Text Generation:
curl -X POST http://localhost:3000/generate-text \
-H "Content-Type: application/json" \
-d '{"prompt": "Explain quantum computing in simple terms"}'Image Analysis:
curl -X POST http://localhost:3000/generate-from-image \
-F "image=@path/to/your/image.jpg" \
-F "prompt=Describe this image"Document Processing:
curl -X POST http://localhost:3000/generate-from-document \
-F "document=@path/to/your/document.pdf" \
-F "prompt=Summarize this document"Audio Transcription:
curl -X POST http://localhost:3000/generate-from-audio \
-F "audio=@path/to/your/audio.mp3" \
-F "prompt=Transcribe this audio"- express: Web framework for Node.js
- multer: Middleware for handling multipart/form-data (file uploads)
- @google/genai: Google AI SDK for Gemini integration
- dotenv: Environment variable management
gemini-flash-api/
├── uploads/ # Temporary file upload directory
├── .env # Environment variables (not in repo)
├── .env.example # Environment variables template
├── .gitignore # Git ignore rules
├── index.js # Main application file
├── package.json # Project dependencies and scripts
├── package-lock.json # Dependency lock file
└── README.md # This file
The API includes error handling for:
- Invalid requests
- File upload errors
- Google AI API errors
- Server errors
All errors return a JSON response with an error message:
{
"error": "Error description"
}- Uploaded files are automatically deleted after processing
- Keep your Google AI API key secure and never commit it to version control
- The uploads directory is created automatically if it doesn't exist
ISC