-
Notifications
You must be signed in to change notification settings - Fork 0
Installation and Configuration
Getting started with coinglass-php requires a Coinglass API v4 key, PHP 8.1 or higher, and Composer. The package is available on Packagist and installs in a single command.
Install the package via Composer:
composer require tigusigalpa/coinglass-phpGuzzle 7.4+ is installed automatically as the default HTTP client. If you intend to use the WebSocket API over a secure wss:// connection, ensure the ext-openssl PHP extension is enabled in your environment.
If you are developing against a local checkout of the package, add a path repository to your project's composer.json before requiring the package:
{
"repositories": [
{ "type": "path", "url": "../packages/coinglass-php" }
]
}The CoinGlassConfig class is an immutable value object that holds all configuration for the SDK. It accepts the API key, the base URL, the request timeout, and the retry policy. You can construct it directly, load it from a plain array, or read it from environment variables.
For standalone usage, the simplest approach is the CoinGlassClient::make() factory method, which accepts an API key and an optional array of overrides:
use Tigusigalpa\CoinGlass\CoinGlassClient;
$client = CoinGlassClient::make('YOUR_API_KEY');For full control over every parameter, construct a CoinGlassConfig object explicitly and pass it to the client constructor:
use Tigusigalpa\CoinGlass\CoinGlassClient;
use Tigusigalpa\CoinGlass\CoinGlassConfig;
$config = new CoinGlassConfig(
apiKey: 'YOUR_API_KEY',
baseUrl: 'https://open-api-v4.coinglass.com',
timeout: 15.0,
retryAttempts: 3,
retryDelay: 1.0,
);
$client = new CoinGlassClient($config);You can also load configuration from environment variables using CoinGlassConfig::fromEnv(). The table below lists all recognized variables and their defaults.
| Environment Variable | Config Key | Default |
|---|---|---|
COINGLASS_API_KEY |
apiKey |
(required) |
COINGLASS_BASE_URL |
baseUrl |
https://open-api-v4.coinglass.com |
COINGLASS_TIMEOUT |
timeout |
15.0 |
COINGLASS_RETRY_ATTEMPTS |
retryAttempts |
3 |
COINGLASS_RETRY_DELAY |
retryDelay |
1.0 |
use Tigusigalpa\CoinGlass\CoinGlassClient;
use Tigusigalpa\CoinGlass\CoinGlassConfig;
$client = new CoinGlassClient(CoinGlassConfig::fromEnv());Because the SDK is built on PSR-18 interfaces, you can inject any compatible HTTP client. Pass your custom client, request factory, stream factory, and URI factory to the CoinGlassClient constructor:
use Tigusigalpa\CoinGlass\CoinGlassClient;
use Tigusigalpa\CoinGlass\CoinGlassConfig;
$client = new CoinGlassClient(
config: CoinGlassConfig::fromEnv(),
httpClient: $myPsr18Client,
requestFactory: $myRequestFactory,
streamFactory: $myStreamFactory,
uriFactory: $myUriFactory,
);coinglass-php ships with a Laravel service provider that is auto-discovered by Laravel's package discovery mechanism. No manual registration is required for Laravel 10 and above.
Add your credentials to the .env file. The table below lists all supported variables, including those for the WebSocket client.
| Variable | Description | Default |
|---|---|---|
COINGLASS_API_KEY |
Your Coinglass API v4 key | (required) |
COINGLASS_BASE_URL |
REST API base URL | https://open-api-v4.coinglass.com |
COINGLASS_TIMEOUT |
Request timeout in seconds | 15.0 |
COINGLASS_RETRY_ATTEMPTS |
Max retries on HTTP 429 | 3 |
COINGLASS_RETRY_DELAY |
Base backoff delay in seconds | 1.0 |
COINGLASS_WS_BASE_URL |
WebSocket endpoint URL | wss://open-ws.coinglass.com/ws-api |
COINGLASS_WS_CONNECT_TIMEOUT |
WebSocket connection timeout in seconds | 10.0 |
COINGLASS_WS_PING_INTERVAL |
Heartbeat ping interval in seconds | 20.0 |
While not required, you can publish the configuration file to config/coinglass.php to manage settings directly in code:
php artisan vendor:publish --tag=coinglass-configThe CoinGlass facade provides the most concise way to call the API in a Laravel application:
use Tigusigalpa\CoinGlass\Laravel\Facades\CoinGlass;
$oi = CoinGlass::futures()->openInterestOhlcHistory('BTC', '1d', 30);The CoinGlassClient is registered as a singleton in the Laravel service container, so you can inject it directly into any controller, job, or service class:
namespace App\Http\Controllers;
use Tigusigalpa\CoinGlass\CoinGlassClient;
class MarketController extends Controller
{
public function __construct(
private readonly CoinGlassClient $coinGlass
) {}
public function index()
{
$flows = $this->coinGlass->etf()->bitcoinFlowHistory('1w', 24);
return view('market.index', ['flows' => $flows]);
}
}Next: REST API Usage