> For the complete documentation index, see [llms.txt](https://docs.afx.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.afx.xyz/api-reference/sdk.md).

# Python SDK

The official Python SDK is maintained in [`afx-dex/afx-python-sdk`](https://github.com/afx-dex/afx-python-sdk).

Use this project instead of downloading standalone SDK files from the docs. The SDK includes the generated protobuf module under `afx.protos`, so users do not need to download `dex.proto`, generate `dex_pb2.py`, or keep a local `dex_client.py`.

## Install

```bash
git clone https://github.com/afx-dex/afx-python-sdk.git
cd afx-python-sdk
python3 -m pip install -e .
```

## Configure Wallets

Private keys are loaded only from environment variables:

```bash
export AFX_MASTER_PRIVATE_KEY="0xYOUR_MASTER_PRIVATE_KEY"
export AFX_AGENT_PRIVATE_KEY="0xYOUR_AGENT_PRIVATE_KEY"
```

## Client Layout

```python
from afx import AfxClient

client = AfxClient.from_env(testnet=True)

products = client.info.get_products()
order = client.exchange.place_order(
    symbol_code=1,
    px="50000",
    qty="0.001",
    side="BUY",
    ord_type="LIMIT",
    tif="GTC",
)
```

Trading actions are under `client.exchange`, read-only queries are under `client.info`, and WebSocket helpers are under `client.websocket`.

For order requests, pass active order types such as `"LIMIT"` or `"MARKET"`. The proto default `"NONE"` and display-only `OrdType` values such as `"MARKET_LIQ_SELLOFF"`, `"LIMIT_LIQ_SELLOFF"`, `"ADL"`, and `"LIQUIDATION"` may appear in query or stream data, but they must not be used in order requests.

## Examples

Every public SDK feature has an example under the SDK repository's `examples/` directory:

```bash
python3 examples/info/get_products.py
python3 examples/exchange/place_order.py
python3 examples/exchange/replace_order.py
python3 examples/exchange/place_bracket_order.py
python3 examples/websocket/subscribe_ticker.py
```

### Example Index

**Info queries**

* [get\_products.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_products.py)
* [get\_wallet.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_wallet.py)
* [get\_orders.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_orders.py)
* [get\_positions.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_positions.py)
* [get\_kline.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_kline.py)
* [get\_agents.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_agents.py)
* [get\_active\_agent.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_active_agent.py)
* [get\_funding\_rate\_current.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/info/get_funding_rate_current.py)

**Exchange actions**

* [faucet\_claim.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/faucet_claim.py)
* [approve\_agent.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/approve_agent.py)
* [revoke\_agent.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/revoke_agent.py)
* [place\_order.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/place_order.py)
* [place\_tp\_sl\_orders.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/place_tp_sl_orders.py)
* [replace\_order.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/replace_order.py)
* [place\_bracket\_order.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/place_bracket_order.py)
* [cancel\_order.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/cancel_order.py)
* [cancel\_all.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/cancel_all.py)
* [set\_leverage.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/set_leverage.py)
* [set\_margin\_mode.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/set_margin_mode.py)
* [assign\_pos\_margin.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/assign_pos_margin.py)
* [withdraw.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/withdraw.py)
* [vault\_deposit.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/vault_deposit.py)
* [vault\_withdraw.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/vault_withdraw.py)
* [bind\_referral.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/exchange/bind_referral.py)

**WebSocket and advanced usage**

* [subscribe.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/websocket/subscribe.py)
* [subscribe\_order\_book.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/websocket/subscribe_order_book.py)
* [subscribe\_ticker.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/websocket/subscribe_ticker.py)
* [subscribe\_account\_state.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/websocket/subscribe_account_state.py)
* [multiple\_wallets.py](https://github.com/afx-dex/afx-python-sdk/blob/main/examples/advanced/multiple_wallets.py)

## Verify

```bash
python3 -m unittest discover -s tests -v
```

The SDK tests cover signing helpers, protobuf serialization, client behavior, and example imports.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.afx.xyz/api-reference/sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
