API Key & HMAC signing¶
Headers¶
| Header | Required | Description |
|---|---|---|
x-ptrader-apikey |
Yes (signed & key-only) | API key |
Signed requests¶
Required for signed trading, account, wallet, and similar private endpoints.
Parameters¶
| Parameter | Required | Description |
|---|---|---|
timestamp |
Yes on endpoints with recv-window checks | Unix ms. May be sent as a query or form field (request.getParameter) or as a request header named timestamp (parameter takes precedence over header). |
recvWindow |
No | Default 5000 ms, max 60000 ms. Query or form field only — no header fallback. Only validated when the endpoint enforces a recv window (see permissions matrix). |
signature |
Yes | HMAC-SHA256 hex. May be sent as a query or form field or as a request header named signature (parameter takes precedence over header). |
Recv window rule¶
When the endpoint enforces a recv window, the request is valid when:
timestamp ≤ serverTime + 1000 and serverTime - timestamp ≤ recvWindow
Other signed endpoints still require signature (and usually timestamp in the query for signing) but do not enforce the recv window.
Signing payload (originStr)¶
Build one string from the query (excluding signature) plus the request body, then HMAC it:
originStr = queryStringWithoutSignature + requestBody
signature = Hex(HMAC-SHA256(apiSecret, originStr))
Rule of thumb: Prefer query or form-encoded place-order requests when getting started. If you send JSON, use compact JSON (no extra whitespace) and number formats that match what the gateway re-serializes; otherwise signatures often fail with -1022.
Rules:
- Query: take the raw query string, apply one UTF-8 URL decode to the entire string, then split on
&intokey=valuepairs (send order preserved). Sign those decoded pairs joined with&. Percent-encode values normally on the wire. Note:+decodes to space. Because decode happens before split, a value containing an encoded&(%26) becomes a literal&and is split into extra pairs.
Query exclusion uses the substring signature
Any query pair whose full key=value string contains the substring signature (anywhere in the name or value) is excluded from the signing payload. Example: clientOrderId=my-signature-1 in the query string is dropped. Do not put signature inside other query parameter values.
- Body:
application/json→ the gateway parses the body and signs the re-serialized JSON tree (not raw bytes). Insignificant whitespace is dropped; avoid exponent notation such as1e3. Send compact JSON. If the body is not valid JSON, signing falls back to form fields and a malformed payload often surfaces later as-1022.application/x-www-form-urlencoded→ form fields whose parameter names are not already present in the query and are not exactlysignature. Exclusion is by exact parameter name, not by substring. A form field such asclientOrderId=my-signature-1is included in the signed string.- Concatenate query and body with no extra separator.
Worked example (LIMIT order, query-only body empty)¶
Illustrative credentials (not production keys):
- API Key:
vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A - API Secret:
NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j
Replace SYMBOL with a symbol value from GET /openapi/v1/exchangeInfo for your environment (the example below uses BTCUSDT for illustration only).
API_KEY="vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A"
API_SECRET="NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
BASE_URL="https://api.pcrypto.com"
SYMBOL="BTCUSDT" # illustrative — pick a listed symbol from exchangeInfo
TIMESTAMP=$(python3 -c 'import time; print(int(time.time()*1000))')
QUERY="symbol=${SYMBOL}&side=BUY&type=LIMIT&timeInForce=GTC&quantity=0.001&price=50000×tamp=${TIMESTAMP}&recvWindow=5000"
SIGNATURE=$(printf '%s' "${QUERY}" | openssl dgst -sha256 -hmac "${API_SECRET}" -hex | awk '{print $NF}')
curl -sS -X POST "${BASE_URL}/openapi/v1/order?${QUERY}&signature=${SIGNATURE}" \
-H "x-ptrader-apikey: ${API_KEY}" \
-H "Content-Type: application/x-www-form-urlencoded"
For JSON bodies, append the compact JSON string to originStr after the query string (no extra whitespace).
Troubleshooting¶
| Symptom | Likely cause |
|---|---|
-1022 with a JSON POST body |
Body not compact, invalid JSON (gateway falls back to form signing), or number formatting differs from gateway re-serialization. |
-1105 / empty signature from a shell example |
Digest extraction failed on your OpenSSL/LibreSSL build — prefer openssl dgst … -hex and take the last field. |
-2017 or -2018 for key/IP problems |
Either code can indicate a key or IP whitelist problem. See permissions — IP whitelist. |
Key-only endpoints (listenKey)¶
The listenKey endpoints require an API key only (no HMAC signature):
POST /openapi/v1/userDataStream (create), POST /openapi/v1/userDataStream/update (keepalive),
POST /openapi/v1/userDataStream/close (close).
Send x-ptrader-apikey only — do not include a signature parameter.