pyokx
Installation
pip install pyokx
Introduction
pyokx is a completely unofficial python API wrapper developed to interact with the OKX V5 API. It's unique insofar as that it has been developed by scraping the API documentation to dynamically generate python code to provide an intuitive pythonic interface for exact same API. This idea essentially is to avoid the need to create separate documentation for this wrapper and instead you can simply refer to the official OKX docs for API usage.
It's used by creating a base client instance to make and receive requests and passing that client to each API class (APIComponent), which has been dynamically generated from the API docs.
Let's start with an example.
Let's say we want to check all current positions.
Check out the docs for get balance here: https://www.okx.com/docs-v5/en/#rest-api-account-get-positions
We can see the endpoint belongs to the Account API and needs to be called with 3 parameters:
In pyokx, you can see the method signature for the Account class is exactly the same:
def get_positions(
self,
instType: str = None,
instId: str = None,
posId: str = None,
use_proxy: bool = False,
) -> APIReturn:
So this can be easily implemented like so:
- Create
.envfile that contains your API information:
KEY = replace_your_key_here
SECRET = replace_your_secret_here
PASSPHRASE = replace_your_passphrase_here
- Read API information from
.envand create the base client:
import os
# python-dotenv should have been installed from the dependencies
from dotenv import load_dotenv
from pyokx import OKXClient, Account
# read information from .env file
load_dotenv()
# create the base client:
client = OKXClient(
key = os.getenv('KEY'),
secret = os.getenv('SECRET'),
passphrase = os.getenv('PASSPHRASE'),
)
...
- Now you can create Account object and call endpoints
...
# create a component for the Account API by passing the client dependency
account = Account(client)
# get positions
api_response = account.get_positions()
# you can convert to a pandas dataframe to make it more readable
df_response = api_response.to_df()
print(df_response)
# or you can get the raw response
raw_response = api_response.response
print(raw_response)
That simple.
Key features
APIReturn
This is essentially a wrapper around the response that is returned from every endpoint. This is to provide some useful helper methods such as dataframe conversion.
Proxies
As is common with a lot of exchange APIs, for calls that require authentication (usually account/trade-related), it is strongly encouraged to limit your API key to a select list IP addresses to protect your account. On some systems this may require routing traffic through a forward proxy. pyokx supports this pattern by allowing you to pass the necessary proxies to the base client and you can trigger this behaviour by setting the use_proxy parameter to True.
For example:
proxies = {
"http": "http://your-proxy-server.com",
"https": "https://your-proxy-server.com",
}
client = OKXClient(
key="key",
secret="secret",
passphrase="passphrase",
proxies=proxies
)
# trigger the use of the proxy server with use_proxy
account = Account(client)
api_response = account.get_positions(use_proxy=True)
Development progress
It's still a very early version - so issues, feature requests and bugs are very welcome!
- REST API implementation.
- Fix pythonic naming conventions when API names contain special characters
- Enhance documentation
- Websocket API implementation.
Disclaimer
NB. pyokx is totally unofficial and is in no way affiliated with OKEX Crypto exchange and simply exists as a helpful wrapper to interact with the V5 API.
Metadata
Release files for pyokx 0.7.0
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 |
|---|---|---|---|---|
| pyokx-0.7.0.tar.gz | 41.3 kB | Without provenance | Details for pyokx-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyokx-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for pyokx-0.7.0-py3-none-any.whl |
Total release size: 91.4 kB
Release files / pyokx-0.7.0.tar.gz
| Download URL | pyokx-0.7.0.tar.gz |
|---|---|
| Size | 41.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2b91c1684fdcfcf37b55885f303c468c953f12b3cdf3d11f52a774309c4da9b
|
|
BLAKE2b-256 checksum How to use checksums |
482dbc6d7237217dd13ffed3ce881b3b09d5f43174b990ee51b817f1816828ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.11.1 Linux/5.15.90.1-microsoft-standard-WSL2
|
Release files / pyokx-0.7.0-py3-none-any.whl
| Download URL | pyokx-0.7.0-py3-none-any.whl |
|---|---|
| Size | 50.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
abfe31679e0d1592648c55cfdd877faaeba211b17788e97d666387f25c4e800f
|
|
BLAKE2b-256 checksum How to use checksums |
37e2e11648af8c583c1dd541b5c6442fe06c6b0094ae79d6a0d9806cb9aae2e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.11.1 Linux/5.15.90.1-microsoft-standard-WSL2
|
Security for pyokx 0.7.0
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.