> ## Documentation Index
> Fetch the complete documentation index at: https://exchanger-api.iexexchanger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Install the PHP SDK with Composer, verify API access, and recover from errors without creating duplicate orders.

The PHP SDK for Exchanger API v3 signs private requests with HMAC v2 and returns typed results with HTTP status and request identifiers.

## Install the SDK

Use PHP 8.5 with the `curl` extension. The built-in Guzzle HTTP client requires libcurl 8.3.0 or later.

Packagist provides the `dev-main` branch. Install it in the application that calls the exchanger.

```bash theme={null}
composer require iexexchanger/exchanger-sdk-php:dev-main
```

Keep the application's `composer.lock` so subsequent installs use the same SDK revision. With Composer's default stability setting, installing without a branch requires a stable release. [PHP SDK on Packagist](https://packagist.org/packages/iexexchanger/exchanger-sdk-php)

## Send a public ping

Save the example as `public-ping.php` in your project root. Replace `https://your-exchange` with your installation's HTTPS origin, for example by setting `EXCHANGER_API_ORIGIN`. Use the scheme, host, and optional port. The SDK adds the `/api/v3` path.

```php public-ping.php theme={null}
<?php

declare(strict_types=1);

use IEXExchanger\Sdk\Client;
use IEXExchanger\Sdk\Config;

require __DIR__ . '/vendor/autoload.php';

$origin = getenv('EXCHANGER_API_ORIGIN') ?: 'https://your-exchange';
$client = new Client(new Config(origin: $origin));

$response = $client->platform()->ping();

printf("HTTP %d | %s\n", $response->metadata->httpStatus, $response->result->status);
```

Run `php public-ping.php`. Expect HTTP `200` and an `ok` status. [Public ping](https://exchanger-api.iexexchanger.com/en/api-reference/system/get-ping.md)

## Verify your token and signature with a private ping

Create an [API key](https://exchanger-api.iexexchanger.com/en/start/access.md) on a test installation. Load the issued token and secret for the same key from your secret store into the process environment. Pass the token as issued. The SDK constructs the `Authorization: Bearer …` header. A valid key and signature are sufficient for `privatePing()`.

Save the example as `private-ping.php` next to the public one and run `php private-ping.php`.

```php theme={null}
<?php

declare(strict_types=1);

use IEXExchanger\Sdk\Api\RequestOptions;
use IEXExchanger\Sdk\Client;
use IEXExchanger\Sdk\Config;
use IEXExchanger\Sdk\Credentials;

require __DIR__ . '/vendor/autoload.php';

$origin = getenv('EXCHANGER_API_ORIGIN') ?: 'https://your-exchange';
$token = getenv('EXCHANGER_API_TOKEN');
$secret = getenv('EXCHANGER_API_HMAC_SECRET');

if ($token === false || $token === '' || $secret === false || $secret === '') {
    throw new RuntimeException('Set EXCHANGER_API_TOKEN and EXCHANGER_API_HMAC_SECRET.');
}

$client = new Client(new Config(
    origin: $origin,
    credentials: new Credentials(bearerToken: $token, hmacSecret: $secret),
    locale: 'en',
    timeoutSeconds: 30,
    connectTimeoutSeconds: 5,
));

$response = $client->platform()->privatePing(new RequestOptions(correlationId: 'sdk-private-ping'));

printf(
    "HTTP %d | %s | %s\n",
    $response->metadata->httpStatus,
    $response->result->status,
    $response->metadata->requestId ?? '-',
);
```

The SDK generates a fresh nonce and uses the current time for each private call. Keep the application server's clock synchronized. [Private ping](https://exchanger-api.iexexchanger.com/en/api-reference/system/get-private-ping.md) · [HMAC v2 authentication](https://exchanger-api.iexexchanger.com/en/start/authentication.md)

## Configure timeouts and diagnostics

* Set the response language with `Config(locale: ...)` and override it for one call with `RequestOptions(locale: ...)`. Available languages depend on the installation.
* The built-in Guzzle client uses `connectTimeoutSeconds` for the connection and `timeoutSeconds` for the whole request. They default to 5 and 30 seconds. For a test server on loopback, explicitly enable `allowInsecureLoopback: true`.
* When using your own PSR-18 transport, configure its timeouts, preserve the signed request bytes, and disable automatic redirects and retries.
* Use pagination for lists. `maxResponseBytes` and `maxResponseTokens` bound the size and complexity of the response. They default to 2 MiB and 50,000 JSON structural tokens.

## Handle errors and retries

The exception classes are in the `IEXExchanger\Sdk\Api\Exception` namespace.

| Exception | What to do |
| - | - |
| `ApiException` | The API returned a structured error. Use `errorCode` and `metadata->httpStatus` to decide how to recover. Respect `metadata->retryAfter` when rate limited if the value is available. |
| `ProtocolException` | The SDK received a response it could not process. Check `metadata->httpStatus`, the request identifiers, the URL, and the proxy settings. |
| `TransportException` | The HTTP exchange ended without a usable response. If the call is a write, check whether the exchanger completed the operation. |

The built-in transport leaves retry decisions to your application. For diagnostics, record the HTTP status, `requestId`, and `correlationId` from `metadata`. For `401`, check your token, secret, and clock. For `429` or `503`, follow the API delay and rate-limit rules. [API errors](https://exchanger-api.iexexchanger.com/en/guides/reliability/errors.md)

Before creating an order, save the operation key and the exact JSON string from `CreateOrderRequest::toJson()`. If you lose the response, look up the order first when you already have its ID. If the operation can be retried, pass the same data and key to `orders()->create($savedJson, $savedKey)`. The SDK generates a fresh signature. `metadata->idempotencyReplayed` reflects the `Idempotency-Replayed` header. Retention and recovery conditions depend on the operation. [Idempotency](https://exchanger-api.iexexchanger.com/en/guides/reliability/idempotency.md)

## More examples

* [PHP SDK repository and method reference](https://github.com/iexexchanger/exchanger-sdk-php)
* [Executable SDK examples](https://github.com/iexexchanger/exchanger-sdk-php/tree/main/examples)
* [Exchanger API reference](https://exchanger-api.iexexchanger.com/en/api.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.