The Python SDK accepts an API key three ways. Pick whichever fits your runtime - they all end up at the same Roboflow object.
Option 1: Environment variable (recommended for scripts and agents)
Set ROBOFLOW_API_KEY in your shell or CI environment:
export ROBOFLOW_API_KEY=rf_xxxxximport roboflow
rf = roboflow.Roboflow() # picks up ROBOFLOW_API_KEYThis is the cleanest path for scripts, notebooks, and AI coding agents - no interactive prompts and no on-disk state.
Option 2: Pass the key explicitly
import roboflow
rf = roboflow.Roboflow(api_key="rf_xxxxx")Useful when you have multiple keys for multiple workspaces in the same process.
Option 3: Interactive login
import roboflow
roboflow.login()Opens an authentication URL in the browser, prompts for a token, and saves credentials to ~/.config/roboflow/config.json. Subsequent Roboflow() calls (with no arguments) read from that file.
To re-authenticate against a different account or refresh the token:
roboflow.login(force=True)The CLI's roboflow login writes to the same on-disk location, so once either tool authenticates, the other picks up the credentials too.
Where to find your API key
In the Roboflow web app, navigate to Settings → API Key. Each workspace has its own key - the key you use determines the workspace your code authenticates against.
For scoped API keys (limit a key to specific resources or operations), see the Authentication section.
Override the config location
If you need credentials stored somewhere other than ~/.config/roboflow/, set ROBOFLOW_CONFIG_DIR:
export ROBOFLOW_CONFIG_DIR=/etc/roboflowSee Environment Variables for the full list of variables the SDK and CLI respect.