> ## Documentation Index
> Fetch the complete documentation index at: https://polar.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Sign in to Polar from the CLI, choose the organization you work on, create access tokens for your app and authenticate in CI.

The CLI signs in through your browser with OAuth. Sessions are stored in your
operating system's keychain, one per environment, so you can be signed in to
sandbox and production at the same time.

## Signing in

Every login targets one environment. Pass the environment as a flag, or run the
command without one in a terminal and the CLI will ask:

> [tab] Sandbox
>
> ```bash Terminal theme={null}
> polar auth login --sandbox
> ```

> [tab] Production
>
> ```bash Terminal theme={null}
> polar auth login --production
> ```

Your browser opens on Polar's authorization page. Approve the request and
return to the terminal. If a session already exists for that environment, the
CLI keeps it and tells you how to replace it with `--new-session`.

After signing in, the CLI lists every organization you can access across the
environments you are signed in to and asks which one to make active.

## The active organization

Commands that act on an organization, such as `polar webhook listen`, use the active
organization. Its environment comes with it: choosing a sandbox organization
means the CLI talks to sandbox, choosing a production organization means it
talks to production. There is no separate environment switch.

```bash Terminal theme={null}
polar auth org
```

Shows the same organization picker as login, so you can switch at any time.

```bash Terminal theme={null}
polar auth org acme
```

Switches to an organization by slug or ID without the picker, for scripts and
coding agents.

```bash Terminal theme={null}
polar auth whoami
```

Prints which environments you are signed in to and the active organization.

```bash Terminal theme={null}
polar auth list
```

Lists every organization you can access, grouped by environment, with the
active one marked.

To use a different organization for a single command without changing the
active one, pass its ID or slug with `--org`. Every command that acts on an
organization accepts it:

```bash Terminal theme={null}
polar webhook listen http://localhost:3000/webhooks --org <organization-id>
```

## Create an access token for your app

Your application needs an [organization access token](https://polar.sh/docs/integrate/oat.md) to call the API.
`polar auth token` creates one for the active organization and prints it:

```bash Terminal theme={null}
echo "POLAR_ACCESS_TOKEN=$(polar auth token)" >> .env.local
```

The CLI opens the dashboard on a token page for the active organization, named after the
current directory. Choose the scopes and expiration, click **Create and send to CLI**, and
the token arrives in your terminal. Preselect scopes with `--scope`, repeated:

```bash Terminal theme={null}
polar auth token --scope products:read --scope checkouts:write | gh secret set POLAR_ACCESS_TOKEN
```

The token belongs to the active organization's environment. A sandbox token only works
against `https://sandbox-api.polar.sh`.

Tokens created this way show up with the rest in **Settings → Access Tokens** in the
dashboard, where you can also create or revoke them. See
[Organization access tokens](https://polar.sh/docs/integrate/oat.md).

## Signing out

Log out of one environment or all of them:

```bash Terminal theme={null}
polar auth logout --production
polar auth logout --all
```

Run it without a flag in a terminal to pick from your sessions. Logging out
removes the session from your keychain and clears the active organization if it
belonged to that environment.

## CI and headless use

On machines without a browser or keychain, and for coding agents, authenticate
with an access token instead of a session: an
[organization access token](https://polar.sh/docs/integrate/oat.md), or an OAuth or agent access token.

```bash Terminal theme={null}
export POLAR_ACCESS_TOKEN=polar_oat_...
export POLAR_ENVIRONMENT=sandbox
polar webhook listen http://localhost:3000/webhooks
```

`POLAR_ENVIRONMENT` selects the environment the token belongs to and defaults
to `production`. An organization access token belongs to one organization, so
commands use that organization.

> [!NOTE]
>
> While `POLAR_ACCESS_TOKEN` is set, saved sessions are ignored and `polar auth
>     login` and `polar auth org` refuse to run. Unset it to go back to your
> browser session.

## Where things are stored

* Sessions live in the operating system keychain under the service name
  `polar-cli`, with one entry per environment.
* The active organization is saved in `~/.config/polar-cli/config.json`, or
  `$XDG_CONFIG_HOME/polar-cli/config.json` when that variable is set.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.