Skip to content

API::Testing Postman Scripts

Edward Chen edited this page Mar 12, 2021 · 4 revisions

Postman Testing scripts for Signed Requests

Import Swagger (2.0) API Docs into Postman

Instructions to import data into Postman
https://learning.postman.com/docs/getting-started/importing-and-exporting-data/#importing-api-specifications

Swagger Doc Url
https://sandbox.caplinked.com/api/v1/swagger_doc.json

Guide to setting variables in Postman
https://learning.postman.com/docs/sending-requests/variables/

Add present Headers to collection

x-api-key => {{api_key}}
x-api-user-token => {{api_user_token}}
x-api-signature => Method=HMAC-SHA256 Signature={{api_signature}}
x-api-exp-date => {{api_expiration}}
x-api-ver => {{api_version}}

Add the script to 'Pre-request Script' to collection

Note: Update the api_key, api_user_token, and api_secret_key values

var api_expiration = Math.floor(Date.now() / 1000) + 20000;

var api_key = '9853eb1c5bcce224fbd24xxxxxxxdf69315';
var api_user_token = 'dc12ead85a0a5f9bfxxxxxxxxxb59c125a74f125';
var api_secret_key = '67eb59xxxxxxxxxxd1a59ce9890e26d69175535e70a82be96eb010d50b860';

postman.setGlobalVariable("api_expiration", api_expiration);
postman.setGlobalVariable("api_version", 'RC1.1');
postman.setGlobalVariable("api_user_token", api_user_token);
postman.setGlobalVariable("api_key", api_key);

var payload = api_key + api_user_token + api_expiration;

var hash = CryptoJS.HmacSHA256(payload, api_secret_key);
var hashInBase64 = CryptoJS.enc.Hex.stringify(hash);
var sig = "Method=HMAC-SHA256 Signature=" + hash;
postman.setGlobalVariable("api_signature", sig);

Clone this wiki locally