Last active
January 19, 2022 11:39
-
-
Save satheshshiva/e641381226617a8a56c11895f36a3556 to your computer and use it in GitHub Desktop.
Kucoin API - Prerequest script for Postman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this to sign postman requests to the KuCoin API | |
// The following environmental variables should be set in postman, API_KEY, KUCOIN_URL, PASSPHRASE, API_SECRET | |
// The requests needs to have the URL like in the example, {{KUCOIN_URL}}/api/v1/sub/user. "{{KUCOIN_URL}}" as-is. | |
// Kucoin API Key version 2. For version 1, you need to pass plain text of the passphraseHash instead of hash | |
const apiKey = postman.getEnvironmentVariable("API_KEY"); | |
const secret = postman.getEnvironmentVariable("API_SECRET"); | |
const passphrase = postman.getEnvironmentVariable("PASSPHRASE"); | |
const url = postman.getEnvironmentVariable("KUCOIN_URL"); | |
const timestamp = Date.now(); | |
const body = request.body ? request.body : ''; | |
const method = request.method; | |
const [,endpoint] = request.url.split("{{KUCOIN_URL}}"); | |
const signature = timestamp + method + endpoint + body; | |
const signHash = CryptoJS.HmacSHA256(signature, secret); | |
const passphraseHash = CryptoJS.HmacSHA256(passphrase, secret); | |
pm.request.headers.add({ | |
key: 'KC-API-KEY', | |
value: apiKey | |
}); | |
pm.request.headers.add({ | |
key: 'KC-API-SIGN', | |
value: signHash.toString(CryptoJS.enc.Base64) | |
}); | |
pm.request.headers.add({ | |
key: 'KC-API-TIMESTAMP', | |
value: timestamp | |
}); | |
pm.request.headers.add({ | |
key: 'KC-API-PASSPHRASE', | |
value: passphraseHash.toString(CryptoJS.enc.Base64) | |
}); | |
pm.request.headers.add({ | |
key: 'KC-API-KEY-VERSION', | |
value: "2" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment