Skip to main content

Send a request

In this article, you can learn how to send a request to Fiat API.

General flow of sending a request

The section contains a general flow of sending a request based on Node.js.

  1. Find a module that supports an RSA signature generation with the SHA256 hashing algorithm. For Node.js, it is a crypto module.

  2. Find a module that allows sending HTTP requests. For Node.js, it is a node-fetch module.

  3. Install the necessary modules in any convenient way. For example, with npm:

npm install crypto
npm install node-fetch
  1. Import the modules to your code:
const crypto = require('crypto');
const fetch = require('node-fetch');
  1. Define the variables for your private and public keys:
const API_PUBLIC_KEY = '<Your public API key>';
const API_PRIVATE_KEY = '<Your private API key>';
  1. Create a private key object with the following type, format, and encoding:
const privateKeyObject = crypto.createPrivateKey({
key: API_PRIVATE_KEY,
type: 'pkcs1',
format: 'pem',
encoding: 'base64',
});
  1. Define the variables for the URL and body message of the API method you want to request:
const path = 'https://fiat-api.changelly.com/v1/validate-address';
const message = {
"currency": "XRP",
"walletAddress": "rwpMvfxood******************WDwQyHUW",
"walletExtraId": "1234hg"
};

To provide path or query request parameters, specify them in the path variable:

const path = 'https://fiat-api.changelly.com/v1/currencies?type=crypto';
warning

This message example is applied to the POST request. If you want to send GET request, you should make the message empty:

const message = {};

If you delete the message variable, you encounter an error.

  1. Concatenate the path and the message object into one string:
const payload = path + JSON.stringify(message);
  1. Create a signature used as X-Api-Signature header:
const signature = crypto.sign('sha256', Buffer.from(payload), privateKeyObject).toString('base64');

crypto.sign takes as input 3 parameters:

  • hashing algorithm – sha256.
  • request data – Buffer.from(payload).
  • private key object in the desired format – privateKeyObject.

crypto.sign returns signature in a buffer. You need to convert it to base64 format.

  1. Create an options object that contains info for an API request, such as a HTTP method, headers, and body:
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': API_PUBLIC_KEY,
'X-Api-Signature': signature,
},
body: JSON.stringify(message),
};
  1. Send a request:
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);
});
Full Node.js sample
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": "rwpMvfxood******************WDwQyHUW",
"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);
});

Samples for sending a request

Feel free to copy the following PHP, Python, and Java samples.

info

Requires PHP 8.

<?php
$API_PUBLIC_KEY = '<Your public API key>';
$API_PRIVATE_KEY = '<Your private API key>';

$private_key_object = base64_decode($API_PRIVATE_KEY);
$private_key_object = openssl_pkey_get_private($private_key_object);

$path = 'https://fiat-api.changelly.com/v1/validate-address';

$message = [
"currency" => "XRP",
"walletAddress" => "rwpMvfxood******************WDwQyHUW",
"walletExtraId" => "1234hg"
];

$json_body = json_encode($message, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES);
$payload = $path . $json_body;
$request_body = $json_body;

openssl_sign($payload, $signature, $private_key_object, OPENSSL_ALGO_SHA256);
$signature = base64_encode($signature);

$request_headers = [
'Content-Type: application/json',
'X-Api-Key: ' . $API_PUBLIC_KEY,
'X-Api-Signature: ' . $signature,
];

$request = curl_init($path);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($request, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, $request_headers);

$response = curl_exec($request);

$http_status = curl_getinfo($request, CURLINFO_HTTP_CODE);

curl_close($request);

if ($http_status >= 200 && $http_status < 300) {
echo 'Successful response: ' . $response;
} else {
echo 'Request error: ' . $http_status . ' ' . $response;
}
?>