const API_KEY = pm.environment.get("API_KEY");
const API_SECRET = (pm.environment.get("API_SECRET") || "").trim();
async function hmacSha256Base64(key, msg) {
const enc = new TextEncoder();
const cryptoKey = await crypto.subtle.importKey(
"raw",
enc.encode(key),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"]
);
const sig = await crypto.subtle.sign("HMAC", cryptoKey, enc.encode(msg));
return btoa(String.fromCharCode(...new Uint8Array(sig)));
}
function sha256Hex(str) {
return CryptoJS.SHA256(str).toString(CryptoJS.enc.Hex);
}
const method = pm.request.method.toUpperCase();
const path = pm.request.url.getPath();
const query = pm.request.url.getQueryString() || "";
const body = pm.request.body?.raw || "";
const bodyHash = sha256Hex(body);
const timestamp = Math.floor(Date.now() / 1000).toString();
const nonce = crypto.randomUUID();
const canonical = [
method,
path,
query,
timestamp,
nonce,
bodyHash
].join("\n");
const signature = await hmacSha256Base64(API_SECRET, canonical);
pm.request.headers.upsert({ key: "X-API-Key", value: API_KEY });
pm.request.headers.upsert({ key: "X-Timestamp", value: timestamp });
pm.request.headers.upsert({ key: "X-Nonce", value: nonce });
pm.request.headers.upsert({ key: "X-Signature", value: `v1=${signature}` });