Skip to contents

Generates HTTP request headers asynchronously for authenticated KuCoin API requests, incorporating the API key, HMAC-SHA256 signature, timestamp, encrypted passphrase, key version, and content type to ensure request security.

Usage

build_headers(
  method,
  endpoint,
  body,
  keys,
  .__coro_env_parent__ = <environment>
)

Arguments

method

Character string specifying the HTTP method (e.g., "GET", "POST").

endpoint

Character string representing the API endpoint (e.g., "/api/v1/orders").

body

Character string containing the JSON-formatted request body; use "" if no payload is required.

keys

List of API credentials including:

  • api_key: Character string; your KuCoin API key.

  • api_secret: Character string; your KuCoin API secret.

  • api_passphrase: Character string; your KuCoin API passphrase.

  • key_version: Character string; the API key version (e.g., "2").

Value

Promise resolving to a list of HTTP headers created with httr::add_headers().

Details

Workflow Overview

  1. Retrieve Server Time: Obtains the current server timestamp in milliseconds by calling get_server_time() with the base URL from get_base_url().

  2. Construct Prehash String: Concatenates the timestamp, uppercase HTTP method, endpoint, and request body.

  3. Generate Signature: Computes an HMAC-SHA256 signature over the prehash string using the API secret, then base64-encodes it.

  4. Encrypt Passphrase: Signs the API passphrase with the API secret using HMAC-SHA256 and base64-encodes the result.

  5. Assemble Headers: Constructs headers with httr::add_headers(), including KC-API-KEY, KC-API-SIGN, KC-API-TIMESTAMP, KC-API-PASSPHRASE, KC-API-KEY-VERSION, and Content-Type.

API Endpoint

Not applicable (helper function for request construction).

Usage

Employed to authenticate and secure API requests to KuCoin endpoints requiring authorisation.

Official Documentation

Not directly tied to a specific endpoint; see KuCoin API authentication guidelines.

Examples

if (FALSE) { # \dontrun{
keys <- list(
  api_key = "your_api_key",
  api_secret = "your_api_secret",
  api_passphrase = "your_api_passphrase",
  key_version = "2"
)
main_async <- coro::async(function() {
  headers <- await(build_headers("POST", "/api/v1/orders", '{"size": 1}', keys))
  print(headers)
  headers <- await(build_headers("GET", "/api/v1/orders", "", keys))
  print(headers)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }