HMAC Authentication
Last updated
/api/v1/orders?limit=10&page=1x-api-sign = hex(HMAC_SHA256(authenticationPayload, apiSecret))import hmac
import hashlib
from urllib.parse import urlencode
def canonical_uri(path: str, query: dict = None) -> str:
if not query:
return path
sorted_pairs = sorted(query.items(), key=lambda x: x[0])
return f"{path}?{urlencode(sorted_pairs)}"
def sign_trading_api(
method: str,
path: str,
query: dict = None,
timestamp: int = None,
nonce: str = None,
raw_body: str = "",
api_secret: str = "",
) -> str:
import time
import uuid
timestamp = timestamp or int(time.time() * 1000)
nonce = nonce or str(uuid.uuid4())
uri = canonical_uri(path, query)
payload = f"{method.upper()}\n{uri}\n{timestamp}\n{nonce}\n{raw_body}"
return hmac.new(
api_secret.encode("utf-8"),
payload.encode("utf-8"),
hashlib.sha256,
).hexdigest()METHOD="GET"
URI="/api/v1/symbols"
# Unix milliseconds. GNU date works directly; BSD (macOS) date emits a literal "N",
# so fall back based on whether the output is all digits (do NOT rely on exit code —
# BSD date exits 0 even for %3N).
TIMESTAMP=$(date +%s%3N)
case "$TIMESTAMP" in
''|*[!0-9]*)
if command -v gdate >/dev/null 2>&1; then TIMESTAMP=$(gdate +%s%3N)
else TIMESTAMP=$(python3 -c 'import time; print(int(time.time()*1000))'); fi ;;
esac
NONCE=$(uuidgen | tr '[:upper:]' '[:lower:]')
RAW_BODY=""
# Pipe printf STRAIGHT into openssl. Three gotchas this avoids:
# 1) Join with REAL newlines (printf format), not the two-character "\n".
# 2) PAYLOAD=$(printf ...) would strip the trailing newline -> wrong sig for empty-body (GET/DELETE) requests.
# 3) -macopt "key:..." (not -hmac "...") so a secret starting with "-" isn't parsed as an openssl option.
HMAC_AUTH=$(printf '%s\n%s\n%s\n%s\n%s' "$METHOD" "$URI" "$TIMESTAMP" "$NONCE" "$RAW_BODY" \
| openssl dgst -sha256 -mac HMAC -macopt "key:$API_SECRET" -hex | awk '{print $NF}')
curl -X GET "https://mainnet-api.monday.trade/rwa/trading/api/v1/symbols" \
-H "x-api-key: ${API_KEY}" \
-H "x-api-ts: ${TIMESTAMP}" \
-H "x-api-nonce: ${NONCE}" \
-H "x-api-sign: ${HMAC_AUTH}" \
-H "x-api-chain-id: 8453" \
-H "x-api-p: MondayTrade"