Tổng Kho eSIM
Authentication

HMAC Request Signing

Every /v1/* endpoint (including GET reads) must be signed with a single HMAC-SHA256 formula. There are no exceptions and no unauthenticated bypasses. Server-side signature evaluation uses constant-time comparison to prevent timing attacks.

Want ready-to-run files instead of copy-pasting a snippet? See the Self-Integration page to download runnable example files, a Postman collection, and an .http bundle — same signing formula as this page, just pre-packaged.

View Self-Integration

Required Headers

The first 4 headers are required on EVERY request. This list is derived directly from the security scheme in openapi.json and cannot drift from the backend.

HeaderTypeMeaning
X-TKE-Keyevery requestPublic key_id (tke_live_… / tke_test_… / tke_member_live_… / tke_member_test_…)
X-TKE-Timestampevery requestDispatch epoch milliseconds (integer string)
X-TKE-Nonceevery requestUUID (v4 or v7), unique per request
X-TKE-Signatureevery requestHMAC-SHA256 hex lowercase (formula below)
Idempotency-Key5 endpoints

A client-generated string — the same key + body executes exactly once (a retry returns the original result). Scoped by api_key_id (§11): two different keys = two separate commands.

Mandatory endpoints for Idempotency-Key
POST/v1/ordersPOST/v1/orders/bulkPOST/v1/esims/{id}/cancelPOST/v1/esims/{id}/change-planPOST/v1/esims/{id}/refund-requests

Signature Canonical Formula

HMAC-SHA256 authentication (§4). Send 4 headers: X-TKE-Key (public key_id), X-TKE-Timestamp (epoch ms), X-TKE-Nonce (a UUID per request), X-TKE-Signature (hex lowercase). signing_string = method\npath\ntimestamp\nnonce\nbody_hash; body_hash = SHA256(raw_body) hex. Compared with constant-time comparison. path INCLUDES the query string.

Segments are joined by newline characters \n (LF), and signed over SHA256(raw_body) rather than the direct body bytes. The server hashes the exact received bytes again to verify the request.

text
signing_string = method + "\n"
               + path + "\n"        # includes query string if present, excludes scheme/host
               + timestamp + "\n"   # = header X-TKE-Timestamp (epoch ms)
               + nonce + "\n"       # = header X-TKE-Nonce
               + body_hash          # SHA256(raw_body) hex lowercase

signature = HMAC_SHA256(secret, signing_string)   → hex lowercase
For body-less requests (GET, DELETE) or empty bodies, raw_body = "" → body_hash = SHA256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.

HMAC Signing Code Snippets

Copy-paste and substitute your actual secret and key_id. Always send the exact hashed bytes as the body — do not re-stringify after signing.

bash
#!/usr/bin/env bash
# Yeu cau: openssl. Ky POST /v1/orders.
KEY_ID="tke_test_ab12cd34..."
SECRET="tke_secret_test_0123456789abcdef"
METHOD="POST"
PATH_Q="/v1/orders"
BODY='{"items":[{"package_code":"THAY-BANG-MA-GOI-CUA-BAN","quantity":2}]}'
TS="$(date +%s000)"                 # epoch milliseconds
NONCE="$(uuidgen)"

BODY_HASH="$(printf '%s' "$BODY" | openssl dgst -sha256 -hex | awk '{print $NF}')"
SIGNING="$METHOD"$'
'"$PATH_Q"$'
'"$TS"$'
'"$NONCE"$'
'"$BODY_HASH"
SIG="$(printf '%s' "$SIGNING" | openssl dgst -sha256 -hmac "$SECRET" -hex | awk '{print $NF}')"

curl -sS "https://api.tongkhoesim.com$PATH_Q"   -H "X-TKE-Key: $KEY_ID"   -H "X-TKE-Timestamp: $TS"   -H "X-TKE-Nonce: $NONCE"   -H "X-TKE-Signature: $SIG"   -H "Idempotency-Key: $(uuidgen)"   -H "Content-Type: application/json"   --data-raw "$BODY"

Live Backend Test Vectors

Below are authoritative test vectors generated by the backend during build. Use them to verify your signing algorithm implementation.

Usage: configure your implementation to sign the exact inputs below (same method, path, timestamp, nonce, body) and verify the resulting signature. If mismatched, compare the intermediate signing_string to isolate the discrepancy.

sample secret: tke_secret_test_0123456789abcdef
POST /v1/orders
methodPOST
path/v1/orders
timestamp1720680000000
nonce11111111-1111-4111-8111-111111111111
raw_body{"items":[{"package_code":"VN_5GB_30D","quantity":2}]}
body_hashc197853f7fe46a9f2de1478e565c48ef9369d0152af5117434be8ccfa9391b31
signing_string
POST
/v1/orders
1720680000000
11111111-1111-4111-8111-111111111111
c197853f7fe46a9f2de1478e565c48ef9369d0152af5117434be8ccfa9391b31
signature (expected result)
97147afdebb5d15cf8441278ef524ce15c715ca95962ddb1cb2604a1672fc5d5
GET /v1/wallet/balance
methodGET
path/v1/wallet/balance
timestamp1720680000000
nonce22222222-2222-4222-8222-222222222222
raw_body(empty)
body_hashe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
signing_string
GET
/v1/wallet/balance
1720680000000
22222222-2222-4222-8222-222222222222
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
signature (expected result)
ab16941884d407924c7b15dea008d6f1d8c0c1be96dca3745344218a99257dbe