Skip to main content

WebSocket Authentication

All API calls related to a user account, require authentication. You need to provide 4 parameters to authenticate a request:

KeyValue
X-PCKAPI Public Key
X-StampTimestamp
X-NonceDelay Tolerance
X-SignatureSignature of Request
warning

All these parameters must be transmitted from the header before opening a connection to the socket.


Signature

Signature is a HMAC-SHA256 encoded message. The HMAC-SHA256 code must be generated using a private key that contains a current timestamp and your API key.

Nonce

Nonce is a integer number. The value sent determines the connection delay tolerance. For example, if the Nonce value is 15000 (15 seconds), and your request meets the condition (X-Stamp + X-Nonce < Current Server Timestamp), your request will fail. This is because the request you sent reaches the server later than the Nonce value


TLS URL Scheme and Ports

WebSocket connections must be established over TLS using the secure WebSocket scheme.

New API Endpoints

Simply update your WebSocket URLs as shown below:

  • HTTPS (REST / Auth related calls)

    https://<host>
  • Secure WebSocket (TLS)

    wss://<host>:8443

The wss scheme indicates a WebSocket connection secured with TLS.
Port 8443 is required for secure WebSocket communication.


TLS URL Scheme and Ports

WebSocket connections must be established over TLS using the secure WebSocket scheme.

New API Endpoints

Simply update your WebSocket URLs as shown below:

  • HTTPS (REST / Auth related calls)

    https://<host>
  • Secure WebSocket (TLS)

    wss://<host>:8443

The wss scheme indicates a WebSocket connection secured with TLS.
Port 8443 is required for secure WebSocket communication.


Code Examples:

public async Task Connect()
{
ClientWebSocket clientWebSocket = new ClientWebSocket();
string publicKey = "PUBLIC_KEY";
string privateKey = "PRIVATE_KEY";
long nonce = 15000;
long timestamp = DateTimeExtension.ToUnixTime(DateTime.UtcNow);
string baseString = $"{publicKey}{timestamp}";
string signature = ComputeHash(privateKey, baseString);

clientWebSocket .Options.SetRequestHeader("X-PCK", publicKey);
clientWebSocket .Options.SetRequestHeader("X-Signature", signature);
clientWebSocket .Options.SetRequestHeader("X-Stamp", timestamp.ToString());
clientWebSocket .Options.SetRequestHeader("X-Nonce", nonce.ToString());

await clientWebSocket .ConnectAsync(new Uri("wss://{BASEURL}:8443"), CancellationToken.None);
}

private string ComputeHash(string privateKey, string message)
{
byte[] key = Convert.FromBase64String(privateKey);
string hashString;

using (HMACSHA256 hmac = new HMACSHA256(key))
{
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
hashString = Convert.ToBase64String(hash);
}

return hashString;
}