|
| 1 | +# Exchange Rates API |
| 2 | + |
| 3 | +This document describes the exchange rate endpoints that provide crypto/fiat currency exchange rates using the Tatum API. |
| 4 | + |
| 5 | +## Endpoints |
| 6 | + |
| 7 | +### GET /api/exchange-rates/:crypto/:fiat |
| 8 | + |
| 9 | +Get a single exchange rate for a specific crypto/fiat pair. |
| 10 | + |
| 11 | +**Parameters:** |
| 12 | +- `crypto` (string): Cryptocurrency symbol (e.g., BTC, ETH, ADA) |
| 13 | +- `fiat` (string): Fiat currency symbol (e.g., USD, EUR, GBP) |
| 14 | + |
| 15 | +**Example Request:** |
| 16 | +```bash |
| 17 | +GET /api/exchange-rates/BTC/USD |
| 18 | +``` |
| 19 | + |
| 20 | +**Example Response:** |
| 21 | +```json |
| 22 | +{ |
| 23 | + "crypto": "BTC", |
| 24 | + "fiat": "USD", |
| 25 | + "rate": 88375.98, |
| 26 | + "timestamp": "2025-01-01T00:00:00.000Z" |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### POST /api/exchange-rates |
| 31 | + |
| 32 | +Get multiple exchange rates for an array of crypto/fiat pairs. |
| 33 | + |
| 34 | +**Request Body:** |
| 35 | +```json |
| 36 | +{ |
| 37 | + "pairs": [ |
| 38 | + { "crypto": "BTC", "fiat": "USD" }, |
| 39 | + { "crypto": "ETH", "fiat": "EUR" }, |
| 40 | + { "crypto": "ADA", "fiat": "GBP" } |
| 41 | + ] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +**Example Response:** |
| 46 | +```json |
| 47 | +{ |
| 48 | + "rates": [ |
| 49 | + { |
| 50 | + "crypto": "BTC", |
| 51 | + "fiat": "USD", |
| 52 | + "rate": 88375.98, |
| 53 | + "timestamp": "2025-01-01T00:00:00.000Z" |
| 54 | + }, |
| 55 | + { |
| 56 | + "crypto": "ETH", |
| 57 | + "fiat": "EUR", |
| 58 | + "rate": 3245.67, |
| 59 | + "timestamp": "2025-01-01T00:00:00.000Z" |
| 60 | + }, |
| 61 | + { |
| 62 | + "crypto": "ADA", |
| 63 | + "fiat": "GBP", |
| 64 | + "rate": 0.85, |
| 65 | + "timestamp": "2025-01-01T00:00:00.000Z" |
| 66 | + } |
| 67 | + ], |
| 68 | + "timestamp": "2025-01-01T00:00:00.000Z" |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +**Constraints:** |
| 73 | +- Maximum 10 currency pairs per batch request |
| 74 | +- At least 1 currency pair is required |
| 75 | + |
| 76 | +### GET /api/exchange-rates/supported |
| 77 | + |
| 78 | +Get lists of supported cryptocurrencies and fiat currencies. |
| 79 | + |
| 80 | +**Example Response:** |
| 81 | +```json |
| 82 | +{ |
| 83 | + "cryptos": [ |
| 84 | + "BTC", "ETH", "ADA", "DOT", "LTC", "XRP", "BCH", "EOS", "TRX", "XLM", |
| 85 | + "LINK", "UNI", "AAVE", "COMP", "MKR", "SNX", "YFI", "SUSHI", "CRV", |
| 86 | + "BAL", "MATIC", "AVAX", "SOL", "LUNA", "FTT", "SRM", "RAY" |
| 87 | + ], |
| 88 | + "fiats": [ |
| 89 | + "USD", "EUR", "GBP", "JPY", "AUD", "CAD", "CHF", "CNY", "SEK", "NZD", |
| 90 | + "MXN", "SGD", "HKD", "NOK", "TRY", "ZAR", "BRL", "INR", "KRW", "PLN", |
| 91 | + "CZK", "HUF", "RON", "BGN", "HRK", "RUB", "UAH", "AED", "SAR" |
| 92 | + ] |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Error Handling |
| 97 | + |
| 98 | +All endpoints return appropriate HTTP status codes and error messages: |
| 99 | + |
| 100 | +### 400 Bad Request |
| 101 | +- Missing required parameters |
| 102 | +- Invalid currency symbols |
| 103 | +- Empty pairs array |
| 104 | +- Too many pairs (>10) |
| 105 | + |
| 106 | +**Example Error Response:** |
| 107 | +```json |
| 108 | +{ |
| 109 | + "error": "Invalid parameters", |
| 110 | + "message": "Invalid crypto symbol: must be a non-empty string" |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### 500 Internal Server Error |
| 115 | +- Tatum API errors |
| 116 | +- Network connectivity issues |
| 117 | +- Server-side processing errors |
| 118 | + |
| 119 | +**Example Error Response:** |
| 120 | +```json |
| 121 | +{ |
| 122 | + "error": "Failed to fetch exchange rate", |
| 123 | + "message": "Tatum API error: 404 Not Found" |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Environment Variables |
| 128 | + |
| 129 | +Make sure to set the following environment variable: |
| 130 | + |
| 131 | +```bash |
| 132 | +TATUM_API_KEY=your_tatum_api_key_here |
| 133 | +``` |
| 134 | + |
| 135 | +## Testing |
| 136 | + |
| 137 | +To test the exchange rate endpoints, you can use the provided test script: |
| 138 | + |
| 139 | +```bash |
| 140 | +# Start the server first |
| 141 | +pnpm dev |
| 142 | + |
| 143 | +# In another terminal, run the exchange rate tests |
| 144 | +node test-exchange-rates.js |
| 145 | +``` |
| 146 | + |
| 147 | +## Implementation Details |
| 148 | + |
| 149 | +- **Service Layer**: [`src/services/exchange-rate-service.js`](src/services/exchange-rate-service.js) |
| 150 | +- **Routes**: [`src/routes/exchange-rates.js`](src/routes/exchange-rates.js) |
| 151 | +- **Tatum Integration**: [`src/utils/tatum.js`](src/utils/tatum.js) |
| 152 | + |
| 153 | +The implementation follows the existing project patterns: |
| 154 | +- Clean separation of concerns |
| 155 | +- Comprehensive error handling |
| 156 | +- Input validation |
| 157 | +- Consistent API response format |
| 158 | +- ESM module structure |
0 commit comments