File size: 6,311 Bytes
eebf5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Crypto API Monitor Backend
emoji: πŸ“Š
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
---

# Crypto API Monitor Backend

Real-time cryptocurrency API monitoring backend service built with FastAPI.

## Features

- **Real-time Health Monitoring**: Automatically monitors 11+ cryptocurrency API providers every 5 minutes
- **WebSocket Support**: Live updates for frontend dashboard integration
- **REST API**: Comprehensive endpoints for status, logs, categories, and analytics
- **SQLite Database**: Persistent storage for connection logs, metrics, and configuration
- **Rate Limit Tracking**: Monitor API usage and rate limits per provider
- **Connection Logging**: Track all API requests with response times and error details
- **Authentication**: Token-based authentication and IP whitelist support

## API Providers Monitored

### Market Data
- CoinGecko (free)
- CoinMarketCap (requires API key)
- CryptoCompare (requires API key)
- Binance (free)

### Blockchain Explorers
- Etherscan (requires API key)
- BscScan (requires API key)
- TronScan (requires API key)

### News & Sentiment
- CryptoPanic (free)
- NewsAPI (requires API key)
- Alternative.me Fear & Greed (free)

### On-chain Analytics
- The Graph (free)
- Blockchair (free)

## API Documentation

Visit `/docs` for interactive API documentation (Swagger UI).
Visit `/redoc` for alternative API documentation (ReDoc).

## Main Endpoints

### Status & Monitoring
- `GET /api/status` - Overall system status
- `GET /api/categories` - Category statistics
- `GET /api/providers` - List all providers with filters
- `GET /api/logs` - Connection logs with pagination
- `GET /api/failures` - Failure analysis
- `GET /api/rate-limits` - Rate limit status

### Configuration
- `GET /api/config/keys` - API key configuration
- `GET /api/schedule` - Schedule configuration
- `POST /api/schedule/trigger` - Manually trigger scheduled task

### Analytics
- `GET /api/charts/health-history` - Health history for charts
- `GET /api/charts/compliance` - Compliance chart data
- `GET /api/freshness` - Data freshness status

### WebSocket
- `WS /ws/live` - Real-time updates

## Environment Variables

Create a `.env` file or set environment variables:

```bash
# Optional: API authentication tokens (comma-separated)
API_TOKENS=token1,token2

# Optional: IP whitelist (comma-separated)
ALLOWED_IPS=192.168.1.1,10.0.0.1

# Optional: Database URL (default: sqlite:///./crypto_monitor.db)
DATABASE_URL=sqlite:///./crypto_monitor.db

# Optional: Server port (default: 7860)
PORT=7860
```

## Deployment to Hugging Face Spaces

### Option 1: Docker SDK

1. Create a new Hugging Face Space
2. Select **Docker** SDK
3. Push this repository to GitHub
4. Connect the GitHub repository to your Space
5. Add environment variables in Space settings:
   - `API_TOKENS=your_secret_token_here`
   - `ALLOWED_IPS=` (optional, leave empty for no restriction)
6. The Space will automatically build and deploy

### Option 2: Local Docker

```bash
# Build Docker image
docker build -t crypto-api-monitor .

# Run container
docker run -p 7860:7860 \
  -e API_TOKENS=your_token_here \
  crypto-api-monitor
```

## Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Run the application
python app.py

# Or with uvicorn
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
```

Visit `http://localhost:7860` to access the API.
Visit `http://localhost:7860/docs` for interactive documentation.

## Database Schema

The application uses SQLite with the following tables:

- **providers**: API provider configurations
- **connection_attempts**: Log of all API connection attempts
- **data_collections**: Data collection records
- **rate_limit_usage**: Rate limit tracking
- **schedule_config**: Scheduled task configuration

## WebSocket Protocol

Connect to `ws://localhost:7860/ws/live` for real-time updates.

### Message Types

**Status Update**
```json
{
  "type": "status_update",
  "data": {
    "total_apis": 11,
    "online": 10,
    "degraded": 1,
    "offline": 0
  }
}
```

**New Log Entry**
```json
{
  "type": "new_log_entry",
  "data": {
    "timestamp": "2025-11-11T00:00:00",
    "provider": "CoinGecko",
    "status": "success",
    "response_time_ms": 120
  }
}
```

**Rate Limit Alert**
```json
{
  "type": "rate_limit_alert",
  "data": {
    "provider": "CoinMarketCap",
    "usage_percentage": 85
  }
}
```

## Frontend Integration

Update your frontend dashboard configuration:

```javascript
// config.js
const config = {
  apiBaseUrl: 'https://YOUR_USERNAME-crypto-api-monitor.hf.space',
  wsUrl: 'wss://YOUR_USERNAME-crypto-api-monitor.hf.space/ws/live',
  authToken: 'your_token_here' // Optional
};
```

## Architecture

```
app.py                          # FastAPI application entry point
config.py                       # Configuration & API registry loader
database/
  β”œβ”€β”€ db.py                     # Database initialization
  └── models.py                 # SQLAlchemy models
monitoring/
  └── health_monitor.py         # Background health monitoring
api/
  β”œβ”€β”€ endpoints.py              # REST API endpoints
  β”œβ”€β”€ websocket.py              # WebSocket handler
  └── auth.py                   # Authentication
utils/
  β”œβ”€β”€ http_client.py            # Async HTTP client with retry
  β”œβ”€β”€ logger.py                 # Structured logging
  └── validators.py             # Input validation
```

## API Keys

API keys are loaded from `all_apis_merged_2025.json` in the `discovered_keys` section:

```json
{
  "discovered_keys": {
    "etherscan": ["key1", "key2"],
    "bscscan": ["key1"],
    "coinmarketcap": ["key1", "key2"],
    ...
  }
}
```

## Performance

- Health checks run every 5 minutes
- Response time tracking for all providers
- Automatic retry with exponential backoff
- Connection timeout: 10 seconds
- Database queries optimized with indexes

## Security

- Optional token-based authentication
- IP whitelist support
- API keys masked in logs and responses
- CORS enabled for frontend access
- SQL injection protection via SQLAlchemy ORM

## License

MIT License

## Author

**Nima Zasinich**
- GitHub: [@nimazasinich](https://github.com/nimazasinich)
- Project: Crypto API Monitor Backend

---

**Built for the crypto dev community**