WebSocket Authentication
HMAC Authentication
The WebSocket feed provides real-time market data updates for orders and trades.
The WebSocket feed uses a bi-directional protocol, which encodes all messages as JSON objects. All messages have a type attribute that can be used to handle the message appropriately.
Please note that new message types can be added at any point in time. Clients are expected to ignore messages they do not support.
HMAC Login Mechanism for WebSocket
From BtcTurk | Kripto go to ACCOUNT > API Access
In the form select WebSocket, enter your IP address and submit.
This action will create public key and a private key to achieve WebSocket login.
For API Key instructions visit https://docs.btcturk.com/api-access-permissions
Code Example
// You can download ApiClient .net core complete library from github https://github.com/BTCTrader/broker-api-csharp-v2
private static async Task HmacTest()
{
Uri _uri = new Uri("wss://ws-feed-pro.btcturk.com");
ClientWebSocket client = new ClientWebSocket();
await client.ConnectAsync(_uri, CancellationToken.None);
string publicKey = "YOUR_PUBLIC_KEY";
string privateKey = "YOUR_PRIVATE_KEY";
long nonce = 3000;
string baseString = $"{publicKey}{nonce}";
string signature = ComputeHash(privateKey, baseString);
long timestamp = ToUnixTime(DateTime.UtcNow);
object[] hmacMessageObject = { 114, new { type = 114, publicKey = publicKey, timestamp = timestamp, nonce = nonce, signature = signature } };
string message = JsonSerializer.Serialize(hmacMessageObject);
await client.SendAsync(buffer: new ArraySegment<byte>(array: Encoding.UTF8.GetBytes(message),
offset: 0,
count: message.Length),
messageType: WebSocketMessageType.Text,
endOfMessage: true,
cancellationToken: CancellationToken.None);
}
private static string ComputeHash(string privateKey, string baseString)
{
var key = Convert.FromBase64String(privateKey);
string hashString;
using (var hmac = new HMACSHA256(key))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(baseString));
hashString = Convert.ToBase64String(hash);
}
return hashString;
}
private static long ToUnixTime(DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalMilliseconds);
}
Last updated
Was this helpful?