API reference
finlab
On this page8
The finlab module handles login and credentials. Log in once in a browser on your own computer or in Colab and it is reused from then on; environments without a browser, such as servers, Docker and schedulers, use environment-variable credentials instead.
Which function to use
| You want to | Use |
|---|---|
| Log in once on your computer and reuse it | python -m finlab login |
| Log in from Python, Jupyter or Colab | finlab.login() |
| Run on a server, Docker or scheduler without a browser | The three environment variables from python -m finlab token --env |
| Check login status and plan | python -m finlab status |
| Check in code whether you are logged in, or get the token | finlab.get_token() |
| Log out | python -m finlab logout |
Migrate from the legacy FINLAB_API_TOKEN | python -m finlab migrate |
Where your code runs decides how it logs in. Pick a login for each of the four machines below and watch where FinLab looks for credentials, in order:
Login match: log in the way that fits where the code runs
Mission 1 of 3 Tap “Your computer” and “Google Colab” and pick the right login for each
How does it log in?
FinLab looks for credentials in order
- 1 FINLAB_REFRESH_TOKEN + FINLAB_SESSION_ID + FINLAB_API_KEY The three variables, only when all three are set 0/3
- 2 ~/.finlab/credentials.json Saved by browser login no
- 3 FINLAB_ID_TOKEN For browser / Pyodide runtimes no
- 4 FINLAB_API_TOKEN Legacy token, deprecated no
- 5 Interactive login If none of the above, start a browser login
No credentials yet: a browser login every time.
The four machines are illustrative; the login ways and the lookup order come from this page.
Log in on your computer or in Colab
Your own computer (recommended)
Run this once in a terminal; every Python session on the same machine then reuses the stored credentials:
python -m finlab loginOr log in from Python / Jupyter:
import finlab
# 1. Call login() without arguments
# 2. The browser opens the FinLab login page automatically
finlab.login()
# 3. After a successful login, credentials are saved to ~/.finlab/credentials.json
# 4. No need to log in again for subsequent use- Credential storage location:
~/.finlab/credentials.json - Validity period: 60 days (auto-refreshes)
If browser login fails, is rejected, or times out, finlab.login() raises finlab.exceptions.LoginError (a RuntimeError subclass) instead of returning silently.
Google Colab
import finlab
finlab.login() # follow the prompt to finish logging in in the browserColab logs in under the name colab. Creating this login record uses one SDK device slot. If the same account already has a record with that name, logging in again reuses it.
finlab.login()
Logs in to FinLab; called without arguments, it starts the browser login.
import finlab
finlab.login()On success the credentials are saved to ~/.finlab/credentials.json and reused automatically on the same machine. In Colab, call finlab.login() the same way. For environments without a browser (servers, schedulers), run python -m finlab token --env on your own machine and set FINLAB_REFRESH_TOKEN, FINLAB_SESSION_ID and FINLAB_API_KEY.
| Parameter | Type | Default | Description |
|---|---|---|---|
api_token | str or None | None | Deprecated legacy API token. Use browser login instead. |
Returns None. Raises LoginError when the browser login fails, is rejected, or times out.
Check your SDK device slots
Sign in to device management to see your login records and the device count, shown as X / Y:
- X is the number of SDK device slots currently in use.
- Y is the SDK device limit for your plan.
- Available slots are Y minus X.
Use the Y shown on this page. The terms of service mention three devices or browsers for using the website. That wording does not mean every plan includes three SDK device slots.
Use it on servers, Docker and schedulers
Environments without a browser don't call finlab.login(). First export credentials on a machine where you are logged in:
python -m finlab token --env
# export FINLAB_REFRESH_TOKEN="..."
# export FINLAB_SESSION_ID="..."
# export FINLAB_API_KEY="..."Then set these three values as environment variables in the target environment (docker-compose, GitHub Actions secrets, Cloud Functions variables, ...). Your code does not need to call finlab.login():
from finlab import data
data.set_market('us')
close = data.get('price:adj_close') # picks up FINLAB_REFRESH_TOKEN etc. automaticallyCheck your login and log out
Check the status or log out in a terminal:
python -m finlab status # show login status and plan
python -m finlab logout # clear local credentials and remove the server sessionIf you set environment-variable credentials, also remove FINLAB_REFRESH_TOKEN, FINLAB_SESSION_ID, FINLAB_API_KEY (and the legacy FINLAB_API_TOKEN) when you log out.
finlab.get_token()
Returns the authentication token of the logged-in member; use it to check in code whether you are logged in.
import finlab
try:
token, token_type = finlab.get_token()
print(f"✅ Logged in ({token_type})")
except RuntimeError:
print("❌ Not logged in")Credentials are looked up in this order:
- Login credentials: the
FINLAB_REFRESH_TOKEN,FINLAB_SESSION_IDandFINLAB_API_KEYenvironment variables when all three are set, otherwise~/.finlab/credentials.jsonfrom browser login; returns anid_token - The
FINLAB_ID_TOKENenvironment variable, returns anid_token - The legacy
FINLAB_API_TOKENenvironment variable, returns anapi_token - Interactive login
Takes no parameters. Returns a tuple of (token_string, token_type), where token_type is 'id_token' or 'api_token'.
How FinLab finds your credentials
| Method | Pros | Cons | Use cases |
|---|---|---|---|
Browser login (python -m finlab login / finlab.login()) | Secure, convenient, auto-refreshes token | Needs a browser | Local development, Jupyter, Colab |
Environment-variable credentials (python -m finlab token --env) | No browser needed, nothing hardcoded | Requires one local login first | Servers, Docker, schedulers, CI |
FINLAB_API_TOKEN (deprecated) | — | Deprecated, will be removed | Please migrate |
FinLab looks for credentials in the following order:
FINLAB_REFRESH_TOKEN+FINLAB_SESSION_ID+FINLAB_API_KEYenvironment variables (only when all three are set)~/.finlab/credentials.json: credentials generated by browser loginFINLAB_ID_TOKENenvironment variable: used by browser / Pyodide runtimesFINLAB_API_TOKENenvironment variable: legacy API token (deprecated, warns)- Interactive login: starts browser login if none of the above exist
Common questions
What if browser login fails?
finlab.login() raises LoginError with instructions. Common causes:
Missing cryptography package
pip install cryptographyNo browser environment (e.g., server)
- Run
python -m finlab loginon your own machine, thenpython -m finlab token --env - Set the printed
FINLAB_REFRESH_TOKEN,FINLAB_SESSION_IDandFINLAB_API_KEYon the server
- Run
Login timed out or blocked by a firewall
- Run
finlab.login()again and finish the browser login before it expires - Check network settings
- Run
I still use FINLAB_API_TOKEN. What should I do?
Run python -m finlab migrate for the steps: use python -m finlab login locally, use the three variables from python -m finlab token --env in headless environments, then remove FINLAB_API_TOKEN.
Next steps
- Command-line interface (CLI) The
login,token,status,logoutandmigratecommands. - Quickstart From installing and logging in to your first backtest.
- FAQ More questions on login, data download and backtesting.
Was this page helpful?