Working with CCXT Python Library
Tags: python, api, programming
Published: 2022-06-15 13:39:46

The [CCXT library](https://github.com/ccxt/ccxt) is a powerful Python, JS, and PHP library that can be used to pull information from all of the major crypto asset exchanges. 

It has made my life a lot easier since it abstracts out the interface of each exchange. Instead of having to learn each API for each exchange or importing a separate library for each one, I just need to use one module - CCXT.

It also eliminates having to worry about keeping up with all the updates of a particular API. CCXT takes care of all the updates for me, which good because some of these APIs have missing documentation. For example, Coinbase has outdated json responses.

Getting started is easy. Just instantiate an exchange with your api key and api secret, which should have the basic permissions assigned to it. I got mine off of [Binance.com](https://accounts.binance.com/en/register?ref=72627473).

First, navigate to the site, login and click 'API Management' under your profile:


![Binance - Api Management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/non07yg65y0rocfmheyi.png)

Click on create API:

![Binance - Create API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gikw1zns5du5czy0k6z5.png)

Give it a name:

![Binance - Naming your API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evhj8po9oxlugl4ix153.png)

And then you will be met with this screen:

![Binance - Create an API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u53r4pjcvadhnjc5d392.png)

1) Be sure to copy down your API key AND API secret. On Binance and a lot of other exchanges, it might only give you a copy button for the API key. And the QR Code may only contain the API key, but, generally speaking, you will need both when programming or doing anything with the API really.

On top of that, you might not be able to copy the API secret once you save the new API, so be sure to copy it and save it for later.

2) Optionally, you can 'edit restrictions' and limit what can be done with your API keys. I would start with reading permissions when you are first programming. You do not want to give your program the ability to make orders until you are sure you can lock it down.

3) You can also limit access to certain IP addresses, which is really handy if you are accessing the exchange from a static IP. If you are accessing it from home, chances are you will not have a static IP though, so it is better not to check this.

After that you are all set. To initialize the exchange in CCXT, you simply have to use the constructor for it:

```
from ccxt import binance

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

I like to import only the exchange I'm using to keep my code clean and less confusing.

Once you have instantiated the exchange, you can reuse it many times to make calls to the private or public API. For example, you can retrieve historical pricing data in order to run backtests:

```
results = binance.fetchOHLCV("BTC/USDT", "1m", 1655298673, 1)

print(results)
[
     [
         165529867000, // UTC timestamp in milliseconds, integer
         24235.4,        // (O)pen price, float
         24240.6,        // (H)ighest price, float
         24230.0,        // (L)owest price, float
         24230.7,        // (C)losing price, float
         37.72941911    // (V)olume (in terms of the base currency), float
     ],
     ...
 ]
```

There are numerous functions you can call on an exchange like this. For a full list, be sure to check the [CCXT documentation](https://docs.ccxt.com/en/latest/manual.html).

## Conclusion

If you are anything like me, you use more than a few different exchanges when you trade crypto assets. In this world, of ever-changing policy and sudden crashes, you never know when you will have to jump off an exchange and move to another, so it is good to have more than one account. 

That's where learning a library like CCXT once comes in handy. You don't have to learn multiple libraries or have multiple dependencies for your project. It can also be used in javascript or PHP. Give it a try and let me know if it works for you.

_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._