Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add api key to request? #16

Open
bernhardh opened this issue Mar 26, 2022 · 3 comments
Open

How to add api key to request? #16

bernhardh opened this issue Mar 26, 2022 · 3 comments

Comments

@bernhardh
Copy link

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?

@pattihis
Copy link

pattihis commented May 11, 2022

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?

@LborV
Copy link

LborV commented Jan 13, 2023

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

@dariuszwit
Copy link

dariuszwit commented Apr 27, 2023

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); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants