PHP code example of cotizave / sdk

1. Go to this page and download the library: Download cotizave/sdk 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/ */

    

cotizave / sdk example snippets


use Cotizave\Cotizave;

// Reads COTIZAVE_API_KEY from the environment; or pass ['apiKey' => '...'].
$client = new Cotizave();

// Every rate published for a country.
$response = $client->rates->list('AR');
foreach ($response['rates'] as $rate) {
    echo $rate['id'], ' ', $rate['mid'] ?? '', PHP_EOL;
}

// A single rate by its canonical id.
$trm = $client->rates->get('CO', 'co-trm');
echo $trm['mid'];

// A currency conversion (resolved on Cotizave, never computed client-side).
$result = $client->convert(['from' => 'USD', 'to' => 'BRL', 'amount' => 100]);
echo $result['to']['amount'];

$client = new Cotizave(['apiKey' => 'ctz_live_...']); // explicit
$client = new Cotizave();                              // from COTIZAVE_API_KEY

foreach ($client->history->iterate('VE', 've-bcv', ['maxPages' => 10]) as $point) {
    echo $point['date'], ' ', $point['mid'] ?? '', PHP_EOL;
}

use Cotizave\Cotizave;
use Cotizave\Exception\CotizaveException;
use Cotizave\Exception\NotFoundException;
use Cotizave\Exception\RateLimitException;

$client = new Cotizave();
try {
    $rate = $client->rates->get('CO', 'co-trm');
} catch (RateLimitException $e) {
    echo $e->isQuotaExhausted() ? 'Monthly quota exhausted' : "Slow down; retry after {$e->retryAfter()}s";
} catch (NotFoundException $e) {
    echo 'No such rate';
} catch (CotizaveException $e) {
    echo $e->status, ' ', $e->errorCode, ' ', $e->getMessage();
}

use Cotizave\Cotizave;

$client = new Cotizave([
    'apiKey' => 'ctz_live_...',
    'timeout' => 5.0,              // per-request timeout, seconds
    'historyTimeout' => 10.0,      // longer timeout for history reads
    'maxRetries' => 2,             // extra retries for eligible requests
    'maxRetryDelay' => 10.0,       // total backoff budget per call, seconds
    'maxResponseBytes' => 25_000_000,
    'language' => 'es',            // Accept-Language for localized names
    'headers' => ['X-Trace-Id' => '...'],
    'retry' => [
        'baseDelay' => 0.25,
        'maxDelay' => 2.0,
        'retryRateLimited' => true,
        'onRetry' => fn ($e) => error_log("retrying {$e->path} in {$e->delay}s"),
    ],
    'onResponse' => fn ($e) => error_log("{$e->meta->status} remaining={$e->meta->rateLimit->remaining}"),
]);

$client->rates->list('AR', [], ['timeout' => 2.0, 'maxRetries' => 0]);

$csv = $client->export('/v2/fx/ar/rates', 'csv');

$result = $client->request('GET', '/v2/fx/co/rates/co-trm', ['ifNoneMatch' => '"abc"']);
echo $result->notModified, ' ', $result->meta->status;
var_dump($result->data);

use Cotizave\Cotizave;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;

$mock = new MockHandler([new Response(200, [], '{"id":"co-trm"}')]);
$client = new Cotizave(['handler' => $mock]);