Skip to main content

Examples of sending a request

There are the Node.js, PHP, Python, and Java code examples which are easy to copy. You can find the detailed manual of sending the signed request here.

note

Node.js >= 15 is required.

const crypto = require('crypto');
const fetch = require('node-fetch');

const API_PUBLIC_KEY = '<Your public API key>';
const API_PRIVATE_KEY = '<Your private API key>';

const privateKeyObject = crypto.createPrivateKey({
key: API_PRIVATE_KEY,
type: 'pkcs1',
format: 'pem',
encoding: 'base64',
});

const path = 'https://fiat-api.changelly.com/v1/validate-address';
const message = {
"currency": "XRP",
"walletAddress": "rwpMvfxoodXggJ1g4qv6MWAPQqWDwQyHUW",
"walletExtraId": "1234hg"
};

const payload = path + JSON.stringify(message);

const signature = crypto.sign('sha256', Buffer.from(payload), privateKeyObject).toString('base64');

const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': API_PUBLIC_KEY,
'X-Api-Signature': signature,
},
body: JSON.stringify(message),
};

fetch(path, options).then(response => {
if (response.ok) {
return response.json();
}
throw new Error(`Request error: ${response.status} ${response.statusText}`);
}).then((response) => {
console.log('Successful response: ', response);
}).catch(error => {
console.error(error);
});

Important! See paragraphs 7 and 8 in the Manual of sending a request for advice on signing API requests.