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())
Authentication Details
The Kalshi API uses RSA-PSS signature authentication. Each request is signed with:
- Timestamp: Current time in milliseconds
- Method: HTTP method (GET, POST, etc.)
- Path: API endpoint path
- Signature: RSA-PSS signature of
timestamp + method + path
The client automatically handles:
- Loading your private key from file
- Generating timestamps
- Creating signatures for each request
- Adding required headers (
KALSHI-ACCESS-KEY,KALSHI-ACCESS-SIGNATURE,KALSHI-ACCESS-TIMESTAMP)
Legacy Bearer Token Authentication
If you need to use the legacy bearer token authentication (not recommended for trading):
from kalshi_py import AuthenticatedClient
client = AuthenticatedClient(
base_url="https://api.elections.kalshi.com/trade-api/v2",
token="your-bearer-token"
)
Advanced Customizations
Custom SSL Configuration
client = create_client(
verify_ssl="/path/to/certificate_bundle.pem"
)
# Or disable SSL verification (not recommended for production)
client = create_client(verify_ssl=False)
Custom Headers and Timeouts
import httpx
client = create_client(
timeout=httpx.Timeout(30.0),
headers={"User-Agent": "MyApp/1.0"}
)
Request Logging
def log_request(request):
print(f"Request: {request.method} {request.url}")
def log_response(response):
print(f"Response: {response.status_code}")
client = create_client(
httpx_args={
"event_hooks": {
"request": [log_request],
"response": [log_response]
}
}
)
API Structure
Every API endpoint becomes a Python module with four functions:
sync: Blocking request that returns parsed datasync_detailed: Blocking request that returns full response detailsasyncio: Async request that returns parsed dataasyncio_detailed: Async request that returns full response details
All path/query parameters and request bodies become function arguments.
Building / Publishing this package
This project uses uv to manage dependencies and packaging. Here are the basics:
- Update the metadata in
pyproject.toml(e.g. authors, version). - If you're using a private repository: https://docs.astral.sh/uv/guides/integration/alternative-indexes/
- Build a distribution with
uv build, buildssdistandwheelby default. - Publish the client with
uv publish, see documentation for publishing to private indexes.
If you want to install this client into another project without publishing it (e.g. for development) then:
- If that project is using uv, you can simply do
uv add <path-to-this-client>from that project - If that project is not using uv:
- Build a wheel with
uv build --wheel. - Install that wheel from the other project
pip install <path-to-wheel>.
- Build a wheel with
Metadata
Release files for kalshi-py 2.0.6.6
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 |
|---|---|---|---|---|
| kalshi_py-2.0.6.6.tar.gz | 75.9 kB | Without provenance | Details for kalshi_py-2.0.6.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kalshi_py-2.0.6.6-py3-none-any.whl | Python 3 | none | any | Details for kalshi_py-2.0.6.6-py3-none-any.whl |
Total release size: 305.3 kB
Release files / kalshi_py-2.0.6.6.tar.gz
| Download URL | kalshi_py-2.0.6.6.tar.gz |
|---|---|
| Size | 75.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfa12d4b425f23be11e20e195143622e1936e32d7e47beffbbdfe17dd18f62eb
|
|
BLAKE2b-256 checksum How to use checksums |
4c57c494e2b371d02b409cd2b60d6623e40d7d142a3789875ab6c8bc8bf7c591
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|
Release files / kalshi_py-2.0.6.6-py3-none-any.whl
| Download URL | kalshi_py-2.0.6.6-py3-none-any.whl |
|---|---|
| Size | 229.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b326eb7b93342e3c5e791a5c474f61f3a94650119bb678dd1356f05f5d8ac07
|
|
BLAKE2b-256 checksum How to use checksums |
c72ea23baf5185c208dfc68c45d330a356218753f310e8bc750e041aeacf5329
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|
Security for kalshi-py 2.0.6.6
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.