You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
useUcubix\PhpClient\Exceptions\ApiException;
useUcubix\PhpClient\Exceptions\AuthenticationException;
useUcubix\PhpClient\Exceptions\RateLimitException;
useUcubix\PhpClient\Exceptions\ValidationException;
try {
$order = $client->createOrder($uuid, 5, 'InvalidRegion');
} catch (AuthenticationException$e) {
// 401 Unauthorized or 403 Forbidden// Invalid API key or IP not whitelistedecho$e->getMessage();
echo$e->getCode(); // 401 or 403
} catch (ValidationException$e) {
// 422 Unprocessable Entityecho$e->getMessage(); // e.g. "Quantity must not be greater than 1."echo$e->field; // field name if provided
} catch (RateLimitException$e) {
// 429 Too Many Requests (after all retries exhausted)echo$e->retryAfter; // seconds to wait
} catch (ApiException$e) {
// All other errors (400, 404, 500, etc.)echo$e->getMessage();
echo$e->getCode();
echo$e->errorKey;
echo$e->errorDetail;
}
The client has a dual-layer rate limiting system, matching the SharpAPI php-core pattern.
1. Client-side Sliding Window
Proactive throttling: the client tracks request timestamps in a sliding window and blocks (via usleep + 50ms buffer) before exceeding the configured requests per minute. This prevents hitting the server limit.
// Check/configure requests per minute (default: 100)$client->getRequestsPerMinute(); // 100$client->setRequestsPerMinute(50); // slow down$client->setRequestsPerMinute(0); // disable client-side throttling// Direct access to the rate limiter$limiter = $client->getRateLimiter();
$limiter->canProceed(); // non-blocking check$limiter->remaining(); // slots left in current window
2. Server-side 429 Retry
Reactive handling: if the server returns 429 Too Many Requests, the client automatically retries up to 3 times, respecting the Retry-After header.
// Configure max retries (default: 3)$client->setMaxRetryOnRateLimit(5);
3. Server Header Tracking
After every response, the client reads X-RateLimit-Limit and X-RateLimit-Remaining headers.
$client->getRateLimitLimit(); // e.g. 100$client->getRateLimitRemaining(); // e.g. 87$client->canMakeRequest(); // true if remaining > 0
4. Server-side Limit Adaptation
If the server reports a higher limit via X-RateLimit-Limit header, the client automatically adapts its sliding window upward (one-way ratchet — never decreases).
5. State Persistence
Export/restore rate limit state for caching across requests: