# Python SDK

> The Python SDK for the STX exchange.

Source: https://docs.stxapp.io/sdks/python/

`stx-python` is the Python SDK for the STX exchange. Read markets, place and cancel orders, follow your account, and stream the [WebSocket channels](/websockets/), with every request signed by your API key and every response a typed model. Response fields are described in the [API reference](/api/rest/).

```bash
pip install stx-python
```

```python
from stx import STX

# Reads your API key and exchange from STX_* environment variables or ~/.stx/credentials.
with STX() as client:
    me = client.me()
    page = client.markets(status="open", limit=5)
    print(me.user_id, [m.symbol for m in page])
```

[Authentication](/sdks/python/authentication/) shows how to give the client your API key, and [Environments](/environments/) lists the exchanges and where to get a key.

There are three clients:

- **`STX`**: blocking, one method per API call.
- **`AsyncSTX`**: the same methods for `asyncio`. See [Async](/sdks/python/async/).
- **`STXWebSocket`**: the channels you join, over one signed socket that keeps itself alive and reconnects. See [WebSockets](/sdks/python/websockets/).

## Prices and quantities are strings

Amounts come back as decimal strings, exactly as the API sends them: dollars with at least four decimals (`order.price == "0.5600"`) and contract counts with at least two (`"2.00"`). Orders take strings too: `place_order(market_id, "buy", "limit", price="0.56", quantity="2")`. Passing a float raises `TypeError` before anything is sent. Use `decimal.Decimal` for arithmetic:

```python
from decimal import Decimal

from stx import STX

with STX() as client:
    for market in client.markets(status="open", limit=3):
        spread = None
        if market.bids and market.offers:
            spread = Decimal(market.offers[0].price) - Decimal(market.bids[0].price)
        print(market.symbol, "pays", market.max_price, "spread", spread)
```

Prices are the same on every channel: the SDK converts the channels that send cents on the wire to dollar strings. Values that are not money stay numbers: percentages, counts, loyalty points, fee factors and timestamps.

## Where to go next

  - [Installation](/sdks/python/installation/): Install and import.
  - [Quickstart](/sdks/python/quickstart/): Add a key and make your first calls.
  - [Authentication](/sdks/python/authentication/): Pass your API key to the client.
  - [Environments](/sdks/python/environments/): Pick the exchange to connect to.
  - [Markets](/sdks/python/markets/): Markets, events and pagination.
  - [Trading](/sdks/python/trading/): Place and cancel orders.
  - [Portfolio](/sdks/python/portfolio/): Balance, positions, fills and history.
  - [WebSockets](/sdks/python/websockets/): Stream market data and your account.
  - [Async](/sdks/python/async/): Concurrent requests and streams with asyncio.
  - [Errors & retries](/sdks/python/errors-and-retries/): Typed exceptions, timeouts and what the client retries.
  - [API reference](/sdks/python/reference/): Every method and what it returns.
  - [Example scripts](https://github.com/stxapp/stx-python-demo): List markets, place and cancel an order, stream live data.

Released under the MIT license.
