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
  • A Postman account

You can set Postman with the Postman collection or manually.

Set 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.

Set 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.
  3. Add api-key and secret to the Variable column.
  4. Set your Private key (Secret) and API Key in Base64 as the api-key and secret variable values respectively.
  5. Go to the Scripts section and choose the Pre-script tab at the collection scope.
  6. Insert the following script:
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);
}

  1. Create a new request and specify an HTTP method.
  2. Set the API URL https://defiapi.changelly.com/{requiredMethod} in the URL field.
  3. Specify the body data in the Body tab using the raw mode.
  4. Send the request to make sure that Postman is now able to send requests to DeFi API.