Building cryptocurrency analytics dashboards, liquidation trackers, or monitoring funding rate arbitrage often requires reliable access to high-quality market intelligence data. The Coinglass platform provides one of the most comprehensive APIs for derivatives, ETFs, and on-chain analytics. However, integrating REST APIs and WebSocket streams from scratch can be a real headache for developers: writing endless boilerplate, managing HTTP clients, handling errors, and dealing with rate limits.
Fortunately, a robust solution has emerged for PHP developers. In this article, we will take a deep dive into the coinglass-php package—a modern, framework-agnostic PHP SDK for the Coinglass API v4 that features first-class Laravel integration [1].
What is coinglass-php?
The coinglass-php package (developed by Igor Sazonov) serves as a comprehensive wrapper for all six endpoint groups of the Coinglass API v4: Futures, Spot, Options, ETF, On-Chain, and Indicators [1]. Furthermore, it includes a built-in WebSocket client for handling real-time data streams natively.
The package was born out of a developer's frustration with hand-rolling Guzzle calls and juggling raw arrays while building a liquidation dashboard. The result is a strictly typed, PSR-18 compliant client that easily drops into any PHP 8.1+ project, or directly into Laravel (versions 10 through 13) [1].
Key Features of the Package
1. Complete API Coverage
The package supports absolutely every endpoint offered by Coinglass v4. You won't have to manually implement missing methods. Out of the box, you can retrieve open interest history, liquidation data, order books, Bitcoin ETF flows, the Fear & Greed index, and much more [1].
2. Built-in WebSocket Client (No Extra Dependencies)
To work with the Coinglass WebSocket API (which streams liquidations, spot/futures trades, and tickers), you do not need to install heavy third-party libraries like Ratchet or ReactPHP. The client is built natively on top of standard PHP streams (stream_socket_client) and the ext-openssl extension [1]. Subscribing to channels and maintaining the connection—including sending the required "ping" heartbeat every 20 seconds—is handled completely automatically [1].
3. Framework-Agnostic and PSR-18 Swappable
While the package boasts excellent Laravel support, it remains entirely independent of it. It uses Guzzle (^7.4) by default, but thanks to the PSR-18 standard, you can swap it out and inject any HTTP client of your preference [1].
4. Automatic Retry Mechanism
The Coinglass API enforces strict rate limits (HTTP 429 Too Many Requests). coinglass-php handles this elegantly: it automatically retries rate-limited requests using exponential backoff, respectfully honoring the Retry-After header when the server provides one [1].
5. Strict Type Hydration (DTOs)
Say goodbye to unpredictable associative arrays. Every API response is hydrated into a CoinGlassDto object (for a single record) or a CoinGlassCollection (for lists) [1]. You can access fields using whatever syntax feels most natural: property syntax ($dto->openInterestUsd), array syntax ($dto['openInterestUsd']), or getter methods ($dto->get('openInterestUsd')). If you ever need the untouched raw payload, it is always available via $dto->raw or $dto->toArray() [1].
6. Clear Exception Hierarchy
Instead of throwing a single catch-all Exception, the package provides a clear and purposeful exception hierarchy: UnauthorizedException, NotFoundException, RateLimitException, and a generic ApiException for all other API-related errors [1]. This makes writing fault-tolerant code much simpler.
Laravel Integration: Ready Out of the Box
For users of Laravel (versions 10, 11, 12, and 13), the package provides maximum convenience. After installing via Composer (composer require tigusigalpa/coinglass-php), you can publish the configuration file:
php artisan vendor:publish --tag=coinglass-config
Configuration is then easily managed through your .env file:
COINGLASS_API_KEY=your-api-key
COINGLASS_BASE_URL=https://open-api-v4.coinglass.com
COINGLASS_TIMEOUT=15
COINGLASS_RETRY_ATTEMPTS=3
COINGLASS_RETRY_DELAY=1
You can leverage Dependency Injection to receive the CoinGlassClient in your controllers, or utilize the elegant CoinGlass facade:
use Tigusigalpa\CoinGlass\Laravel\Facades\CoinGlass;
// Fetching BTC open interest history for the last 30 days
$oi = CoinGlass::futures()->openInterestOhlcHistory('BTC', '1d', 30);
Practical Usage Examples
The client's API is logically divided into endpoint groups. Here are a few examples demonstrating how effortless it is to retrieve the data you need:
Working with Futures:
$client = CoinGlassClient::make('YOUR_API_KEY');
// Funding rate exchange list
$funding = $client->futures()->fundingRateExchangeList('BTC');
// Liquidation history
$liq = $client->futures()->liquidationHistory('BTC', 'BTCUSDT', '1h');
Fetching ETF Data:
// Bitcoin ETF flows over the last 24 weeks
$flows = $client->etf()->bitcoinFlowHistory('1w', 24);
Real-time WebSocket Streaming:
use Tigusigalpa\CoinGlass\WebSocket\Channels;
use Tigusigalpa\CoinGlass\WebSocket\Message;
$stream = $client->websocket()->connect();
$stream->subscribe(
Channels::liquidationOrders(),
Channels::futuresTicker('Binance', 'BTCUSDT'),
);
$stream->listen(function (Message $message) {
if ($message->channel === Channels::liquidationOrders()) {
foreach ($message->collection() as $order) {
echo "{$order->exchange} {$order->symbol} liquidated \${$order->volume_usd}\n";
}
}
});
The listen() method blocks execution forever, making it a perfect fit for long-running CLI workers or queued Laravel jobs [1].
Conclusion
The coinglass-php package is a prime example of what a modern PHP API client should look like. It abstracts away all the tedious boilerplate related to HTTP requests, rate limiting, and WebSocket connections, providing developers with a clean, typed, and predictable interface.
Whether you are building a complex algorithmic trading system in vanilla PHP or developing a crypto analytics portal in Laravel 11, this package will save you hours—if not days—of development time.
The code quality is backed by solid test coverage (using PHPUnit and Orchestra Testbench for Laravel) and leverages the modern features of PHP 8.1+ [2]. If your project involves crypto market intelligence, coinglass-php absolutely deserves a spot in your composer.json.
References
[1] tigusigalpa/coinglass-php GitHub Repository
[2] tigusigalpa/coinglass-php Packagist Page
Top comments (0)