v3.2.0
Adds full SDK support for the Kalshi Perps (margin) API — a separate
perpetual-futures exchange — as standalone clients alongside the existing
prediction-API surface. Additive release: no changes to KalshiClient.
Added
PerpsClient/AsyncPerpsClient— standalone clients for the perps
exchange (external-api.kalshi.com/ demoexternal-api.demo.kalshi.co,
/trade-api/v2), with their ownPerpsConfigand a separateKALSHI_PERPS_*
credential namespace. They reuse the prediction-API RSA-PSS signer and HTTP
transport unchanged. The constructors andfrom_env()also accept
ws_base_url(set the WS endpoint independently from REST) andpassword
(passphrase for an encrypted key), and readKALSHI_PERPS_WS_BASE_URL/
KALSHI_PERPS_PRIVATE_KEY_PASSPHRASEfrom the environment; passingconfig=
together withdemo/base_url/ws_base_urlis rejected. Resource families:
exchange(status / enabled gate /
risk parameters),markets(list / get / orderbook / candlesticks),orders
(create / get / list / cancel / decrease / amend + FCM),order_groups,
portfolio(positions / fills / trades),margin(balance / risk /
notional risk limit / fee tiers),funding(rate estimate / historical /
history), andtransfers(intra-exchange-instance + margin subaccounts).
Margin order side isbid/ask; prices areDollarDecimal
(FixedPointDollars), countsFixedPointCount, andnumber/doubleratios
(leverage, funding rate, ROE, fee tiers) areMultiplierDecimal(exact
Decimal, string-serialized) — consistent across the REST, WS, and exchange
surfaces. Margin-account list responses tolerate a server-returnednull.PerpsWebSocket— the perps margin WebSocket
(external-api-margin-ws.kalshi.com,/trade-api/ws/v2/margin) with six typed
channels (subscribe_orderbook_delta,subscribe_ticker— carrying
funding_rate+next_funding_time_ms,subscribe_trade,subscribe_fill,
subscribe_user_orders,subscribe_order_group). Reuses the event-contract
WS connection / sequence-gap / backpressure machinery; perps WS timestamps are
Unix epoch milliseconds (*_msfields).KlearClient/AsyncKlearClient— the Self-Clearing-Member "Klear"
settlement API (api.klear.kalshi.com/ demodemo-api.kalshi.co,
/klear-api/v1) with a third auth model: cookie-session + MFA via
login(email=..., password=..., code=...). Resourcemargincovers reports,
active/historical obligations, settlement estimate, settlement + guaranty-fund
balances, settlement-balance history, and settlement-balance withdrawal.
Klear money fields are integer centicents; the single real-money write
(withdraw_settlement_balance) validates a positive amount at construction.
Credentials and the session cookie are never logged or shown inrepr().docs/perps.md(+ mkdocs nav), README "Perps (margin) trading" section, and
runnableexamples/perps_create_order.py/perps_stream_ticker.py/
perps_balance_risk.py.
Changed
- Prediction-API list endpoints
markets.candlesticks/bulk_candlesticks/
bulk_orderbooksnow validate a typed response envelope: a missing
spec-required array key raisesValidationError(surfacing spec drift instead
of silently returning[]), while a null array coerces to[](Kalshi's
empty-as-null convention — the priordata.get(...)extraction would
TypeErroron a null array). The perpsmarkets.list/markets.candlesticks
/funding.historical_rates/funding.historyresponses use the same
NullableListenvelopes, so null-handling is consistent across both surfaces.
The optionalorder_groups.liststays tolerant of a missing/null array.
Fixed
KalshiWebSocket._stop()now retrieves an already-finished receive-loop's
exception, so a session torn down after a permanent close no longer logs
asyncio's "Task exception was never retrieved" on garbage collection.
Internal
- Vendored the three perps specs (
specs/perps_openapi.yaml,
perps_asyncapi.yaml,perps_scm_openapi.yaml);scripts/sync_spec.pyand the
weekly spec-sync workflow now fetch/diff/checksum them and fold their sha256
into the drift fingerprint (preserving thecontents: read+issues: write
security model). - Parameterized the contract-drift harness per spec:
TestPerps*Drift/
TestPerpsScm*Driftvalidate the perps REST + SCM surfaces against their own
specs, alongside the existing prediction-API drift suites. - README /
docs/index.mdbanners note the perps surface (34 REST operations,
6 WS channels, 10 SCM operations).