Kalshi
Ruby client for the Kalshi API.
Installation
Install the gem and add to the application’s Gemfile by executing:
bundle add kalshiIf bundler is not being used to manage dependencies, install the gem by executing:
gem install kalshiUsage
Configuration
Configure the gem with your API key (optional for public endpoints). You can set it via the KALSHI_API_KEY environment variable or configure it explicitly:
Rubyists::Kalshi.configure do |config|
config.api_key = 'your_api_key'
config.base_url = 'https://api.elections.kalshi.com/trade-api/v2' # Default
endClient
Initialize the client:
client = Rubyists::Kalshi.clientMarket
Access Market endpoints via the market namespace.
Series List
List series with optional filtering:
# List series with default filter
series_list = client.market.series_list.list
# List series with custom filter
series_list = client.market.series_list.list(category: 'Sports', tags: 'Football')Series
Get a specific series by ticker:
series = client.market.series.fetch('KX-SERIES')Markets
List markets with optional filtering:
# List markets with default filter
markets = client.market.markets.list
# List markets with custom filter
markets = client.market.markets.list(status: 'open', limit: 10)Get a specific market by ticker:
market = client.market.markets.fetch('KX-MARKET')Orderbook
Get the orderbook for a market:
# Get full orderbook
orderbook = client.market.orderbook.fetch('KX-MARKET')
# Get orderbook with specific depth
orderbook = client.market.orderbook.fetch('KX-MARKET', depth: 10)Trades
List trades with optional filtering:
# List trades with default filter
trades = client.market.trades.list
# List trades with custom filter
trades = client.market.trades.list(ticker: 'KX-MARKET', limit: 100)Candlesticks
Get candlesticks for a market:
candlesticks = client.market.candlesticks.fetch(
series_ticker: 'KX-SERIES',
ticker: 'KX-MARKET',
start_ts: 1600000000,
end_ts: 1600000060,
period_interval: 1
)Get candlesticks for multiple markets:
candlesticks = client.market.candlesticks.batch(
tickers: ['KX-MARKET-1', 'KX-MARKET-2'],
series_ticker: 'KX-SERIES',
start_ts: 1600000000,
end_ts: 1600000060,
period_interval: 1
)Search
Access Search endpoints via the search namespace.
Tags by Category
Get tags organized by series categories:
tags = client.search.tags_by_categoriesFilters by Sport
Get filters organized by sport:
filters = client.search.filters_by_sportWebSocket Client
A raw WebSocket client is included in bin/wss-raw for testing connection and subscription.
|
Important
|
This script requires the pass password manager to be installed and configured. It expects the API key on the first line and the PEM private key starting from the third line of the pass entry.
|
|
Note
|
If you aren’t using pass yet, you should! It works on Windows, Android, macOS, Linux, and everything else. It is the standard unix password manager.
|
# Set up your pass entry at the default path
printf '%s\n\n%s\n' "$API_KEY" "$PEM_PRIVATE_KEY" | pass insert -m kalshi/api/initial-dev-ro
# Default pass path: kalshi/api/initial-dev-ro
./bin/wss-raw
# Set up your pass entry at a custom path
printf '%s\n\n%s\n' "$API_KEY" "$PEM_PRIVATE_KEY" | pass insert -m my/custom/path
# Override pass path
KALSHI_PASS_PATH=my/custom/path ./bin/wss-raw|
Important
|
The "$PEM_PRIVATE_KEY" must include the BEGIN and END lines
For example: |
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to [rubygems.org](https://rubygems.org).
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/rubyists/kalshi.