nodeidea
nodeidea

Reputation: 1

Send comma separated list to CoinMarketCap API for price conversion

Could anyone please tell me how to send a comma separated list of assets to CoinMarketCap for price conversion?

The following code works fine for a single asset. They instruct in their guidelines that you can:

'Pass up to 120 comma-separated fiat or cryptocurrency symbols to convert the source amount to' https://coinmarketcap.com/api/documentation/v1/#operation/getV2ToolsPriceconversion

I cannot find a way to send a list that is not either:

  1. rejected with a 400 response.
  2. Sends an individual call (and uses a credit) for every item in the list

Their guidelines generally suggest that you can send comma separated lists and receive results for multiple items in the list, USING ONLY ONE API CALL and ONLY 1 CREDIT.

This works fine for a single symbol, but not for a comma separated list as they suggest:

from requests import Session from requests.exceptions import ConnectionError, Timeout, TooManyRedirects

def usdPrice(assets):

url = 'https://pro-api.coinmarketcap.com/v2/tools/price-conversion'

parameters = {

    'amount' : 1,
    'symbol': assets,
    'convert' : 'USD'
    }

headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': cmcKey,
}

session = Session()
session.headers.update(headers)

try:

    response = session.get(url, params=parameters)
    print(response)
    data = response.json().get("data")
    print(data)

except (ConnectionError, Timeout, TooManyRedirects) as e:
    
    print(e)

assets = ['BTC','XRP','LTC'] usdPrice(assets)

Please note that this is not a solution either:

It sends a single call for every item in the list and uses a credit for every single one.

def usdPrice(assets):

url = 'https://pro-api.coinmarketcap.com/v2/tools/price-conversion'

for i in assets:

    parameters = {

        'amount' : 1,
        'symbol': i,
        'convert' : 'USD'
        }
    
    headers = {
    'Accepts': 'application/json',
    'X-CMC_PRO_API_KEY': cmcKey,
    }

    session = Session()
    session.headers.update(headers)

    try:

        response = session.get(url, params=parameters)
        print(response)
        data = response.json().get("data")
        print(data)

    except (ConnectionError, Timeout, TooManyRedirects) as e:
        
        print(e)

assets = ['BTC','XRP','LTC'] usdPrice(assets)

Upvotes: 0

Views: 699

Answers (1)

StephanT
StephanT

Reputation: 833

You are trying to convert FROM MANY to USD. The list input is only valid to convert one TO MANY.

In other words: your "symbol" parameter (source) is not allowed to be a list, but "convert" (target) is.

See the query parameters in the API documentation.

Upvotes: 0

Related Questions