# Castor Price Forecasting API - Deployment Guide ## Quick Start ### 1. Generate API Key ```bash python3 -c " import json, os, uuid, sys from datetime import datetime API_KEYS_FILE = 'api_keys.json' api_key = f'castor_{uuid.uuid4().hex[:32]}' api_keys = {} if os.path.exists(API_KEYS_FILE): with open(API_KEYS_FILE, 'r') as f: api_keys = json.load(f) api_keys[api_key] = { 'name': 'app_developer', 'created_at': datetime.now().isoformat(), 'last_used': None, 'requests_count': 0, 'active': True } with open(API_KEYS_FILE, 'w') as f: json.dump(api_keys, f, indent=2) print(f'API Key: {api_key}') print(f'Saved to {API_KEYS_FILE}') " ``` ### 2. Start the API Server ```bash # Using Python venv D:\models\arima\venv_short\Scripts\python.exe api_production.py # Or using direct Python python3 api_production.py ``` Server runs on: `http://0.0.0.0:5000` --- ## API Endpoints ### 1. Health Check **Endpoint:** `GET /api/health` **No authentication required** ```bash curl http://localhost:5000/api/health ``` **Response:** ```json { "status": "healthy", "timestamp": "2025-12-04T22:00:00", "service": "Castor Price Forecasting API" } ``` --- ### 2. Generate API Key **Endpoint:** `POST /api/generate-key` **No authentication required** ```bash curl -X POST http://localhost:5000/api/generate-key \ -H "Content-Type: application/json" \ -d '{"name":"your_app_name"}' ``` **Response:** ```json { "status": "success", "api_key": "castor_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6", "name": "your_app_name", "created_at": "2025-12-04T22:00:00", "message": "Use this API key in X-API-Key header for all requests" } ``` --- ### 3. Get Combined Forecast (ARIMA + LSTM) **Endpoint:** `POST /api/forecast` **Authentication:** Required (X-API-Key header) ```bash curl -X POST http://localhost:5000/api/forecast \ -H "Content-Type: application/json" \ -H "X-API-Key: castor_YOUR_API_KEY_HERE" \ -d '{ "product": "Castor", "start_date": "2025-12-01", "end_date": "2026-01-31" }' ``` **Response:** ```json { "status": "success", "product": "Castor", "last_known_price": 3856.50, "forecast_period": { "start": "2025-12-01", "end": "2026-01-31", "days": 62 }, "forecast": [ { "date": "2025-12-01", "arima_price": 3856.50, "lstm_price": 3856.54, "average_price": 3856.52 }, { "date": "2025-12-02", "arima_price": 3856.50, "lstm_price": 3856.89, "average_price": 3856.70 } ], "timestamp": "2025-12-04T22:00:00" } ``` --- ### 4. Get ARIMA Forecast Only **Endpoint:** `POST /api/forecast/arima` **Authentication:** Required ```bash curl -X POST http://localhost:5000/api/forecast/arima \ -H "Content-Type: application/json" \ -H "X-API-Key: castor_YOUR_API_KEY_HERE" \ -d '{ "product": "Castor", "start_date": "2025-12-01", "end_date": "2026-01-31" }' ``` **Response:** ```json { "status": "success", "model": "ARIMA", "product": "Castor", "forecast": [ { "date": "2025-12-01", "price": 3856.50 } ] } ``` --- ### 5. Get LSTM Forecast Only **Endpoint:** `POST /api/forecast/lstm` **Authentication:** Required ```bash curl -X POST http://localhost:5000/api/forecast/lstm \ -H "Content-Type: application/json" \ -H "X-API-Key: castor_YOUR_API_KEY_HERE" \ -d '{ "product": "Castor", "start_date": "2025-12-01", "end_date": "2026-01-31" }' ``` --- ## Integration Examples ### JavaScript/Node.js ```javascript const API_KEY = 'castor_your_api_key_here'; const API_URL = 'http://localhost:5000/api/forecast'; async function getForecast() { const response = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': API_KEY }, body: JSON.stringify({ product: 'Castor', start_date: '2025-12-01', end_date: '2026-01-31' }) }); const data = await response.json(); console.log(data); } getForecast(); ``` ### Python ```python import requests API_KEY = 'castor_your_api_key_here' API_URL = 'http://localhost:5000/api/forecast' response = requests.post( API_URL, headers={ 'X-API-Key': API_KEY, 'Content-Type': 'application/json' }, json={ 'product': 'Castor', 'start_date': '2025-12-01', 'end_date': '2026-01-31' } ) forecast = response.json() print(forecast) ``` ### React/Frontend ```jsx import React, { useState } from 'react'; export function ForecastComponent() { const [forecast, setForecast] = useState(null); const [loading, setLoading] = useState(false); const fetchForecast = async () => { setLoading(true); try { const response = await fetch('http://localhost:5000/api/forecast', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.REACT_APP_CASTOR_API_KEY }, body: JSON.stringify({ product: 'Castor', start_date: '2025-12-01', end_date: '2026-01-31' }) }); const data = await response.json(); setForecast(data.forecast); } catch (error) { console.error('Error:', error); } finally { setLoading(false); } }; return (
| Date | ARIMA Price | LSTM Price | Average |
|---|---|---|---|
| {row.date} | {row.arima_price} | {row.lstm_price} | {row.average_price} |