Skip to main content

PHP SDK

Installation

composer require flexops/sdk

Quick Start

use FlexOps\Client;

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

$rates = $client->rates->get([
'from' => ['zip' => '80202', 'country' => 'US'],
'to' => ['zip' => '10001', 'country' => 'US'],
'parcel' => ['weight' => 2.5, 'length' => 10, 'width' => 8, 'height' => 4],
]);

foreach ($rates->rates as $rate) {
echo "{$rate->carrier} {$rate->service}: \${$rate->totalPrice}\n";
}

Configuration

$client = new Client([
'apiKey' => 'your-api-key',
'baseUrl' => 'https://api.flexops.io/v1', // optional
'timeout' => 30, // seconds
'maxRetries' => 3,
]);

Shipping Operations

Create a Label

$label = $client->labels->create([
'carrier' => 'usps',
'service' => 'priority',
'from' => [
'name' => 'Warehouse',
'street1' => '123 Main St',
'city' => 'Denver',
'state' => 'CO',
'zip' => '80202',
'country' => 'US',
],
'to' => [
'name' => 'Customer',
'street1' => '456 Broadway',
'city' => 'New York',
'state' => 'NY',
'zip' => '10001',
'country' => 'US',
],
'parcel' => ['weight' => 2.5, 'length' => 10, 'width' => 8, 'height' => 4],
], idempotencyKey: 'order-12345');

echo $label->trackingNumber;
echo $label->labelUrl;

Track a Shipment

$tracking = $client->tracking->get('9400111899223456789012');

echo $tracking->status;
echo $tracking->estimatedDelivery;

foreach ($tracking->events as $event) {
echo "{$event->timestamp}: {$event->description}\n";
}

Validate an Address

$result = $client->addresses->validate([
'street1' => '123 Main St',
'city' => 'Denver',
'state' => 'CO',
'zip' => '80202',
'country' => 'US',
]);

if ($result->isValid) {
echo "Validated: " . json_encode($result->normalized);
}

Webhook Verification

$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_FLEXOPS_SIGNATURE'] ?? '';

if (!FlexOps\Webhook::verify($payload, $signature, 'whsec_your_secret')) {
http_response_code(401);
exit('Invalid signature');
}

$event = json_decode($payload, true);
if ($event['type'] === 'tracking.delivered') {
handleDelivery($event['data']);
}

http_response_code(200);
echo 'OK';

Error Handling

use FlexOps\Exception\RateLimitException;
use FlexOps\Exception\FlexOpsException;

try {
$label = $client->labels->create([...]);
} catch (RateLimitException $e) {
echo "Retry after {$e->retryAfter} seconds";
} catch (FlexOpsException $e) {
echo "{$e->getCode()}: {$e->getMessage()}";
}