Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/5a50882baaf76aa57067b09a7ee7cb97 to your computer and use it in GitHub Desktop.
Save normanlmfung/5a50882baaf76aa57067b09a7ee7cb97 to your computer and use it in GitHub Desktop.
python_syntax_restapi_call
import aiohttp
import asyncio
'''
OKX API
OKX REST API addresses:
REST: https://www.okx.com/
Public WebSocket: wss://ws.okx.com:8443/ws/v5/public
Private WebSocket: wss://ws.okx.com:8443/ws/v5/private
Business WebSocket: wss://ws.okx.com:8443/ws/v5/business
GET Orderbooks endpoint:
GET /api/v5/market/books?instId=BTC-USDT
REF:
https://www.okx.com/docs-v5/en/
https://www.okx.com/docs-v5/en/#order-book-trading-market-data-get-order-book
https://www.okx.com/docs-v5/en/#overview-websocket-subscribe
https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-order-book-channel
'''
async def fetch_orderbook(pair):
base_url = "https://www.okx.com"
endpoint = f"/api/v5/market/books?instId={pair}"
url = base_url + endpoint
async with aiohttp.ClientSession() as session:
try:
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
print(f"Order Book for {pair}: {data}")
else:
print(f"Failed to fetch order book for {pair}. Status code: {response.status}")
except Exception as e:
print(f"An error occurred while fetching order book for {pair}: {e}")
async def main():
trading_pairs = ["BTC-USDT", "ETH-USDT", "SOL-USDT", "MATIC-USDT", "LTC-USDT"]
tasks = [fetch_orderbook(pair) for pair in trading_pairs]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment