File size: 6,526 Bytes
d13574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# βœ… 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