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.
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
Retrieve Server Time: Obtains the current server timestamp in milliseconds by calling
get_server_time()
with the base URL fromget_base_url()
.Construct Prehash String: Concatenates the timestamp, uppercase HTTP method, endpoint, and request body.
Generate Signature: Computes an HMAC-SHA256 signature over the prehash string using the API secret, then base64-encodes it.
Encrypt Passphrase: Signs the API passphrase with the API secret using HMAC-SHA256 and base64-encodes the result.
Assemble Headers: Constructs headers with
httr::add_headers()
, includingKC-API-KEY
,KC-API-SIGN
,KC-API-TIMESTAMP
,KC-API-PASSPHRASE
,KC-API-KEY-VERSION
, andContent-Type
.
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()
} # }