Skip to main content

How to use Postman with DeFi API

The guide helps you send DeFi API requests with Postman.


Prerequisites​

Before you start, make sure that you have:

  • DeFi API keys (api-key and secret),
  • a Postman account.

You can set Postman with the Postman collection or manually.


Setup Postman with the collection​

The Postman collection can help you speed up the usage of DeFi API in Postman. It already has all the variables and the pre-request script at the collection scope .

Download the collection (.zip)

  1. Go to the Variables section at the collection scope.
  2. Set your Private key (Secret) and API Key in Base64 as the api-key and secret variable values respectively.
  3. Choose the necessary request from the Postman collection.
  4. Specify the request parameter values in the Body section.
  5. Send the request.

Setup your Postman manually​

If you want to set Postman manually, follow the steps:

  1. Create a new collection.
  2. Go to the Variables section at the collection scope and add api-key and secret to the Variable column.
  3. Set your Private key (Secret) and API Key in Base64 as the api-key and secret variable values respectively.
  4. Go to the Scripts section and choose the Pre-script tab at the collection scope and insert the script below.
  5. Create a new request and specify an HTTP method.
  6. Set the API URL https://defiapi.changelly.com/{requiredMethod} in the URL field and specify the body data in the Body tab using the raw mode.
  7. Send the request to make sure that Postman is now able to send requests to Changelly DeFi API.
Pre-request script body
const forge = pm.require('npm:[email protected]');

addAuthHeaders()

function addAuthHeaders() {
const secretHex = pm.variables.get('secret');
const apiKey = pm.variables.get('api-key');

const derBytes = forge.util.hexToBytes(secretHex);
const asn1Obj = forge.asn1.fromDer(derBytes);
const pk = forge.pki.privateKeyFromAsn1(asn1Obj);

const method = pm.request.method;
let path = pm.request.url.getPath();

if (path === '/v1/health/') { return };

if (path[path.length-1] !== '/' && !path.startsWith("/v1/intents/")) {
path = `${path}/`
}

const query = pm.request.url.query;
const queryString = buildQueryString(query);

const body = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : '';
console.log(body);
const bodyString = jsonStringifySorted(body);
console.log(bodyString);


const message = `${method}:${path}:${queryString}:${bodyString}`;
console.log(message);
const md = forge.md.sha256.create();
md.update(message, 'utf8');
const signatureBytes = pk.sign(md);
const signature = forge.util.encode64(signatureBytes);

pm.request.headers.upsert({ key: 'Content-Type', value: 'application/json' });

pm.request.headers.upsert({ key: 'X-Api-Key', value: apiKey });
pm.request.headers.upsert({ key: 'X-Api-Signature', value: signature });

if (method === 'GET') { return };

pm.request.headers.upsert({
key: 'X-Api-Timestamp',
value: String(Math.floor(Date.now() / 1000)),
});
pm.request.headers.upsert({ key: 'X-Api-Nonce', value: randomNonce(32) });

console.log(pm.request.headers);
console.log(pm.request.body);
}

function buildQueryString(params) {
if (!params) return '';
return params.map(
obj => {
const { key, value } = {...obj};
const string = `${key}=${value}`;
return string;
}
).join('&');
}

function randomNonce(length) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
const idx = Math.floor(Math.random() * chars.length);
result += chars[idx];
}
return result;
}

function jsonStringifySorted(body) {
if (!body) return '';
const sortedKeys = Object.keys(body).sort();
const sortedObj = {};
for (const key of sortedKeys) {
sortedObj[key] = body[key];
}
return JSON.stringify(sortedObj);
}