Prerequisites
- Python 3.13
- VSCode (with Python extension)
Two Methods: Manual Signing vs Python SDK
Method 1: Manual Signing
1. Set Up Development Environment
Install required library:
pip3 install requestsVerify installation:
python3 -c "import requests; print(requests.__version__)"
2. Get OKX API Keys
- Log in to OKX โ Profile โ API โ Create API Key
- Set permissions: "Read Account Info" for balance checking
- Note your API Key, Secret Key, and Passphrase
3. Create Workspace
- Make a new folder (e.g., on Desktop)
Create Python file (
okx_balance.py) with this template:import requests import time import hashlib import hmac import base64 # Replace with your API credentials API_KEY = "your_api_key" SECRET_KEY = "your_secret_key" PASSPHRASE = "your_passphrase" def get_balance(): url = "https://www.okx.com/join/BLOCKSTARapi/v5/account/balance" timestamp = str(time.time()) message = timestamp + "GET" + "/api/v5/account/balance" signature = base64.b64encode( hmac.new(SECRET_KEY.encode(), message.encode(), hashlib.sha256).digest() ).decode() headers = { "OK-ACCESS-KEY": API_KEY, "OK-ACCESS-SIGN": signature, "OK-ACCESS-TIMESTAMP": timestamp, "OK-ACCESS-PASSPHRASE": PASSPHRASE } response = requests.get(url, headers=headers) return response.json() if __name__ == "__main__": balance = get_balance() print("Current balance:", balance)
๐ Get started with OKX API today
Common Issues (Manual Signing)
1. API Key Errors (50101)
- Verify permissions and character cases
2. Connection Failures (50113)
- Test connectivity using OKX public endpoints first
3. Invalid Signature (50113)
- Sync local time with OKX server time
Method 2: Python SDK
1. Install OKX SDK
pip3 install okx2. Sample Code
from okx.app.account import AccountSPOT
API_KEY = "your_api_key"
SECRET_KEY = "your_secret_key"
PASSPHRASE = "your_passphrase"
account = AccountSPOT(API_KEY, SECRET_KEY, PASSPHRASE)
balance = account.get_balances()
print("Balance:", balance)Common Issues (SDK)
1. Rust Compilation Error
Install Rust first:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh2. Module Import Errors
Use correct import path:
from okx.app.account import AccountSPOT๐ Explore advanced API features
FAQ
Q: Which method is better for beginners?
A: Python SDK handles signatures automatically, making it easier for beginners.
Q: How often should I rotate my API keys?
A: For security, rotate keys every 3-6 months.
Q: Can I test the API without real funds?
A: Yes, use OKX's demo trading environment first.
Q: What's the rate limit for balance queries?
A: 20 requests per 2 seconds per account.
Key Takeaways
- Always secure your API keys
- Python SDK simplifies development
- Regular error handling improves reliability