|
| 1 | +# Python REST API for Pyth Price Feeds |
| 2 | + |
| 3 | +A Flask-based REST API for monitoring cryptocurrency prices using Pyth Network's decentralized oracle. This example demonstrates how to build a production-ready API with Pyth price feeds, supporting 20+ tokens including BTC, ETH, SOL, and more. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ✅ **20+ Cryptocurrencies** - Monitor BTC, ETH, SOL, BNB, AVAX, MATIC, ARB, OP, and more |
| 8 | +- ✅ **Threshold Monitoring** - Check if prices cross specific thresholds |
| 9 | +- ✅ **Real-time Prices** - Powered by Pyth Network's Hermes API |
| 10 | +- ✅ **Session Management** - Monitor multiple tokens simultaneously |
| 11 | +- ✅ **RESTful API** - Clean, well-documented endpoints |
| 12 | +- ✅ **CORS Enabled** - Ready for web applications |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +### 1. Install Dependencies |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install -r requirements.txt |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Start the Server |
| 23 | + |
| 24 | +```bash |
| 25 | +python app.py |
| 26 | +``` |
| 27 | + |
| 28 | +Server will start on `http://localhost:8080` |
| 29 | + |
| 30 | +### 3. Test the API |
| 31 | + |
| 32 | +```bash |
| 33 | +# In another terminal |
| 34 | +python test_endpoints.py |
| 35 | +``` |
| 36 | + |
| 37 | +## API Endpoints |
| 38 | + |
| 39 | +| Endpoint | Method | Description | |
| 40 | +|----------|--------|-------------| |
| 41 | +| `/health` | GET | Health check | |
| 42 | +| `/api/tokens` | GET | List all supported tokens | |
| 43 | +| `/api/price/<symbol>` | GET | Get current price for any token | |
| 44 | +| `/api/check` | POST | Check price vs threshold (single) | |
| 45 | +| `/api/monitor/start` | POST | Start monitoring session | |
| 46 | +| `/api/monitor/<session_id>` | GET | Get session status | |
| 47 | +| `/api/monitor/<session_id>/stop` | POST | Stop monitoring session | |
| 48 | +| `/api/monitor/sessions` | GET | List all sessions | |
| 49 | + |
| 50 | +## Usage Examples |
| 51 | + |
| 52 | +### Get Bitcoin Price |
| 53 | + |
| 54 | +```bash |
| 55 | +curl http://localhost:8080/api/price/BTC/USD |
| 56 | +``` |
| 57 | + |
| 58 | +**Response:** |
| 59 | +```json |
| 60 | +{ |
| 61 | + "success": true, |
| 62 | + "symbol": "BTC/USD", |
| 63 | + "price": 67234.56, |
| 64 | + "timestamp": "2024-01-23T10:15:23.123456" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Check if ETH is Below $3000 |
| 69 | + |
| 70 | +```bash |
| 71 | +curl -X POST http://localhost:8080/api/check \ |
| 72 | + -H "Content-Type: application/json" \ |
| 73 | + -d '{"symbol": "ETH/USD", "threshold": 3000}' |
| 74 | +``` |
| 75 | + |
| 76 | +**Response:** |
| 77 | +```json |
| 78 | +{ |
| 79 | + "success": true, |
| 80 | + "symbol": "ETH/USD", |
| 81 | + "price": 2845.67, |
| 82 | + "threshold": 3000, |
| 83 | + "is_below_threshold": true, |
| 84 | + "result": true, |
| 85 | + "timestamp": "2024-01-23T10:15:23.123456" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Start Continuous Monitoring |
| 90 | + |
| 91 | +```bash |
| 92 | +curl -X POST http://localhost:8080/api/monitor/start \ |
| 93 | + -H "Content-Type: application/json" \ |
| 94 | + -d '{ |
| 95 | + "symbol": "BTC/USD", |
| 96 | + "threshold": 50000, |
| 97 | + "update_interval": 10 |
| 98 | + }' |
| 99 | +``` |
| 100 | + |
| 101 | +**Response:** |
| 102 | +```json |
| 103 | +{ |
| 104 | + "success": true, |
| 105 | + "message": "Started monitoring BTC/USD with threshold $50000.00", |
| 106 | + "session_id": "session_1", |
| 107 | + "symbol": "BTC/USD", |
| 108 | + "threshold": 50000, |
| 109 | + "update_interval": 10 |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Supported Tokens |
| 114 | + |
| 115 | +BTC/USD, ETH/USD, SOL/USD, BNB/USD, AVAX/USD, MATIC/USD, ARB/USD, OP/USD, DOGE/USD, ADA/USD, DOT/USD, LINK/USD, UNI/USD, ATOM/USD, XRP/USD, LTC/USD, APT/USD, SUI/USD, TRX/USD, NEAR/USD |
| 116 | + |
| 117 | +Get the complete list: |
| 118 | +```bash |
| 119 | +curl http://localhost:8080/api/tokens |
| 120 | +``` |
| 121 | + |
| 122 | +## Project Structure |
| 123 | + |
| 124 | +``` |
| 125 | +python-rest-api/ |
| 126 | +├── app.py # Flask REST API server |
| 127 | +├── price_monitor.py # Core price monitoring logic |
| 128 | +├── requirements.txt # Python dependencies |
| 129 | +├── test_endpoints.py # API test suite |
| 130 | +└── README.md # This file |
| 131 | +``` |
| 132 | + |
| 133 | +## Key Components |
| 134 | + |
| 135 | +### price_monitor.py |
| 136 | + |
| 137 | +Core module that handles Pyth Network integration: |
| 138 | +- Fetches prices from Pyth's Hermes API |
| 139 | +- Supports 20+ cryptocurrency pairs |
| 140 | +- Provides both single-check and continuous monitoring |
| 141 | +- Handles price threshold comparisons |
| 142 | + |
| 143 | +### app.py |
| 144 | + |
| 145 | +Flask REST API server that provides: |
| 146 | +- RESTful endpoints for price queries |
| 147 | +- Session-based continuous monitoring |
| 148 | +- Background threading for real-time updates |
| 149 | +- CORS support for web applications |
| 150 | + |
| 151 | +## Python Client Example |
| 152 | + |
| 153 | +```python |
| 154 | +import requests |
| 155 | +import time |
| 156 | + |
| 157 | +BASE_URL = "http://localhost:8080" |
| 158 | + |
| 159 | +# Get current price |
| 160 | +response = requests.get(f"{BASE_URL}/api/price/BTC/USD") |
| 161 | +price_data = response.json() |
| 162 | +print(f"BTC Price: ${price_data['price']:,.2f}") |
| 163 | + |
| 164 | +# Quick threshold check |
| 165 | +response = requests.post(f"{BASE_URL}/api/check", json={ |
| 166 | + "symbol": "ETH/USD", |
| 167 | + "threshold": 3000 |
| 168 | +}) |
| 169 | +result = response.json() |
| 170 | +print(f"ETH below $3000: {result['is_below_threshold']}") |
| 171 | + |
| 172 | +# Start monitoring |
| 173 | +response = requests.post(f"{BASE_URL}/api/monitor/start", json={ |
| 174 | + "symbol": "BTC/USD", |
| 175 | + "threshold": 50000, |
| 176 | + "update_interval": 10 |
| 177 | +}) |
| 178 | +session_id = response.json()['session_id'] |
| 179 | +print(f"Started monitoring, session: {session_id}") |
| 180 | + |
| 181 | +# Poll for updates |
| 182 | +try: |
| 183 | + while True: |
| 184 | + response = requests.get(f"{BASE_URL}/api/monitor/{session_id}") |
| 185 | + data = response.json()['data'] |
| 186 | + |
| 187 | + if data['price']: |
| 188 | + print(f"[{data['timestamp']}] {data['symbol']}: " |
| 189 | + f"${data['price']:,.2f} | Below threshold: {data['is_below_threshold']}") |
| 190 | + |
| 191 | + time.sleep(10) |
| 192 | +except KeyboardInterrupt: |
| 193 | + # Stop monitoring |
| 194 | + requests.post(f"{BASE_URL}/api/monitor/{session_id}/stop") |
| 195 | + print("\nMonitoring stopped") |
| 196 | +``` |
| 197 | + |
| 198 | +## Use Cases |
| 199 | + |
| 200 | +- **Price Alerts** - Build notification systems for price thresholds |
| 201 | +- **Trading Bots** - Integrate real-time price data for automated trading |
| 202 | +- **Portfolio Tracking** - Monitor multiple assets simultaneously |
| 203 | +- **Market Analysis** - Collect and analyze price trends |
| 204 | +- **DeFi Integrations** - Use as a data feed for decentralized applications |
| 205 | + |
| 206 | +## Technologies |
| 207 | + |
| 208 | +- **Flask** - Lightweight web framework |
| 209 | +- **Pyth Network** - Decentralized oracle for real-time price data |
| 210 | +- **Python 3.7+** - Programming language |
| 211 | +- **Threading** - For concurrent monitoring sessions |
| 212 | + |
| 213 | +## Deployment |
| 214 | + |
| 215 | +This API can be easily deployed using: |
| 216 | +- **ngrok** - For quick public exposure (see deployment guide) |
| 217 | +- **Docker** - Containerized deployment |
| 218 | +- **Cloud Services** - AWS, GCP, Azure, Heroku, etc. |
| 219 | + |
| 220 | +## About Pyth Network |
| 221 | + |
| 222 | +This example uses [Pyth Network](https://pyth.network)'s Hermes API to fetch real-time cryptocurrency prices. Pyth Network is a decentralized oracle that provides high-fidelity, high-frequency market data from over 90+ first-party publishers. |
| 223 | + |
| 224 | +## License |
| 225 | + |
| 226 | +Apache 2.0 |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +**Built with [Pyth Network](https://pyth.network) 🔮** |
| 231 | + |
0 commit comments