Skip to main content

Overview

This is an unofficial Python wrapper for the OKX exchange v5 API

If you came here looking to purchase cryptocurrencies from the OKX exchange, please go here.

Source code

https://github.com/okxapi/python-okx

API trading tutorials

Make sure you update often and check the Changelog for new features and bug fixes.

Features

  • Implementation of all Rest API endpoints.
  • Private and Public Websocket implementation
  • Testnet support
  • Websocket handling with reconnection and multiplexed connections

Quick start

Prerequisites

python version:>=3.7

Step 1: Register an account on OKX and apply for an API key

Step 2: Install python-okx

pip install python-okx

API Credentials

Option 1: Hardcoded credentials

from okx import Account

account = Account.AccountAPI(
    api_key="your-api-key-here",
    api_secret_key="your-api-secret-here",
    passphrase="your-passphrase-here",
    flag="1",  # 0 = live trading, 1 = demo trading
    debug=False
)

Option 2: Using .env file (recommended)

Create a .env file in your project root:

OKX_API_KEY=your-api-key-here
OKX_API_SECRET=your-api-secret-here
OKX_PASSPHRASE=your-passphrase-here
OKX_FLAG=1

Then load it in your code:

import os
from dotenv import load_dotenv
from okx import Account

load_dotenv()

account = Account.AccountAPI(
    api_key=os.getenv('OKX_API_KEY'),
    api_secret_key=os.getenv('OKX_API_SECRET'),
    passphrase=os.getenv('OKX_PASSPHRASE'),
    flag=os.getenv('OKX_FLAG', '1'),
    debug=False
)

Development Setup

For contributors or local development:

# Clone the repository
git clone https://github.com/okxapi/python-okx.git
cd python-okx

# Install dependencies
pip install -r dev_requirements.txt
pip install -e .

# Run tests
pytest test/unit/ -v

Step 3: Run examples

  • Fill in API credentials in the corresponding examples
api_key = ""
secret_key = ""
passphrase = ""

Note

  • To learn more about OKX API, visit official OKX API documentation

  • If you face any questions when using WebSocketAPI,you can consult the following links

    • asyncio、websockets document/github:

      https://docs.python.org/3/library/asyncio-dev.html
      https://websockets.readthedocs.io/en/stable/intro.html
      https://github.com/aaugustin/websockets
      
    • About code=1006:

      https://github.com/Rapptz/discord.py/issues/1996
      https://github.com/aaugustin/websockets/issues/587
      

Metadata

Release files for python-okx 0.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-okx 0.4.4
File Size Uploaded
python_okx-0.4.4.tar.gz 41.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-okx 0.4.4
File Interpreter ABI Platform
python_okx-0.4.4-py3-none-any.whl Python 3 none any Details

Total release size: 82.0 kB

Release files / python_okx-0.4.4.tar.gz

Download URL python_okx-0.4.4.tar.gz
Size 41.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9c01cfdfb01a604384b0d167b1443d3e3fd6437c11647b741912fd4ed5fab4de
BLAKE2b-256 checksum
How to use checksums
bfb35d6bdd3ed94188947695555846972c19118246264971da5f926819be796d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / python_okx-0.4.4-py3-none-any.whl

Download URL python_okx-0.4.4-py3-none-any.whl
Size 40.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0478059cc326adad3e413da32dd5785b510f4fe026b26ea10b95831e1a56346a
BLAKE2b-256 checksum
How to use checksums
30480fe26772f2eadbdca1148e27786a0c6cdf5307794083f47bc81239490c8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.4.4 This release

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

1 release file

0.4.0

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.9

1 release file

0.2.8

1 release file

0.2.7

1 release file

0.2.6

2 release files

0.2.5

1 release file

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page