# Authenticate with the Python SDK

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:

```bash
export ROBOFLOW_API_KEY=rf_xxxxx
```

```python
import roboflow

rf = roboflow.Roboflow()  # picks up ROBOFLOW_API_KEY
```

This 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

```python
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

```python
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:

```python
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](https://app.roboflow.com/), 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](/developer/authentication/scoped-api-keys.md) (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`:

```bash
export ROBOFLOW_CONFIG_DIR=/etc/roboflow
```

See [Environment Variables](/developer/environment-variables.md) for the full list of variables the SDK and CLI respect.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/developer/python-sdk/authenticate-with-the-python-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
