Spaces:
Configuration error
Configuration error
| # β Castor Price Forecasting API - Ready for Deployment | |
| ## π― Your API Key | |
| ``` | |
| castor_d167aa169b5e4219a66779e45fbaaefe | |
| ``` | |
| **Status:** β ACTIVE AND RUNNING | |
| --- | |
| ## π Server Information | |
| | Property | Value | | |
| |----------|-------| | |
| | **Server URL (Local)** | http://127.0.0.1:5000 | | |
| | **Server URL (Network)** | http://172.16.32.97:5000 | | |
| | **Port** | 5000 | | |
| | **Status** | β Running | | |
| | **Environment** | Python 3.12 + Flask | | |
| --- | |
| ## π Quick Start Examples | |
| ### 1. Health Check (No Auth Required) | |
| ```bash | |
| curl http://127.0.0.1:5000/api/health | |
| ``` | |
| **Response:** | |
| ```json | |
| { | |
| "status": "healthy", | |
| "timestamp": "2025-12-04T23:08:39", | |
| "service": "Castor Price Forecasting API" | |
| } | |
| ``` | |
| --- | |
| ### 2. Get Forecast (With Auth) | |
| ```bash | |
| curl -X POST http://127.0.0.1:5000/api/forecast \ | |
| -H "Content-Type: application/json" \ | |
| -H "X-API-Key: castor_d167aa169b5e4219a66779e45fbaaefe" \ | |
| -d '{ | |
| "product": "Castor", | |
| "start_date": "2025-12-01", | |
| "end_date": "2026-01-31" | |
| }' | |
| ``` | |
| --- | |
| ### 3. JavaScript Integration | |
| ```javascript | |
| const forecast = await fetch('http://127.0.0.1:5000/api/forecast', { | |
| method: 'POST', | |
| headers: { | |
| 'X-API-Key': 'castor_d167aa169b5e4219a66779e45fbaaefe', | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({ | |
| product: 'Castor', | |
| start_date: '2025-12-01', | |
| end_date: '2026-01-31' | |
| }) | |
| }); | |
| const data = await forecast.json(); | |
| console.log(data.forecast); | |
| ``` | |
| --- | |
| ### 4. Python Integration | |
| ```python | |
| import requests | |
| response = requests.post( | |
| 'http://127.0.0.1:5000/api/forecast', | |
| headers={ | |
| 'X-API-Key': 'castor_d167aa169b5e4219a66779e45fbaaefe' | |
| }, | |
| json={ | |
| 'product': 'Castor', | |
| 'start_date': '2025-12-01', | |
| 'end_date': '2026-01-31' | |
| } | |
| ) | |
| forecast_data = response.json() | |
| for item in forecast_data['forecast'][:5]: | |
| print(f"{item['date']}: ARIMA={item['arima_price']}, LSTM={item['lstm_price']}") | |
| ``` | |
| --- | |
| ## π Available Endpoints | |
| | Endpoint | Method | Auth Required | Description | | |
| |----------|--------|---------------|-------------| | |
| | `/` | GET | No | API Documentation | | |
| | `/api/health` | GET | No | Health Check | | |
| | `/api/generate-key` | POST | No | Generate New API Key | | |
| | `/api/forecast` | POST | Yes | Get Combined Forecast | | |
| | `/api/forecast/arima` | POST | Yes | Get ARIMA Forecast Only | | |
| | `/api/forecast/lstm` | POST | Yes | Get LSTM Forecast Only | | |
| --- | |
| ## π§ Deployment Options | |
| ### Option 1: Docker (Recommended for Production) | |
| ```bash | |
| docker build -t castor-api . | |
| docker run -p 5000:5000 castor-api | |
| ``` | |
| ### Option 2: Gunicorn (Production WSGI) | |
| ```bash | |
| pip install gunicorn | |
| gunicorn -w 4 -b 0.0.0.0:5000 api_production:app | |
| ``` | |
| ### Option 3: Direct Python (Development) | |
| ```bash | |
| python api_production.py | |
| ``` | |
| --- | |
| ## π¦ Files Included | |
| | File | Purpose | | |
| |------|---------| | |
| | `api_production.py` | Main API server | | |
| | `api_keys.json` | API key storage | | |
| | `DEPLOYMENT_GUIDE.md` | Complete deployment guide | | |
| | `API_CREDENTIALS.md` | Credentials reference | | |
| | `test_api_production.py` | API test suite | | |
| --- | |
| ## βοΈ API Key Management | |
| ### View Keys | |
| ```bash | |
| cat api_keys.json | |
| ``` | |
| ### Generate New Key | |
| ```bash | |
| curl -X POST http://127.0.0.1:5000/api/generate-key \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"name": "my_app"}' | |
| ``` | |
| ### Revoke Key | |
| Edit `api_keys.json` and set `"active": false` | |
| --- | |
| ## π‘οΈ Security Checklist | |
| - β API keys required for forecast endpoints | |
| - β CORS enabled for cross-origin requests | |
| - β Keys stored in JSON file (upgrade to database in production) | |
| - β Request counting and tracking enabled | |
| - β Error handling for invalid requests | |
| ### For Production: | |
| - [ ] Use HTTPS instead of HTTP | |
| - [ ] Implement database for key storage | |
| - [ ] Add rate limiting | |
| - [ ] Use environment variables for configuration | |
| - [ ] Implement request logging | |
| - [ ] Set up monitoring and alerts | |
| --- | |
| ## π§ͺ Testing the API | |
| ```bash | |
| python test_api_production.py | |
| ``` | |
| Expected output: | |
| ``` | |
| β Health: PASSED | |
| β Forecast: PASSED | |
| β ARIMA: PASSED | |
| β All tests passed! API is ready for deployment. | |
| ``` | |
| --- | |
| ## π API Response Example | |
| **Request:** | |
| ```json | |
| { | |
| "product": "Castor", | |
| "start_date": "2025-12-01", | |
| "end_date": "2025-12-03" | |
| } | |
| ``` | |
| **Response:** | |
| ```json | |
| { | |
| "status": "success", | |
| "product": "Castor", | |
| "last_known_price": 3856.50, | |
| "forecast_period": { | |
| "start": "2025-12-01", | |
| "end": "2025-12-03", | |
| "days": 3 | |
| }, | |
| "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 | |
| }, | |
| { | |
| "date": "2025-12-03", | |
| "arima_price": 3856.50, | |
| "lstm_price": 3857.24, | |
| "average_price": 3856.87 | |
| } | |
| ], | |
| "timestamp": "2025-12-04T23:08:39" | |
| } | |
| ``` | |
| --- | |
| ## π¨ Error Handling | |
| ### Unauthorized (401) | |
| ```json | |
| { | |
| "status": "error", | |
| "message": "Invalid or missing API key. Generate one using /api/generate-key" | |
| } | |
| ``` | |
| ### Not Found (404) | |
| ```json | |
| { | |
| "status": "error", | |
| "message": "Product not found in database" | |
| } | |
| ``` | |
| ### Bad Request (400) | |
| ```json | |
| { | |
| "status": "error", | |
| "message": "Invalid date format" | |
| } | |
| ``` | |
| --- | |
| ## π‘ Tips for App Developers | |
| 1. **Cache Results**: Store forecasts locally to reduce API calls | |
| 2. **Handle Errors**: Always check `status` field in response | |
| 3. **Set Timeouts**: Use 30-second request timeout | |
| 4. **Store Key Securely**: Use environment variables, not hardcoded | |
| 5. **Monitor Usage**: Track requests_count in api_keys.json | |
| --- | |
| ## π Support | |
| For issues or questions: | |
| 1. Check `DEPLOYMENT_GUIDE.md` for detailed information | |
| 2. Review API response for error messages | |
| 3. Test with `test_api_production.py` | |
| 4. Enable debug logging in `api_production.py` | |
| --- | |
| ## β¨ Next Steps | |
| 1. **Test Locally**: Use the examples above to verify the API | |
| 2. **Integrate into App**: Use the provided code samples | |
| 3. **Deploy to Production**: Follow `DEPLOYMENT_GUIDE.md` | |
| 4. **Monitor**: Track API usage and performance | |
| --- | |
| **API Status: β READY FOR PRODUCTION DEPLOYMENT** | |
| Generated: 2025-12-04 | |
| API Key: castor_d167aa169b5e4219a66779e45fbaaefe | |
| Server: Running on http://127.0.0.1:5000 | |