library(kalshi)
check_openssl_installed()Authenticating with the Kalshi API
The kalshi package uses a secure, “Pure R” approach to authentication. Instead of relying on external Python libraries, it interfaces directly with your system’s openssl tool to perform RSA-PSS signing.
Prerequisite: OpenSSL
This package requires the openssl command-line tool. Most Linux and macOS systems have this pre-installed. You can verify it by running:
First-Time Setup: Generating Keys
If you do not already have an RSA key pair, generate one:
generate_kalshi_keypair("kalshi-key.key")This creates a private .key file and displays the Public Key in your console.
Registering with Kalshi
- Copy the Public Key printed in your console.
- Log in to the Kalshi Developer Dashboard.
- Click “Create API Key” and paste your public key.
- Save the Key ID (a UUID) that Kalshi provides.
Step 1: Environment Variables
Add your Key ID to your .Renviron file:
usethis::edit_r_environ()Add this line:
KALSHI_API=your-uuid-here
Save and restart R.
Step 2: Session Setup
Before making API calls, point the package to your private RSA key:
kalshi_auth_setup("kalshi-key.key")Making API Calls
Once authenticated, you can call any function:
# Get your account balance
bal <- get_balance()
cat("Balance: $", bal$balance / 100, "\n")
# Get market data
markets <- get_markets(status = "open", limit = 10)
# Get series information
series <- get_series_list(max_pages = 1)How Authentication Works
When you call a function like get_markets(): 1. Validation: check_kalshi_auth() ensures you’ve run the setup. 2. Signing: A message (timestamp + method + path) is signed using RSA-PSS via the system openssl binary. 3. Request: httr2 attaches the signature and your Key ID to the headers.
Security Considerations
- Key Safety: Your private key is never read into R memory; it is only accessed by the system OpenSSL tool.
- Git Safety: Ensure
*.keyand.Renvironare in your.gitignore.
Troubleshooting
If you see OpenSSL system call failed, verify the binary is available:
which openssl
openssl versionOn Ubuntu/Debian:
sudo apt update && sudo apt install opensslOn macOS:
brew install openssl