Home / Market / go-kalshi

go-kalshi

Unofficial Go client for Kalshi, generated from the API specs Kalshi publishes. The root package `kalshi` (`github.com/psyb0t/go-kalshi`) is an oapi-codegen REST client for the Trade API at `https://external-api.kalshi.com/trade-api/v2` with a typed `...WithResponse` method per endpoint (markets, events, series, orderbooks, trades, balance, positions, fills, orders, RFQs and quotes). `kalshiws` (`/ws`) streams market data (orderbook, ticker, trade, fill, lifecycle, RFQ and index pushes) through

skill★ 00 dlclawhubMIT-0Install in CosmosUpstream Source

Documentation & Preview

go-kalshi: setup and reference

This module is unofficial and not affiliated with Kalshi.

Requirements

Install

go get github.com/psyb0t/go-kalshi

The WebSocket transport, github.com/psyb0t/gen-asyncapi/asyncapirt/gorillaws, comes in with the module's own dependency on github.com/psyb0t/gen-asyncapi; run go mod tidy after the first import.

| Import path | Package | Production | Demo | |---|---|---|---| | github.com/psyb0t/go-kalshi | kalshi | https://external-api.kalshi.com/trade-api/v2 | https://external-api.demo.kalshi.co/trade-api/v2 | | github.com/psyb0t/go-kalshi/ws | kalshiws | kalshiws.ServerProductionURL | | | github.com/psyb0t/go-kalshi/perpsws | kalshiperpsws | kalshiperpsws.ServerProductionURL | kalshiperpsws.ServerDemoURL | | github.com/psyb0t/go-kalshi/auth | kalshiauth | | |

Signing

Public endpoints such as GetMarketsWithResponse work without the signer.

REST client

Reading a response

| Field or method | Holds | |---|---| | JSON200 | The decoded body on success, nil otherwise | | JSON401, JSON500 and so on | ErrorResponse (Code, Message, Details), present only for the error statuses that operation declares (GetBalance declares 401 and 500, GetMarkets none) | | Body, StatusCode(), Status(), HTTPResponse | The raw response |

WebSocket clients

kalshiws.Handler methods (embed kalshiws.UnimplementedHandler to skip the ones you do not need):

| Group | Methods | |---|---| | Command responses | HandleSubscribedResponse, HandleUnsubscribedResponse, HandleOkResponse, HandleListSubscriptionsResponse, HandleErrorResponse, HandleSubscribedUnderlyingsResponse, HandleSubscribedIndicesResponse | | Market data | HandleOrderbookSnapshot, HandleOrderbookDelta, HandleTicker, HandleTrade, HandleMarketMetadataUpdated | | Lifecycle | HandleMarketLifecycleV2, HandleEventLifecycle, HandleEventFeeUpdate, HandleMultivariateMarketLifecycle | | Account | HandleFill, HandleMarketPosition, HandleUserOrder, HandleOrderGroupUpdates | | RFQ | HandleRfqCreated, HandleRfqDeleted, HandleQuoteCreated, HandleQuoteAccepted, HandleQuoteExecuted | | Indices | HandleCfbenchmarksValue, HandleCfbenchmarksIndexList, HandleCfbenchmarksValue5Hz, HandleCfbenchmarks5HzIndexList, HandlePythValue, HandlePythUnderlyingList | | Other | HandleUnknownMessage |

kalshiperpsws.Handler has the command responses, HandleOrderbookSnapshot, HandleOrderbookDelta, HandleTicker, HandleTrade, HandleFill, HandleUserOrder, HandleOrderGroupUpdates and HandleUnknownMessage.

Each push payload has Type, Sid (the subscription ID) and Msg with the data.

Errors

| Error | When | |---|---| | commerr.ErrParseFailed | ParsePrivateKeyPEM got no PEM block or an unsupported one. | | commerr.ErrValidationFailed | The key is neither RSA nor Ed25519 (NewSigner, or a PKCS#8 key in ParsePrivateKeyPEM). | | asyncapirt.ErrUnknownMessage | Passed to HandleUnknownMessage for a frame whose type the spec does not list. Run keeps going unless the handler returns an error. | | A validation error | A command you send, or a frame you receive, does not match Kalshi's schema. | | context.Canceled, context.DeadlineExceeded | Run stopped because the context ended (wrapped). |

Use errors.Is to match them.