File size: 13,389 Bytes
b190b45 |
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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# API Usage Guide - How to Use the Crypto Monitor Services
## راهنمای استفاده از API - چگونه از سرویسهای کریپتو مانیتور استفاده کنیم
---
## English Guide
### Overview
This application provides cryptocurrency monitoring services through a web interface and backend APIs. Users can access real-time crypto prices, news, and market data.
### Architecture
```
┌─────────────────┐
│ User/Browser │
└────────┬────────┘
│ HTTP Requests
▼
┌─────────────────┐
│ Frontend (UI) │
│ - HTML/CSS/JS │
│ - React/Vue │
└────────┬────────┘
│ API Calls
▼
┌─────────────────┐
│ Backend Server │
│ - Node.js/Py │
│ - API Routes │
└────────┬────────┘
│
├─────────────────┐
▼ ▼
┌─────────────┐ ┌──────────────┐
│ News API │ │ Crypto APIs │
│ External │ │ CoinGecko │
└─────────────┘ └──────────────┘
```
### How to Use the Services
#### 1. **News Service**
**Access Method**: Web Browser
- Navigate to: `http://localhost:PORT/static/pages/news/index.html`
- The page automatically loads latest cryptocurrency news
**JavaScript API Usage**:
```javascript
// The news page uses this internally
const newsPage = new NewsPage();
await newsPage.loadNews();
// Get filtered articles
newsPage.currentFilters.keyword = 'bitcoin';
newsPage.applyFilters();
```
**Configuration**:
```javascript
// Edit news-config.js
export const NEWS_CONFIG = {
apiKey: 'YOUR_API_KEY',
defaultQuery: 'cryptocurrency OR bitcoin',
pageSize: 100
};
```
#### 2. **Backend API Endpoints**
**News Endpoint**:
```http
GET /api/news
```
**Query Parameters**:
- `source`: Filter by news source
- `sentiment`: Filter by sentiment (positive/negative/neutral)
- `limit`: Number of articles (default: 100)
**Example Request**:
```bash
# Using curl
curl "http://localhost:3000/api/news?limit=50&sentiment=positive"
# Using JavaScript fetch
fetch('/api/news?limit=50')
.then(response => response.json())
.then(data => console.log(data.articles));
# Using Python requests
import requests
response = requests.get('http://localhost:3000/api/news?limit=50')
articles = response.json()['articles']
```
**Response Format**:
```json
{
"articles": [
{
"title": "Bitcoin Reaches New High",
"content": "Article description...",
"source": {
"title": "CryptoNews"
},
"published_at": "2025-11-30T10:00:00Z",
"url": "https://example.com/article",
"sentiment": "positive",
"category": "market"
}
],
"total": 50,
"fallback": false
}
```
#### 3. **Cryptocurrency Data Endpoints**
**Get Crypto Prices**:
```http
GET /api/crypto/prices
```
**Example**:
```bash
curl "http://localhost:3000/api/crypto/prices?symbols=BTC,ETH,ADA"
```
**Get Market Data**:
```http
GET /api/crypto/market
```
**Get Historical Data**:
```http
GET /api/crypto/history?symbol=BTC&days=30
```
### Client-Side Integration
#### HTML Page
```html
<!DOCTYPE html>
<html>
<head>
<title>Crypto Monitor</title>
</head>
<body>
<div id="news-container"></div>
<script type="module">
// Load news dynamically
async function loadNews() {
const response = await fetch('/api/news?limit=10');
const data = await response.json();
const container = document.getElementById('news-container');
container.innerHTML = data.articles.map(article => `
<div class="news-card">
<h3>${article.title}</h3>
<p>${article.content}</p>
<a href="${article.url}">Read more</a>
</div>
`).join('');
}
loadNews();
</script>
</body>
</html>
```
#### React Component
```jsx
import { useState, useEffect } from 'react';
function NewsComponent() {
const [articles, setArticles] = useState([]);
useEffect(() => {
fetch('/api/news?limit=20')
.then(res => res.json())
.then(data => setArticles(data.articles));
}, []);
return (
<div>
{articles.map(article => (
<div key={article.url}>
<h3>{article.title}</h3>
<p>{article.content}</p>
</div>
))}
</div>
);
}
```
#### Vue Component
```vue
<template>
<div>
<div v-for="article in articles" :key="article.url">
<h3>{{ article.title }}</h3>
<p>{{ article.content }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return { articles: [] };
},
async mounted() {
const response = await fetch('/api/news?limit=20');
const data = await response.json();
this.articles = data.articles;
}
}
</script>
```
### Error Handling
**Handle API Errors**:
```javascript
async function fetchNewsWithErrorHandling() {
try {
const response = await fetch('/api/news');
if (!response.ok) {
if (response.status === 401) {
throw new Error('Authentication failed');
} else if (response.status === 429) {
throw new Error('Too many requests');
} else if (response.status === 500) {
throw new Error('Server error');
}
}
const data = await response.json();
return data.articles;
} catch (error) {
console.error('Error fetching news:', error);
// Show user-friendly error message
alert(`Failed to load news: ${error.message}`);
return [];
}
}
```
### Rate Limiting
**API Limits**:
- News API: 100 requests/day (free tier)
- Backend API: Configurable (default: 1000 requests/hour)
**Handle Rate Limits**:
```javascript
// Implement caching
const cache = new Map();
const CACHE_TTL = 60000; // 1 minute
async function fetchWithCache(url) {
const cached = cache.get(url);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.data;
}
const response = await fetch(url);
const data = await response.json();
cache.set(url, {
data,
timestamp: Date.now()
});
return data;
}
```
### WebSocket Integration (Real-time Updates)
```javascript
// Connect to WebSocket for real-time crypto prices
const ws = new WebSocket('ws://localhost:3000/ws/crypto');
ws.onopen = () => {
console.log('Connected to crypto feed');
// Subscribe to specific coins
ws.send(JSON.stringify({
action: 'subscribe',
symbols: ['BTC', 'ETH', 'ADA']
}));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Price update:', data);
// Update UI with new prices
updatePriceDisplay(data);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('Disconnected from crypto feed');
// Attempt reconnection
setTimeout(connectWebSocket, 5000);
};
```
---
## راهنمای فارسی
### نحوه استفاده از سرویسها
#### ۱. **سرویس اخبار**
**روش دسترسی**: مرورگر وب
- آدرس: `http://localhost:PORT/static/pages/news/index.html`
- صفحه به صورت خودکار آخرین اخبار ارز دیجیتال را بارگذاری میکند
**استفاده از API در جاوااسکریپت**:
```javascript
// صفحه اخبار از این کد استفاده میکند
const newsPage = new NewsPage();
await newsPage.loadNews();
// فیلتر کردن مقالات
newsPage.currentFilters.keyword = 'bitcoin';
newsPage.applyFilters();
```
#### ۲. **نقاط پایانی API سرور**
**دریافت اخبار**:
```http
GET /api/news
```
**پارامترهای درخواست**:
- `source`: فیلتر بر اساس منبع خبر
- `sentiment`: فیلتر بر اساس احساسات (مثبت/منفی/خنثی)
- `limit`: تعداد مقالات (پیشفرض: ۱۰۰)
**مثال درخواست**:
```bash
# استفاده از curl
curl "http://localhost:3000/api/news?limit=50&sentiment=positive"
# استفاده از fetch در جاوااسکریپت
fetch('/api/news?limit=50')
.then(response => response.json())
.then(data => console.log(data.articles));
# استفاده از Python
import requests
response = requests.get('http://localhost:3000/api/news?limit=50')
articles = response.json()['articles']
```
**فرمت پاسخ**:
```json
{
"articles": [
{
"title": "بیتکوین به رکورد جدید رسید",
"content": "توضیحات مقاله...",
"source": {
"title": "اخبار کریپتو"
},
"published_at": "2025-11-30T10:00:00Z",
"url": "https://example.com/article",
"sentiment": "positive"
}
],
"total": 50
}
```
#### ۳. **نقاط پایانی دادههای ارز دیجیتال**
**دریافت قیمتها**:
```bash
curl "http://localhost:3000/api/crypto/prices?symbols=BTC,ETH,ADA"
```
**دریافت دادههای بازار**:
```bash
curl "http://localhost:3000/api/crypto/market"
```
**دریافت دادههای تاریخی**:
```bash
curl "http://localhost:3000/api/crypto/history?symbol=BTC&days=30"
```
### یکپارچهسازی با برنامه کاربردی
#### صفحه HTML
```html
<!DOCTYPE html>
<html dir="rtl" lang="fa">
<head>
<meta charset="UTF-8">
<title>مانیتور کریپتو</title>
</head>
<body>
<div id="news-container"></div>
<script type="module">
// بارگذاری اخبار
async function loadNews() {
const response = await fetch('/api/news?limit=10');
const data = await response.json();
const container = document.getElementById('news-container');
container.innerHTML = data.articles.map(article => `
<div class="news-card">
<h3>${article.title}</h3>
<p>${article.content}</p>
<a href="${article.url}">ادامه مطلب</a>
</div>
`).join('');
}
loadNews();
</script>
</body>
</html>
```
### مدیریت خطاها
```javascript
async function fetchNewsWithErrorHandling() {
try {
const response = await fetch('/api/news');
if (!response.ok) {
if (response.status === 401) {
throw new Error('احراز هویت ناموفق بود');
} else if (response.status === 429) {
throw new Error('تعداد درخواستها زیاد است');
} else if (response.status === 500) {
throw new Error('خطای سرور');
}
}
const data = await response.json();
return data.articles;
} catch (error) {
console.error('خطا در دریافت اخبار:', error);
alert(`خطا در بارگذاری اخبار: ${error.message}`);
return [];
}
}
```
### محدودیتهای استفاده
**محدودیتهای API**:
- News API: ۱۰۰ درخواست در روز (نسخه رایگان)
- Backend API: قابل تنظیم (پیشفرض: ۱۰۰۰ درخواست در ساعت)
### بهروزرسانیهای زنده (WebSocket)
```javascript
// اتصال به WebSocket برای قیمتهای لحظهای
const ws = new WebSocket('ws://localhost:3000/ws/crypto');
ws.onopen = () => {
console.log('اتصال برقرار شد');
// اشتراک در سکههای خاص
ws.send(JSON.stringify({
action: 'subscribe',
symbols: ['BTC', 'ETH', 'ADA']
}));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('بهروزرسانی قیمت:', data);
// بهروزرسانی رابط کاربری
updatePriceDisplay(data);
};
```
---
## Quick Reference
### Common Queries
| Purpose | Endpoint | Example |
|---------|----------|---------|
| Get all news | `/api/news` | `GET /api/news?limit=50` |
| Filter by source | `/api/news?source=X` | `GET /api/news?source=CoinDesk` |
| Positive news only | `/api/news?sentiment=positive` | `GET /api/news?sentiment=positive&limit=20` |
| Search keyword | Client-side filter | `newsPage.currentFilters.keyword = 'bitcoin'` |
| Get BTC price | `/api/crypto/prices?symbols=BTC` | `GET /api/crypto/prices?symbols=BTC` |
| Market overview | `/api/crypto/market` | `GET /api/crypto/market` |
### Response Status Codes
| Code | Meaning | Action |
|------|---------|--------|
| 200 | Success | Process data |
| 401 | Unauthorized | Check API key |
| 429 | Rate limited | Wait and retry |
| 500 | Server error | Use fallback data |
| 503 | Service unavailable | Retry later |
|