cointokio/coingecko-api-client

Fetch data from the CoinGecko API, intended for use in WordPress plugins or themes.

Maintainers

Package info

github.com/cointokio/coingecko-api-client

pkg:composer/cointokio/coingecko-api-client

Statistics

Installs: 33

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2025-03-03 01:10 UTC

This package is auto-updated.

Last update: 2026-05-26 09:54:34 UTC


README

A PHP client for fetching data from the CoinGecko API intended to be used in WordPress plugins or themes. Being a good WordPress citizen, this client uses the WordPress core HTTP API to fetch its data.

Installation

This package is available on packagist.org and can be added as a dependency to your project via Composer.

composer require cointokio/coingecko-api-client

Use

Include the main class-client.php file and create an instance of the Client class and use its methods to fetch data from the CoinGecko API. Each method represents a separate API endpoint and will either return a list of response data or a WP_Error object. Note that access to the CoinGecko API requires an API key.

API key

There are two ways to supply your CoinGecko API key:

  • Pass the API key into the Client constructor (shown below). The client stores this value and passes it to each request class.
  • Define the PHP constant COINGECKO_API_KEY. Endpoint classes extend Request, which reads that constant when the key string passed from Client is empty—so new Cointokio\CoinGecko\Client() with no arguments still works if the constant is defined before you call any API methods.

API endpoint

CoinGecko exposes two hosts so the host must be selected explicitly:

  • Demo (free): https://api.coingecko.com/api/v3, authenticated with x-cg-demo-api-key.
  • Pro (paid): https://pro-api.coingecko.com/api/v3, authenticated with x-cg-pro-api-key.

The client defaults to the Demo host. If you have a Pro key, point the client at the Pro host so it switches to the x-cg-pro-api-key header automatically:

$client = new Cointokio\CoinGecko\Client(
	'your-pro-api-key',
	'https://pro-api.coingecko.com/api/v3'
);

As with the API key, the endpoint can also be supplied via a constant (COINGECKO_API_ENDPOINT) so new Cointokio\CoinGecko\Client() with no arguments works.

The auth header is only attached when the configured host is one of the two recognised CoinGecko hosts. Pointing the client at any other host (e.g. a local mock) deliberately suppresses the key header so credentials cannot leak to a third-party server.

Example — demo key, default host

require_once '/your/cool/path/to/coingecko-client/class-client.php';

$client = new Cointokio\CoinGecko\Client( 'your-demo-api-key' );

/*
 * Use the ping() method to check the CoinGecko API status.
 *
 * @see https://www.coingecko.com/api/documentations/v3#/ping
 */
$response = $client->ping();

Example — pro key, paid host

require_once '/your/cool/path/to/coingecko-client/class-client.php';

$client = new Cointokio\CoinGecko\Client(
	'your-pro-api-key',
	'https://pro-api.coingecko.com/api/v3'
);

$response = $client->ping();

The following example uses the $client->coins()->get_markets() method to fetch data from CoinGecko's /coins/markets endpoint. Note that access to the CoinGecko API requires an API key.

require_once '/your/cool/path/to/coingecko-client/class-client.php';

$client = new Cointokio\CoinGecko\Client( 'your-demo-api-key' );

/*
 * Get a list of price, market cap, volume and market-related data.
 *
 * @see https://www.coingecko.com/api/documentations/v3#/coins/get_coins_markets
 *
 * Note that the $client->coins() method returns an instance of the
 * \Cointokio\CoinGecko\Coins class. This allows us to use the Coins class methods
 * more easily via method chaining, eg.:
 *
 * $client->coins()->get_list()
 * $client->coins()->get_markets()
 *
 * @see https://github.com/cointokio/coingecko-api-client/blob/main/class-client.php#L41
 * @see https://github.com/cointokio/coingecko-api-client/blob/main/classes/class-coins.php
 * @see https://en.wikipedia.org/wiki/Method_chaining
 */
$response = $client->coins()->get_markets(
	'eur',
	array( 'ids' => 'btc, eth' )
);

Coding Standards

This project aims to adhere to the VIP Coding Standards.