Created
March 28, 2024 05:05
-
-
Save normanlmfung/e6129dd4d5c6a2c5e95498b747a88618 to your computer and use it in GitHub Desktop.
csharp_rest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
/* | |
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 | |
*/ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
// Define the trading pairs | |
string[] tradingPairs = { "BTC-USDT", "ETH-USDT", "SOL-USDT", "MATIC-USDT", "LTC-USDT" }; | |
// Initialize HttpClient | |
using (HttpClient client = new HttpClient()) | |
{ | |
// Base URL for OKX REST API | |
string baseUrl = "https://www.okx.com"; | |
foreach (string pair in tradingPairs) | |
{ | |
try | |
{ | |
// Form the request URL | |
string endpoint = $"/api/v5/market/books?instId={pair}"; | |
string requestUrl = baseUrl + endpoint; | |
// Make GET request to fetch order book | |
HttpResponseMessage response = await client.GetAsync(requestUrl); | |
// Check if request was successful | |
if (response.IsSuccessStatusCode) | |
{ | |
// Read response content as string | |
string responseContent = await response.Content.ReadAsStringAsync(); | |
// Print the response | |
Console.WriteLine($"Order Book for {pair}:"); | |
Console.WriteLine(responseContent); | |
} | |
else | |
{ | |
Console.WriteLine($"Failed to fetch order book for {pair}. Status code: {response.StatusCode}"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"An error occurred while fetching order book for {pair}: {ex.Message}"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment