Not sure if im being a bit silly with this or if the API just doesnt work.
I am using crypto.com exchange API to trade crypto and it works fine for buying and selling. But when I use the endpoint to check user balances if just get a response of BAD_REQUEST
My code is below:
require_once 'crypto_api.php';
// Function to fetch balances from Crypto.com API
function fetch_balances() {
global $api_key, $api_secret;
$nonce = round(microtime(true) * 1000);
$method = 'private/get-account-summary';
$params = []; // No additional parameters needed for this call
$sig = generate_signature($method, $id, $api_key, $params, $nonce, $api_secret);
$payload = json_encode($request_params);
$ch = curl_init('https://api.crypto.com/v2/private/get-account-summary');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Error: $http_codenResponse: $responsen";
$result = json_decode($response, true);
if (isset($result['error'])) {
echo "API Error: " . $result['error']['message'] . "n";
return $result['result']['accounts'] ?? [];
// Display balances in a readable format
function display_balances() {
$balances = fetch_balances();
echo "Failed to fetch balances or no balances available.n";
echo "Your Current Balances:n";
echo "----------------------n";
foreach ($balances as $balance) {
$currency = $balance['currency'];
$available = $balance['available'];
$reserved = $balance['reserved'];
echo "Currency: $currencyn";
echo "Available: $availablen";
echo "Reserved: $reservedn";
echo "----------------------n";
// Run the balance retrieval
<code><?php
require_once 'crypto_api.php';
// Function to fetch balances from Crypto.com API
function fetch_balances() {
global $api_key, $api_secret;
$nonce = round(microtime(true) * 1000);
$method = 'private/get-account-summary';
$id = $nonce;
$params = []; // No additional parameters needed for this call
// Generate signature
$sig = generate_signature($method, $id, $api_key, $params, $nonce, $api_secret);
$request_params = [
'id' => $id,
'method' => $method,
'api_key' => $api_key,
'nonce' => $nonce,
'params' => $params,
'sig' => $sig
];
$payload = json_encode($request_params);
// Make API call
$ch = curl_init('https://api.crypto.com/v2/private/get-account-summary');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code != 200) {
echo "HTTP Error: $http_codenResponse: $responsen";
return null;
}
$result = json_decode($response, true);
if (isset($result['error'])) {
echo "API Error: " . $result['error']['message'] . "n";
return null;
}
return $result['result']['accounts'] ?? [];
}
// Display balances in a readable format
function display_balances() {
$balances = fetch_balances();
if (!$balances) {
echo "Failed to fetch balances or no balances available.n";
return;
}
echo "Your Current Balances:n";
echo "----------------------n";
foreach ($balances as $balance) {
$currency = $balance['currency'];
$available = $balance['available'];
$reserved = $balance['reserved'];
echo "Currency: $currencyn";
echo "Available: $availablen";
echo "Reserved: $reservedn";
echo "----------------------n";
}
}
// Run the balance retrieval
display_balances();
?>
</code>
<?php
require_once 'crypto_api.php';
// Function to fetch balances from Crypto.com API
function fetch_balances() {
global $api_key, $api_secret;
$nonce = round(microtime(true) * 1000);
$method = 'private/get-account-summary';
$id = $nonce;
$params = []; // No additional parameters needed for this call
// Generate signature
$sig = generate_signature($method, $id, $api_key, $params, $nonce, $api_secret);
$request_params = [
'id' => $id,
'method' => $method,
'api_key' => $api_key,
'nonce' => $nonce,
'params' => $params,
'sig' => $sig
];
$payload = json_encode($request_params);
// Make API call
$ch = curl_init('https://api.crypto.com/v2/private/get-account-summary');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code != 200) {
echo "HTTP Error: $http_codenResponse: $responsen";
return null;
}
$result = json_decode($response, true);
if (isset($result['error'])) {
echo "API Error: " . $result['error']['message'] . "n";
return null;
}
return $result['result']['accounts'] ?? [];
}
// Display balances in a readable format
function display_balances() {
$balances = fetch_balances();
if (!$balances) {
echo "Failed to fetch balances or no balances available.n";
return;
}
echo "Your Current Balances:n";
echo "----------------------n";
foreach ($balances as $balance) {
$currency = $balance['currency'];
$available = $balance['available'];
$reserved = $balance['reserved'];
echo "Currency: $currencyn";
echo "Available: $availablen";
echo "Reserved: $reservedn";
echo "----------------------n";
}
}
// Run the balance retrieval
display_balances();
?>
I’ve even filtered this through AI to help with a resolution and still no dice. Absolutely no idea what I could be missing here and any help would be greatly appreciated.
EDIT: I have edited to include the error message exactly as I receive it:
HTTP Error: 400 Response: {“code”:”10004″,”msg”:”BAD_REQUEST”}