DEV Community

Cover image for Using CCXT Implicit API methods
Neal Chambers
Neal Chambers

Posted on

Using CCXT Implicit API methods

I wrote about the invaluable CCXT library before, which can be used to access the common methods of an exchange's API. CCXT supports a whole host of unified methods out-of-the-box. CCXT does all the heavy lifting in terms of giving you a standard set of data from each exchange.

However, as we all know, each exchange can have its own quirks and special features. For instance, Binance, as well as several other exchanges, issues dividends if you choose to lock up certain crypto assets. For example, if you keep USDT on Binance, you can earn up to 10% APR for the first $2000.

There isn't a unified set of methods for retrieving or dealing with dividends built into CCXT, which means we need to use the exchange-specific methods or API endpoints to pull the data. Lucky for us, CCXT has a way of doing that.

To use any of the underlying endpoints you simply string together the parts of the endpoint to form a function, leaving out the version number. For example, the endpoint for dividends is /sapi/v1/asset/assetDividend, and this is a GET request as well so the function name becomes .sapiGetAssetAssetDividend(). Note the use of CamelCase and the addition of 'Get' between the name of the API and the endpoint.

It might be useful to look at the describe() function of the class for each exchange to get an idea of how these functions are formed. You can take a look at the one for Binance here.

Once you have the implicit API function, you will just need to pass the params for the endpoint in a Dict:

from ccxt import binance

client = binance(
  {
    "apiKey": "the_api_key_from_binance"
    "secret": "the_api_secret_from_binance"
  }
)

response = client.sapiGetAssetAssetDividend({"startTime":
    1656635948000, "endTime": 1658191192000, "limit": 500})

for dividend in response["rows"]:
    print(f"You received {dividend["amount"]} of
    {dividend["asset"] from {dividend["enInfo"]}.")
Enter fullscreen mode Exit fullscreen mode

Each endpoint has a different set of necessary and optional parameters that you can give it. You can check the details of what is required for the Dividend Endpoint here.

Most exchanges will have documentation for their API similar to Binance. Keep in mind that they may not be up-to-date, so you will want to sample the json response from an example call to see if it is what they say it is.

Another thing I learned is that the standard set of CCXT calls will sort returned data with the most recent record in the first position, and the oldest in the last position. However, Binance and some other exchanges returns this in the opposite order. Again, double-check your data.

Conclusion

CCXT will provide a very good set of standard calls to get and post data to exchanges, but exchanges come in all sorts of shapes and sizes. Sometimes, you will need to get under the hood and call some of the endpoints unique to the exchange. These implicit API calls help you do just that. Don't be afraid to get your hands a little dirty.

This post includes affiliate links; At no additional cost to you, I may receive compensation if you purchase products or services from the links provided in this article. Buying, selling, and trading crypto assets involves risk. Always Do Your Own Research before investing any money. This post is Not Financial Advice.

Top comments (0)