Skip to content

exiast/gemini-rest-api

Repository files navigation

Gemini REST API

A clean, performant REST API for Google's Gemini AI model that supports text generation, image analysis, document processing, and audio analysis.

🚀 Features

  • Text Generation: Generate content from text prompts
  • Image Analysis: Analyze and describe images
  • Document Processing: Extract and analyze document content
  • Audio Analysis: Process and transcribe audio files
  • Health Monitoring: Built-in health check endpoint
  • File Validation: Secure file type and size validation
  • Error Handling: Comprehensive error handling and recovery

📋 Prerequisites

  • Node.js (v16 or higher)
  • Google Gemini API key

🛠️ Installation

  1. Clone the repository
  2. Install dependencies:
    npm install
  3. Copy the environment template:
    cp .env.example .env
  4. Edit .env and add your Gemini API key:
    Gemini_API=your_actual_api_key_here
    

🏃 Running the Application

Development Mode

npm run dev

Production Mode

npm start

The server will start on port 3000 (or the port specified in your .env file).

📡 API Endpoints

Health Check

GET /health

Text Generation

POST /generate-text
Content-Type: application/json

{
  "prompt": "Your text prompt here"
}

Image Analysis

POST /generate-from-image
Content-Type: multipart/form-data

image: [image file]
prompt: "Optional custom prompt" (default: "Analyze this image:")

Document Processing

POST /generate-from-document
Content-Type: multipart/form-data

document: [document file]
prompt: "Optional custom prompt" (default: "Analyze this document:")

Audio Analysis

POST /generate-from-audio
Content-Type: multipart/form-data

audio: [audio file]
prompt: "Optional custom prompt" (default: "Analyze this audio:")

📊 Response Format

All endpoints return responses in the following format:

Success Response

{
  "success": true,
  "output": "Generated content here..."
}

Error Response

{
  "success": false,
  "error": "Error message",
  "details": ["Additional error details if applicable"]
}

🛡️ Security & Limits

  • File Size Limit: 50MB maximum
  • Request Body Limit: 10MB maximum
  • Supported Image Types: All standard image formats
  • Supported Document Types: PDF, DOC, DOCX, TXT
  • Supported Audio Types: All standard audio formats

🔧 Configuration

Environment variables:

Variable Required Default Description
Gemini_API Yes - Your Google Gemini API key
PORT No 3000 Server port number

📝 Recent Refactoring

This codebase has been recently refactored for improved:

  • Performance: Better memory management and file handling
  • Security: Input validation and file type restrictions
  • Maintainability: Modular code structure and error handling
  • Standards: Following JavaScript best practices and linting rules

See REFACTOR_SUMMARY.md for detailed information about the improvements.

🐛 Error Handling

The API includes comprehensive error handling for:

  • Invalid file types
  • Missing required fields
  • File upload failures
  • API rate limits
  • Network timeouts
  • Invalid prompts

📈 Monitoring

Use the /health endpoint to monitor server status:

curl http://localhost:3000/health

🤝 Contributing

  1. Follow the existing code style
  2. Add appropriate error handling
  3. Update documentation as needed
  4. Test all endpoints before submitting

📄 License

ISC License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors