↓ Skip to main content
All methods

stakepd.kalshi_data.KalshiClient

Class
Signature
kd.KalshiClient(api_key_id=None, private_key=None, private_key_path=None, *,
                environment="prod", base_url=None, timeout=30.0, max_retries=3, session=None)

Create a client for the Kalshi Trade API v2.

Without credentials, the client can fetch every market-data endpoint. With credentials, it can also fetch portfolio data. Every request is signed for you, pages are followed automatically, and rate-limit and server errors are retried with exponential backoff.

Parameters

api_key_id:str, optional
Your Kalshi API key ID.
private_key:str or bytes, optional
The private key’s PEM text. Use this or private_key_path, not both.
private_key_path:str or os.PathLike, optional
Path to the private key .pem file. ~ is expanded.
environment:{'prod', 'demo'}, default 'prod'
Which Kalshi exchange to use. Demo keys only work on demo, and production keys only work on production.
base_url:str, optional
A custom API base URL. Overrides environment.
timeout:float, default 30.0
Seconds to wait for each HTTP response.
max_retries:int, default 3
How many times to retry a request that gets a rate-limit (429) or server (5xx) error, with exponential backoff.
session:requests.Session, optional
Use your own HTTP session, for example to add a proxy or for testing. By default, the client builds one with retries.

Returns

KalshiClient
A new client.
Attributes and other methods
NameTypeDescription
client.is_authenticatedboolTrue if the client has credentials.
client.base_urlstrThe API URL in use. The defaults are in kd.BASE_URLS ({"prod": ..., "demo": ...}).
client.close()methodCloses the HTTP session. You can also use the client as a context manager (see below).

Raises

KalshiAuthError
If only one of api_key_id and a private key is given, or the key file can’t be read or parsed.
ValueError
If environment is not "prod" or "demo".

See also

KalshiClient.from_env
Create an authenticated client from environment variables.
load_private_key
Load an RSA or Ed25519 PEM private key.
KalshiAuthError
Raised when credentials are missing, incomplete or unreadable.

Examples
#

Public market data:

from stakepd import kalshi_data as kd

client = kd.KalshiClient()
print(client)
Output:
KalshiClient(base_url='https://external-api.kalshi.com/trade-api/v2', authenticated=False)

With credentials:

client = kd.KalshiClient(
    api_key_id="a1b2c3d4-...",
    private_key_path="~/.kalshi/kalshi_private_key.pem",
)

As a context manager (closes the session when done):

with kd.KalshiClient() as client:
    markets = client.get_markets(status="open", max_results=100)
Keep the .pem file private: never commit it to git or paste it into a notebook you share.

This method is also covered, alongside the rest of the module, in the overview guide.