A client library for accessing Kalshi Trade API
Low maintenance activity. Last release 16 months ago. Weaker on recent releases and release cadence.
pip install kalshi-pyREADME as published on PyPI with version 2.0.6.6.
kalshi-py
A client library for accessing Kalshi Trade API
📚 Documentation - Complete API reference, examples, and guides
Installation
pip install kalshi-py
Usage
Basic Usage (Public Endpoints)
For public endpoints that don't require authentication:
from kalshi_py import Client
client = Client(base_url="https://api.elections.kalshi.com/trade-api/v2")
from kalshi_py.api.market import get_markets
response = get_markets.sync(client=client, limit=10)
print(f"Found {len(response.markets)} markets")
Authenticated Usage (Trading Endpoints)
For trading endpoints that require authentication, use the Kalshi-specific authenticated client:
from kalshi_py import create_client
# Using environment variables ("KALSHI_API_KEY_ID" and "KALSHI_PY_PRIVATE_KEY_PEM")
client = create_client()
# Or with file path
client = create_client(
access_key_id="your-access-key-id",
private_key_path="/path/to/your/private-key.pem"
)
# Or with PEM data directly
client = create_client(
access_key_id="your-access-key-id",
private_key_data="-----BEGIN PRIVATE KEY-----\n..."
)
from kalshi_py.api.portfolio import get_balance
balance = get_balance.sync(client=client)
print(f"Account balance: {balance.balance}")
Direct Client Usage
You can also create the authenticated client directly if you prefer:
from kalshi_py import KalshiAuthenticatedClient
# Direct usage requires explicit credentials
client = KalshiAuthenticatedClient(
access_key_id="your-access-key-id",
private_key_pem="-----BEGIN PRIVATE KEY-----\n..."
)
from kalshi_py.api.portfolio import get_balance
balance = get_balance.sync(client=client)
print(f"Account balance: {balance.balance}")
Environment Variables
You can set the following environment variables to avoid passing credentials explicitly:
KALSHI_API_KEY_ID: Your Kalshi access key IDKALSHI_PY_PRIVATE_KEY_PEM: Your RSA private key in PEM format
API Endpoints
The client supports both synchronous and asynchronous operations:
from kalshi_py import create_client
from kalshi_py.api.market import get_markets
from kalshi_py.api.portfolio import get_balance
# Synchronous usage
client = create_client()
markets = get_markets.sync(client=client, limit=5)
balance = get_balance.sync(client=client)
# Asynchronous usage
import asyncio
async def main():
client = create_client()
markets = await get_markets.asyncio(client=client, limit=5)
balance = await get_balance.asyncio(client=client)
asyncio.run(main())