π Trade Opportunities API π Overview
This project implements a FastAPI service that analyzes market data for a given sector in India and generates a Markdown report of trade opportunities using Google Gemini AI.
It was built as part of the Trade Opportunities API Developer Task.
βοΈ Features
β FastAPI backend with async support
β Single endpoint: GET /analyze/{sector}
β Collects recent market data from DuckDuckGo-Search
β Uses Google Gemini API to analyze and generate structured Markdown reports
β Built-in rate limiting (5 requests per minute per IP)
β Clean error handling (502 for data issues, 503 for AI issues)
β Returns Markdown report wrapped in JSON
π Project Structure trade_api/ βββ main.py # FastAPI app (endpoint + rate limiting + data fetch) βββ requirements.txt # Dependencies βββ README.md # This file
π Setup & Run
-
Clone & Install git clone cd trade_api pip install -r requirements.txt
-
Set Gemini API Key
You need a Google Gemini API key (from Google AI Studio or Google Cloud). Set it as an environment variable:
export GEMINI_API_KEY="your_api_key_here"
(Windows PowerShell)
$env:GEMINI_API_KEY="your_api_key_here"
- Run FastAPI uvicorn main:app --reload --host 127.0.0.1 --port 8000
π‘ API Usage Endpoint GET /analyze/{sector}
Example Request GET http://127.0.0.1:8000/analyze/pharmaceuticals
Example Response { "report": "# Market Analysis β Pharmaceuticals\n\n- Key Trends...\n- Opportunities...\n- Risks...\n- Future Outlook...\n\nGenerated by AI" }
π Security
Open access (no authentication required).
Rate limiting: 5 requests/minute per IP to prevent abuse.
502 β Market data fetch failed
503 β AI service unavailable
β Success Criteria
API accepts sector names (e.g., pharmaceuticals, technology, agriculture)
Returns structured Markdown report containing trade opportunities
Implements rate limiting and proper error handling
Clean, well-organized code with separation of concerns
π Notes
The data collection currently uses DuckDuckGo API for simplicity. In a production system, this can be replaced with a more robust data source (e.g., NewsAPI, scraping, financial APIs).
Gemini model used: gemini-2.5-flash (fast & lightweight).
β¨ Youβre all set! Run the server, test in Postman, and save the returned Markdown as .md files for reports.