ojas / API_README.md
AgentCrafter's picture
all commits
839f813 verified
|
Raw
History Blame Contribute Delete
3.75 kB

Castor Price Forecasting API

A REST API for accessing ARIMA and LSTM forecasting models for Castor oil prices.

Getting Started

1. Generate an API Key

curl -X POST http://localhost:5000/api/keys/generate \
  -H "Content-Type: application/json" \
  -d '{"name": "my-app"}'

Response:

{
  "status": "success",
  "api_key": "castor_api_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6",
  "message": "API key generated: castor_api_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
}

2. Use Your API Key

Include the API key in the X-API-Key header for all requests:

curl -X POST http://localhost:5000/api/forecast/arima \
  -H "X-API-Key: castor_api_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6" \
  -H "Content-Type: application/json" \
  -d '{
    "product": "Castor",
    "start_date": "2025-12-01",
    "end_date": "2026-12-31"
  }'

API Endpoints

Health Check

GET /api/health

Check if the API is running.

Key Management

Generate API Key

POST /api/keys/generate
Body: {"name": "my-app"}

List All Keys

GET /api/keys/list

Revoke API Key

POST /api/keys/revoke
Body: {"api_key": "castor_api_..."}

Forecasting

ARIMA Forecast

POST /api/forecast/arima
Headers: X-API-Key: YOUR_KEY
Body: {
  "product": "Castor",
  "start_date": "2025-12-01",
  "end_date": "2026-12-31"
}

LSTM Forecast

POST /api/forecast/lstm
Headers: X-API-Key: YOUR_KEY
Body: {
  "product": "Castor",
  "start_date": "2025-12-01",
  "end_date": "2026-12-31"
}

Compare Models

POST /api/forecast/compare
Headers: X-API-Key: YOUR_KEY
Body: {
  "product": "Castor",
  "start_date": "2025-12-01",
  "end_date": "2026-12-31"
}

Example Usage (Python)

import requests

# Generate API Key
response = requests.post('http://localhost:5000/api/keys/generate', 
  json={'name': 'my-app'})
api_key = response.json()['api_key']

# Get forecast
headers = {'X-API-Key': api_key}
payload = {
  'product': 'Castor',
  'start_date': '2025-12-01',
  'end_date': '2026-12-31'
}

forecast = requests.post('http://localhost:5000/api/forecast/arima',
  json=payload, headers=headers)
print(forecast.json())

Running the API Server

# Using the virtual environment
D:\models\arima\venv_short\Scripts\python.exe api_server.py

The server will start at http://localhost:5000

API Response Format

Success Response

{
  "status": "success",
  "product": "Castor",
  "model": "ARIMA",
  "forecast": {
    "2025-12-01": 3500.5,
    "2025-12-02": 3502.3,
    ...
  },
  "start_date": "2025-12-01",
  "end_date": "2026-12-31"
}

Error Response

{
  "status": "error",
  "message": "Invalid or missing API key"
}

API Key Security

  • Each API key is tracked with:

    • Creation timestamp
    • Last used timestamp
    • Request count
    • Custom name/identifier
  • Keys are stored in api_keys.json

  • In production, use a proper database with encryption

  • Revoke keys when no longer needed

Features

βœ… API Key-based authentication
βœ… ARIMA forecasting
βœ… LSTM forecasting
βœ… Model comparison
βœ… Request tracking
βœ… Multiple products support
βœ… Flexible date ranges
βœ… Cross-Origin Resource Sharing (CORS)

Troubleshooting

"Invalid or missing API key": Ensure you're including the correct API key in the X-API-Key header.

"Product not found": Check that the product name matches the data in your CSV file.

Server not responding: Make sure the Flask server is running: python api_server.py