Example pre-request script to demonstrate calling the Shieldpay API using Postman.
const uuid = require('uuid');
const moment = require('moment');
const privateKey = pm.environment.get('private_key');
const apiKey = pm.environment.get('auth_token');
const requestId = uuid.v4();
const timestamp = moment().toISOString();
// Create the payload for the request.
let bodyPayload = pm.request.body;
const requestUrl = pm.request.url.getPath()
let url = pm.environment.get('url') + requestUrl
// Create the string containing the key elements of the request, which we'll use
// to generate the digital signature. For request methods without a body, e.g., GET,
// you can omit the body from the string. E.g.:
// const signatureString = `${url}${apiKey}${requestId}${timestamp}`;
const signatureString = `${url}${apiKey}${requestId}${timestamp}${bodyPayload}`
// Load the library that we will use for encryption. We recommend using
// postman-util-lib, which you can find here:
// https://joolfe.github.io/postman-util-lib/dist/bundle.js
eval(pm.environment.get('pmlib_code'));
let sig = new pmlib.rs.KJUR.crypto.Signature({ "alg": "SHA256withRSA" });
sig.init(privateKey);
let hash = sig.signString(signatureString);
// Generate a digital signature for the request.
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(hash));
// Add headers
pm.request.headers.add({
key: 'Authorization',
value: apiKey
});
pm.request.headers.add({
key: 'RequestID',
value: requestId
});
pm.request.headers.add({
key: 'Timestamp',
value: timestamp
});
pm.request.headers.add({
key: 'DigitalSignature',
value: signature
});