AI-powered chat application backend built with FastAPI and Google Gemini.
- Python 3.9+
- Google Gemini API key
- Firebase project with Admin SDK credentials
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On macOS/Linux # or venv\Scripts\activate # On Windows
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env
Edit
.envand add your credentials:GEMINI_API_KEY: Get from Google AI StudioFIREBASE_CREDENTIALS_PATH: Path to your Firebase Admin SDK JSON file
-
Add Firebase credentials:
- Go to Firebase Console β Project Settings β Service Accounts
- Generate new private key
- Save as
firebase-credentials.jsonin the backend root folder
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
GET /- Root endpointGET /api/health- Health checkGET /api/chat/test- Test chat endpoint
POST /api/chat/message- Send message to AI
curl -X POST http://localhost:8000/api/chat/message \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_FIREBASE_ID_TOKEN" \
-d '{"message": "Hello, how are you?"}'backend/
βββ app/
β βββ api/ # API route handlers
β βββ middleware/ # Authentication middleware
β βββ models/ # Pydantic schemas
β βββ services/ # Business logic
β βββ config.py # Configuration
β βββ dependencies.py # Dependency injection
β βββ main.py # FastAPI app entry point
βββ requirements.txt
βββ .env.example
βββ README.md
This API uses Firebase Authentication. All protected endpoints require a valid Firebase ID token in the Authorization header:
Authorization: Bearer <firebase_id_token>
Test the API without authentication:
curl http://localhost:8000/api/chat/test| Variable | Description | Required |
|---|---|---|
GEMINI_API_KEY |
Google Gemini API key | Yes |
FIREBASE_CREDENTIALS_PATH |
Path to Firebase Admin SDK JSON | Yes |
CORS_ORIGINS |
Allowed CORS origins (comma-separated) | Yes |
DEBUG |
Enable debug mode | No |
This project follows PEP 8 guidelines. Format code with:
black app/mypy app/MIT License