Use an API key¶
Most sources read public data and need no key. A source that reads data only account holders can see declares the keys it needs, and you give them to it in environment variables. A key is never a command-line option, because the shell keeps every command line in its history.
Find out which keys a source needs¶
ob-analytics sources lists each source. A source that needs a key ends its
line with the environment variables to set:
For example, the kalshi source:
The venues section lists each source that needs a key.
Set the key¶
Set each variable the source names before you run capture. Do not type the
key into the command itself, because that also goes into the shell history.
Keep it in a file and read the file:
Some venues sign each connection with a private key that lives in a file. For those, the variable holds the path of the file, not the key:
chmod 600 ~/.config/venue/private-key.pem
export VENUE_PRIVATE_KEY_PATH=~/.config/venue/private-key.pem
To keep the variables in one place, put them in a file named .env that git
ignores and only you can read (chmod 600 .env), and load it before a
capture:
A key file of one line is read without the spaces and newline around the key. A key file of several lines, such as a PEM file, is read as it is.
If a key is missing, the capture stops before it creates the output directory.
The error names each variable to set and where the venue issues keys. A key
shorter than eight characters is refused, since no venue issues one that short:
use the real key, not a placeholder such as test.
capture reads the key once, when it starts. A long capture keeps running if
you move or replace the key file while it runs.
Give the key from Python¶
A source's settings read an unset key from its environment variable. To give
the key yourself, pass it to the settings. A value you pass is used instead of
the environment. For a key file, pass a Path and the settings read the file:
from pathlib import Path
from pydantic import SecretStr
settings = VenueSettings(
api_key=SecretStr(key), # key: a str from wherever you keep it
private_key=Path("~/.config/venue/private-key.pem"),
)
Build the settings once and give the same settings to every segment of a capture. A factory that builds new settings reads the key again for each segment, and stops the capture if the key file has gone:
from ob_analytics.live import run_capture
run = await run_capture(lambda: VenueSource(settings=settings), config)
What ob-analytics does with a key¶
- The settings hold the key as a pydantic
SecretStr. It prints as**********in arepr, a log line that shows the settings, andmodel_dump. - No file a capture writes holds the key:
orders.csv,depth.csv,trades.csv,raw.jsonl,meta.jsonandmanifest.json. If a venue sends the key back in a message, or an error message quotes it, the file has**********where the key was. - The command line's log does not hold the key either. From Python, the files are cleaned the same way, but a log line that a source writes itself is cleaned only when the command line set up the logging.
ob-analytics cannot protect the environment of the process. Other programs that run as your user can read it.
Venues¶
Kalshi¶
The kalshi source reads Kalshi's WebSocket feed, and signs each connection
with your key. Set two variables:
| Variable | Holds |
|---|---|
KALSHI_API_KEY_ID |
the Key ID Kalshi shows with the key |
KALSHI_PRIVATE_KEY_PATH |
the path of the file that holds the private key |
Kalshi issues keys on the profile page of a Kalshi account, https://kalshi.com/account/profile. Give the key Read all data access only: the capture never trades or moves money. Kalshi shows the private key once, as text; paste it into a file that only you can read. An Ed25519 key (Kalshi's default) and an RSA key both work. Capture Kalshi shows a capture.
Kalshi's REST capture through the ccxt source needs no key: see Capture
Kalshi.
Kraken¶
The kraken source reads Kraken's per-order (level3) WebSocket channel. It
uses your key to ask Kraken's REST API for a token, which opens the channel.
Set two variables:
| Variable | Holds |
|---|---|
KRAKEN_API_KEY |
the API key |
KRAKEN_API_SECRET |
the key's private key (Kraken calls it the secret) |
Kraken issues keys in the API settings of a Kraken account, https://pro.kraken.com/app/settings/api. A normal account can make one at no cost. Turn on WebSocket interface. Kraken also asks for at least one permission: tick one that only reads, such as Query Open Orders & Trades, and leave every permission that trades or moves funds off. The capture never trades or moves funds. Kraken shows both values once; save each in a file that only you can read. Capture Kraken order by order shows a capture.
Kraken's price-level book through the ccxt source needs no key: see Capture
CCXT venues.
Coinbase's price-level (L2) book needs no key either: see Capture Coinbase. Its per-order feed is not reachable with a normal account's key.
Write a source that needs a key¶
Extending ob-analytics shows how a source declares its keys.