'How to request the data of only one cryptocurrency using CoinMarketCap api?
i know how to request data of many cryptocurrencies using CoinMarketCap api by setting start and limit params but, it is possible to request the data of only one cryptocurrency? by example bitcoin. I can do it iterating the list of the result that i obtain making a request with start and limit params but i wouldn't like to do it for obvious performance reasons. I just don't find the endpoint to do it in coinmarketcap´s api documentation
Solution 1:[1]
If you want to get the price data:
url = 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion'
parameters = {
'amount':'1',
'symbol':'BTC',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': CMC_KEY,
}
Get your API key: https://pro.coinmarketcap.com/
Solution 2:[2]
I have resolved my own trouble though i'm not sure if it is the best solution. You can request the price of only one cryptocurrency using v1/tools/price-conversion
endpoint, you just especify in headers the amount and symbol. For more information there is the part of the documentation: https://coinmarketcap.com/api/documentation/v1/#operation/getV1ToolsPriceconversion
Solution 3:[3]
It depends on what data you want to get.
Bellow two options Latest Price and Metadata.
Latest price
https://coinmarketcap.com/api/documentation/v1/#operation/getV2CryptocurrencyQuotesLatest
Query Parameters
id
:string => one or more comma-separated IDs. These are the CMC IDs Example 1,2.slug
:string => one or more comma-separated slugs. This are the slugs Example: "bitcoin"symbol
:string =>one or more comma-separated symbol. Example: 'BTC,ETC'
You need one of the above three id
, slugs
, or symbols
to make a request. CMC recommends id
, but the other options work without problem.
Optional parameters:
convert
:stringconvert_id
:stringaux
:string
aux
. by default it pass a string value of"num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply,is_active,is_fiat"
you can customize the reponse removing some of the attributes.
import os
import requests
import json
BASE_URL = 'https://pro-api.coinmarketcap.com'
endpoint = '/v2/cryptocurrency/quotes/latest'
url = BASE_URL + endpoint
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': os.environ['CMC_API'],
}
params = {
'id': '1',
'aux': 'num_market_pairs,cmc_rank,date_added,max_supply,circulating_supply,total_supply,is_active,is_fiat'
}
session = requests.Session()
session.headers.update(headers)
response = session.get(url, params=params)
print(response.text)
data = json.loads(response.text)
with open('btc_info.json', 'w') as file:
json.dump(data, file, indent=6)
Metadata
Metadata like logos, address, Twitter, Reddit related links
Query Parameters
id
:string => one or more comma-separated IDs. These are the CMC IDs.slug
:string => (Alternatively) pass a comma-separated list of cryptocurrency slugs. Example: "bitcoin,ethereum"symbol
:string => (Alternatively) pass one or more comma-separated cryptocurrency symbols. Example: "BTC,ETH".
At least one "id" or "slug" or "symbol" is required for this request.
Optional parameters:
address
:string => Contract address, Example: "0xc40af1e4fecfa05ce6bab79dcd8b373d2e436c4e"aux
:string => "urls,logo,description,tags,platform,date_added,notice"
import json
import os
import requests
BASE_URL = 'https://pro-api.coinmarketcap.com'
endpoint = '/v2/cryptocurrency/info'
url = BASE_URL + endpoint
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': os.environ['CMC_API'],
}
params = {
'id': '1',
'aux': 'num_market_pairs,cmc_rank,date_added,max_supply,circulating_supply,total_supply,is_active,is_fiat'
}
session = requests.Session()
session.headers.update(headers)
response = session.get(url, params=params)
print(response.text)
data = json.loads(response.text)
with open('btc_info.json', 'w') as file:
json.dump(data, file, indent=6)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anarkrypto |
Solution 2 | matt020java |
Solution 3 | Victor Andres Aguirre Fernande |