Using Postman with our API
Before you start, make sure that you have:
- Changelly API keys
- A Postman account
1. Get jsrsasign library used for signing
- Create a new GET request.
- Copy https://raw.githubusercontent.com/kjur/jsrsasign/master/jsrsasign-all-min.js to the URL field.
- Go to the Scripts tab, select Post-response section and enter:
postman.setGlobalVariable("jsrsasign-js", responseBody);
- Send the request.
note
This step is essential for sending authorized requests to our API. Once completed, the outcome of the GET request is stored in a global variable, which is subsequently employed in the pre-request scripts used for authorization.
2. Add a pre-request script
- Create a new POST request.
- Go to the Pre-request Scripts tab.
- Insert the following script and replace the placeholders (
yourPrivateKey
,yourApiKey
) with your actual keys:
const navigator = {}; //fake a navigator object for the lib
const window = {}; //fake a window object for the lib
eval(postman.getGlobalVariable("jsrsasign-js"));
const API_PUBLIC_KEY = 'yourApiKey';
const API_PRIVATE_KEY = 'yourPrivateKey';
const URL = request.url.toString();
const message = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {};
const payload = URL + JSON.stringify(message);
const privateKeyString = Buffer.from(API_PRIVATE_KEY, 'base64').toString('binary');
const private_key_object = KEYUTIL.getKey(privateKeyString);
const sig = new KJUR.crypto.Signature({"alg": "SHA256withRSA"});
sig.init(private_key_object);
const signature = hextob64(sig.signString(payload));
pm.request.headers.add({
key: 'X-Api-Key',
value: API_PUBLIC_KEY
});
pm.request.headers.add({
key: 'X-Api-Signature',
value: signature
});
- Set the API URL in the URL field and specify the body data in the Body tab using the raw mode.
- Send the request to make sure that Postman is now able to send requests to our API.