coingecko-api icon indicating copy to clipboard operation
coingecko-api copied to clipboard

How to add api key to request?

Open bernhardh opened this issue 2 years ago • 3 comments

We have now a paid plan of coingecko, but I can't find an option, to add my new API Key to the request.

Any ideas?

bernhardh avatar Mar 26 '22 10:03 bernhardh

Paid subscriptions also have a different base url: https://pro-api.coingecko.com/api/v3/ping?x_cg_pro_api_key=YOUR_API_KEY

While the class CoinGeckoClient has BASE_URI = 'https://api.coingecko.com';

Any easy way to do this with this client?

pattihis avatar May 11 '22 00:05 pattihis

Hi! I just create fork with this functionality, check out https://github.com/LborV/coingecko-api

LborV avatar Jan 13 '23 11:01 LborV

Solution

  1. Make a change in code in a file CoinGeckoClient.php: From:

protected const BASE_URI = 'https://api.coingecko.com';

To:

protected` const BASE_URI = 'https://pro-api.coingecko.com';

  1. Make a change in a file Api.php, in function get():
public function get(string $uri, array $query = []): array
{
    $query['x_cg_pro_api_key'] = 'CG-XXXXXXXXXXXXX;
    $response = $this->client->getHttpClient()->request('GET', '/api/' . $this->version
        . $uri, ['query' => $query]);
    $this->client->setLastResponse($response);

    return $this->transformer->transform($response);
}

/*******/ Alternative way, without changes in Api.php: Adding API Key Add this as an additional parameter. For example:

$parameters = array( "ids" => $id_crypto_coin, "order" => "volume_desc", "per_page" => $per_page, "sparkline" => "true", "price_change_percentage" => "1h,24h,7d", "x_cg_pro_api_key" => "CG-XXXXXXXXXXX" ); $data = $myCoinGeckoClient->coins()->getMarkets($national_currency, $parameters);

And that's it.

Explanation Request is sent as a string query: https://pro-api.coingecko.com/api/v3/coins/markets?vs_currency=usd&x_cg_pro_api_key=CG-XXXXXXXXXXXXXXXXX&order=market_cap_desc&per_page=100&page=1&sparkline=false&locale=en and API key can be add in any part of the query string. image

Warnings! This library is not complete. Here is an example from Ping.php. Now is: public function ping(): array { return $this->get('/ping'); } It should be: public function ping( array $params = [] ): array { return $this->get('/ping', $params); } Just change it in your project if you want to use Ping request with Coin Gecko PRO. If you do not use Ping at all, you do not need to make this change.

Another example comes from Coins.php. Now is: public function getMarketChart(string $id, string $vsCurrency, string $days): array { $params['vs_currency'] = $vsCurrency; $params['days'] = $days; return $this->get('/coins/' . $id . '/market_chart', $params); }

Should be: public function getMarketChart(string $id, string $vsCurrency, string $days, array $params = [] ): array { $params['vs_currency'] = $vsCurrency; $params['days'] = $days; return $this->get('/coins/' . $id . '/market_chart', $params); }

dariuszwit avatar Apr 27 '23 00:04 dariuszwit