Share articles to

Academy Advance Guides Article
cryptocurrency Trading

How to Use API Trading on OKX


If you want to trade you will be needing an exchange platform for screening stocks or assets like bitcoins. The user finds an asset he suits  to trade, takes a look at the price, chart and the order book, and places an order in the market for a limit or stop order using the interface.

This system is an evolution over the stock exchange trading systems of the past but is still not the most effective process, particularly for high-frequency trading. The wants of advancement today are met by automated or algorithmic trading systems, which allow traders to program their strategies and set them to execute and adapt to market conditions through human input.

Application programming interface or API is the center of the trading systems as they are automated and is an important group of code that lets the software platforms share data and communicate effectively in real-time.

The main users of the API trading are the algorithm trading firms and professional traders, but the retail traders can also use the API connections for their portfolio-management.

In the article let us go through an introduction to API trading on OKX and on the new OKX API v5 upgrade.

Let’s get to the basics first:

Basics of API trading

There are 3 important aspects in API trading:

  • API keys
  • API documentation
  • Connection interfaces

API keys are unique identifiers that authenticate traders and developers who use the exchange’s API. In other words API keys consisting of a public key and a secret key work like a username and password.

The documentation has technical information on how to use and integrate API acting as a reference book for the developers and traders.

There are two types of APIs: REST and WebSocket.

REST is for representational state transfer and is the most common web service that supports a number of different formats HTTP like JSON and XML.

WebSocket is a new HTML5 protocol that enables efficient data transmission between the client and server. This is particularly useful for developers intending to program and connect scripts, bots or other software with an exchange .


OKX has launched its new API v5 to give the users seamless trading which is available for all Unified Account users. It is a significant upgrade from v3, v5 has upgrades in the following areas:

  1. Enhanced efficiency in capital allocation
  2. Order operation in both REST and WebSocket API
  3. Simultaneous support multiple trading products
  4. A unified JSON format for error reporting.

API trading using OKX

There are three categories of API trading: account, trading and market trends. In the account and trading modules, users can place orders, enquire about the order status and account information. Users have access to historical price data of trading pairs via the publicity available market data API.

There is support for both REST and WebSockets from OKX in v3 and traders can use the API for 5 trading products, including soptm margin, futures, perpetual swaps and options.

The v3 can be accessed by the retail and institutional traders, in a bid to continue improving user experience.

Access to trading products

In the v3 of the API it can only access the interface of one trading product. In Comparison v5 allows traders and users to access interfaces of up to 5 trading products.

Account funds usage

In the v3 there is a corresponding account for each business line. This implies that users need to transfer their funds from one trading product to another. The transfer of funds is time consuming and reduces efficiency for users. The fund transfers between trading product accounts is limited to once per second.

Users can use it to their advantage with greater capital efficiency when using v5 of the API. Like OKX’s unified account, traders using API v5 can utilize the funds in an account. To add to it, the profits and losses of many positions in v5 are also consolidated according to the user’s account mode thus improving capital efficiency.

Order operations

The order operations include placing new orders, order withdrawals. As the REST API in v3 supports order operations, the new v5 allows both REST and WebSocket APIs to support order operations.

Upgrade based on Scenario

Scenario-based upgrades refer to the upgrades in a specific function or format. This has order mode upgrades and format updates of time function and REST response function.

Order mode

The API v3 supports only opening and closing positions and if the users hold both long and short positions in the same contract., these positions are viewed separately and cannot offset against each other.

In combination with the Unified Accounts, API v5 has a new long/short mode for users. This implicates that in single-currency margin mode, many long and short positions in the same underlying currency can be offset against each other while multi-currency mode allows for positions to be hedged across assets and traders can view the net positions.

WebSocket subscription function

This subscription function lets users fetch data. When seen along with API v3, v5 enables users to subscribe to multiple trading pairs simultaneously.

Private liquidation order

In v3 users cannot view the status of private liquidation. They need to do so by connecting endpoints from the REST API.

The order channel of API v5 supports private liquidation orders. Traders can view the directly and they can decide to increase or decrease their positions by navigating the “Category” field

Number of opened contracts

When the traders in v3 want to view the number of opened contracts, they have to send requests to the WebSocket account channel to view it.

Viewing the number of opened contracts is straightforward in v5, as traders can have such data through the “Get maximum number of contracts” interface in the REST API.

Time format

The API v3 embraces the ISO8601 time format, referring to the UTC time zones. But, this format may not be compatible across all programming languages. This needs some more functionality for time zone conversion.

V5 takes on a more universal time format: the Unix Epoch, which does not take time zones into account API v5 also has relevant resources in the library, allowing easy time format conversion for developers.

Launch of v5

On March 31, v5 of the API will be available to all Unified account users. As v3 will be discontinued shortly after the release of v5, the users are encouraged to upgrade their API to the v5 for better and new functionalities and a more seamless trading experience.

Disclaimer: This material should not be taken as the basis for making investment decisions, nor be construed as a recommendation to engage in investment transactions. Trading digital assets involve significant risk and can result in the loss of your invested capital. You should ensure that you fully understand the risk involved and take into consideration your level of experience, investment objectives and seek independent financial advice if necessary.