Skip to contents

Retrieves the current server time as a Unix timestamp in milliseconds from the KuCoin Futures API asynchronously. This helper function is essential for authenticated requests, ensuring timestamps align with server time within a 5-second window.

Usage

get_server_time(base_url = get_base_url())

Arguments

base_url

Character string specifying the base URL for the KuCoin Futures API. Defaults to get_base_url().

Value

Promise resolving to a numeric Unix timestamp in milliseconds or rejecting with an error if the request fails.

Details

Workflow Overview

  1. URL Construction: Combines the base URL (default from get_base_url()) with the endpoint /api/v1/timestamp.

  2. API Request: Sends an asynchronous GET request with a 3-second timeout using httr::GET().

  3. Status Check: Validates the HTTP status code is 200, rejecting the promise if not.

  4. Response Parsing: Extracts the response text and parses it as JSON with jsonlite::fromJSON().

  5. Structure Validation: Ensures "code" and "data" fields exist, rejecting if missing.

  6. Success Check: Confirms the API code is "200000", rejecting if not.

  7. Result Resolution: Resolves the promise with the timestamp from the "data" field.

API Endpoint

GET https://api.kucoin.com/api/v1/timestamp

Usage

Utilised to synchronise request timestamps for signature generation and to prevent replay attacks in authenticated API calls.

Official Documentation

KuCoin Futures Get Server Time

Examples

if (FALSE) { # \dontrun{
get_server_time()$
  then(function(timestamp) {
    cat("KuCoin Server Time:", timestamp, "\n")
  })$
  catch(function(e) {
    message("Error retrieving server time:", conditionMessage(e))
  })
while (!later::loop_empty()) later::run_now(timeoutSecs = Inf, all = TRUE)
} # }