Skip to main content

Add your description here

Project description

dtr-kalshi

A lightweight Python client for the Kalshi Trade API.

Quickstart

Installation

pip install dtr-kalshi

Authentication

Kalshi uses RSA key-pair authentication. You'll need:

  1. API Key ID — found in your Kalshi account settings.
  2. RSA private key file — a .key file in PEM format, downloaded when you create an API key.

Export both as environment variables so you don't hardcode credentials:

export KALSHI_API_KEY_ID="your-api-key-id"
export KALSHI_API_KEY_SECRET="/path/to/your/private_key.key"

Alternatively, pass them directly to the client constructor.

Sync usage

KalshiSyncClient is a straightforward blocking client suitable for scripts and simple applications.

from dtr_kalshi import KalshiSyncClient

# credentials are read from environment variables by default
client = KalshiSyncClient()

# use use_sandbox=True to hit the demo environment instead
# client = KalshiSyncClient(use_sandbox=True)

# GET /portfolio/balance
balance = client.get('/portfolio/balance')
print(balance)

# POST /portfolio/orders
order = client.post('/portfolio/orders', data={
    'ticker': 'INXD-23DEC31-B4000',
    'side': 'yes',
    'count': 10,
    'type': 'limit',
    'yes_price': 55,
})
print(order)

# DELETE /portfolio/orders/{order_id}
client.delete(f"/portfolio/orders/{order['order']['order_id']}")

Async usage

KalshiAsyncClient uses a shared httpx.AsyncClient for connection pooling. Use it as an async context manager so the underlying connection is closed cleanly on exit.

import asyncio
from dtr_kalshi import KalshiAsyncClient

async def main():
    async with KalshiAsyncClient() as client:
        balance = await client.get('/portfolio/balance')
        print(balance)

        # fan out multiple requests concurrently
        btc, eth = await asyncio.gather(
            client.get('/markets/KXBTC-25DEC31'),
            client.get('/markets/KXETH-25DEC31'),
        )
        print(btc, eth)

asyncio.run(main())

WebSocket usage

KalshiWebSocketConnection streams real-time market data. Use it as an async context manager.

Send commands with .send_message(cmd, params), which maps directly to the Kalshi WebSocket protocol.

Callback-based (recommended for multiple channels)

Register handlers with .on(channel, handler), then call .listen() to process messages indefinitely.

import asyncio
from dtr_kalshi import KalshiWebSocketConnection

async def main():
    async with KalshiWebSocketConnection() as ws:
        ws.on("ticker", lambda msg: print("ticker:", msg))
        ws.on("trade", lambda msg: print("trade:", msg))

        await ws.send_message("subscribe", {
            "channels": ["ticker", "trade"],
            "market_ticker": ["INXD-23DEC31-B4000"],
        })
        await ws.listen()

asyncio.run(main())

Iterator-based (for custom message handling)

Iterate directly over the connection with async for to handle all messages yourself.

import asyncio
from dtr_kalshi import KalshiWebSocketConnection

async def main():
    async with KalshiWebSocketConnection() as ws:
        await ws.send_message("subscribe", {
            "channels": ["orderbook_delta"],
            "market_ticker": ["INXD-23DEC31-B4000"],
        })
        async for msg in ws:
            print(msg)

asyncio.run(main())

Concurrent REST and WebSocket

Wrap .listen() in a task to run alongside REST calls.

import asyncio
from dtr_kalshi import KalshiAsyncClient, KalshiWebSocketConnection

async def main():
    async with KalshiAsyncClient() as client, KalshiWebSocketConnection() as ws:
        await ws.send_message("subscribe", {
            "channels": ["ticker"],
            "market_ticker": ["INXD-23DEC31-B4000"],
        })
        listen_task = asyncio.create_task(ws.listen())

        balance = await client.get('/portfolio/balance')
        print(balance)

        listen_task.cancel()

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dtr_kalshi-0.2.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dtr_kalshi-0.2.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dtr_kalshi-0.2.3.tar.gz.

File metadata

  • Download URL: dtr_kalshi-0.2.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dtr_kalshi-0.2.3.tar.gz
Algorithm Hash digest
SHA256 50d7bc2a87ac691dc23f15ab0e7734bbf65cb37c512597f9c199222a27b472b4
MD5 8c93059c75bdeabb0e996538b6f212f5
BLAKE2b-256 7989597bd3c06e1f2e2a6c4a8f5f4920f825a747cb02f4174b1f5c372d14850b

See more details on using hashes here.

File details

Details for the file dtr_kalshi-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: dtr_kalshi-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dtr_kalshi-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 77c75f3f343ad67a5b8110a3043189e6f334dd1600aef3d29ba2d3e9ca1c4159
MD5 48c98eb0cf6c0ab1c2759c930c68de67
BLAKE2b-256 5d5a6c7244f37c24bcda77b739b43b2aa032a447e004a81edcb99458291ea3c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page