PHP code example of happyslucker / changenow-api

1. Go to this page and download the library: Download happyslucker/changenow-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

happyslucker / changenow-api example snippets


use ChangeNow\ChangeNowClient;

$client = new ChangeNowClient('your-api-key');

$currencies = $client->common()->getCurrencies();

foreach ($currencies as $currency) {
    echo $currency->ticker . ' — ' . $currency->name . PHP_EOL;
}

use ChangeNow\ChangeNowClient;
use ChangeNow\Model\Request\CreateExchangeRequest;

$client = new ChangeNowClient('your-api-key');

// Step 1: Estimate
$estimate = $client->standardFlow()->getEstimatedAmount('btc', 'eth', '1');
echo "You'll receive: {$estimate->estimatedAmount} ETH" . PHP_EOL;

// Step 2: Create exchange
$request = new CreateExchangeRequest(
    fromCurrency: 'btc',
    toCurrency: 'eth',
    address: '0xRecipientAddress',
    fromAmount: '1',
    refundAddress: '1MyRefundAddress',
);
$exchange = $client->standardFlow()->createExchange($request);

echo "Exchange ID: {$exchange->id}" . PHP_EOL;
echo "Send BTC to: {$exchange->payinAddress}" . PHP_EOL;

use ChangeNow\ChangeNowClient;
use ChangeNow\Model\Request\CreateExchangeRequest;

$client = new ChangeNowClient('your-api-key');

$request = new CreateExchangeRequest(
    fromCurrency: 'btc',
    toCurrency: 'eth',
    address: '0xRecipient',
    fromAmount: '1',
    toAmount: '15.5',          // exact desired output
    flow: 'fixed-rate',
    type: 'reverse',
);

$exchange = $client->fixedRateFlow()->createExchange($request);
echo "{$exchange->id} — send {$exchange->amountExpectedFrom} BTC";

$result = $client->validate()->address('btc', '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');

echo $result->result
    ? '✓ Address is valid'
    : "✗ Invalid: {$result->message}";

$tx = $client->common()->getTransactionStatus('abc123');

echo $tx->status->value;           // enum-backed string
echo $tx->fromCurrency;            // string
echo $tx->amountExpectedTo;       // string (numeric, atomic precision)

use ChangeNow\Exception\ChangeNowException;

try {
    $client->common()->getCurrencies();
} catch (ChangeNowException $e) {
    echo "API error [{$e->getCode()}]: {$e->getMessage()}";
}