Overview
This is an official 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/okx/python-okx
API trading tutorials
- Spot trading: https://www.okx.com/help/how-can-i-do-spot-trading-with-the-jupyter-notebook
- Derivative trading: https://www.okx.com/help/how-can-i-do-derivatives-trading-with-the-jupyter-notebook
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
- Register for an account: https://www.okx.com/account/register
- Apply for an API key: https://www.okx.com/account/users/myApi
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/okx/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 = ""
- RestAPI
- For spot trading: run example/get_started_en.ipynb
- For derivative trading: run example/trade_derivatives_en.ipynb
- Tweak the value of the parameter
flag(live trading: 0, demo trading: 1 ) to switch between live and demo trading environment
- WebSocketAPI
- Run test/WsPrivateTest.py for private websocket channels
- Run test/WsPublicTest.py for public websocket channels
- Use different URLs for different environment
- Live trading URLs: https://www.okx.com/docs-v5/en/#overview-production-trading-services
- Demo trading URLs: https://www.okx.com/docs-v5/en/#overview-demo-trading-services
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、websocketsdocument/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.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_okx-0.4.5.tar.gz | 40.8 kB | Without provenance | Details for python_okx-0.4.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_okx-0.4.5-py3-none-any.whl | Python 3 | none | any | Details for python_okx-0.4.5-py3-none-any.whl |
Total release size: 81.4 kB
Release files / python_okx-0.4.5.tar.gz
| Download URL | python_okx-0.4.5.tar.gz |
|---|---|
| Size | 40.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f61d4e41dd97525e9e98591e079ee327e2c3a9126544394ea8de31f03be31e02
|
|
BLAKE2b-256 checksum How to use checksums |
1243b0f7f3d745387c13a39ea380617a8b304d8ff22c69e13e0f5caa5991c116
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / python_okx-0.4.5-py3-none-any.whl
| Download URL | python_okx-0.4.5-py3-none-any.whl |
|---|---|
| Size | 40.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b596562718fecb8c35c5da7851003624c308170595c7bff251a05a4d201230e9
|
|
BLAKE2b-256 checksum How to use checksums |
881a7d32104ea579f5ec05e9eafcb6c715742875ef9b0627fcb72f1432d798de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for python-okx 0.4.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.