CLI sign-in
CLI sign-in lets a developer sign in to the gateway from a terminal with the same single sign-on the Admin UI uses, instead of pasting a virtual key into a shell profile. spanwayctl login opens the browser, the person signs in as themselves, and the CLI stores a short-lived key locally. From then on the keys, models, teams and users commands run as that person, with their role and team, and the key can be handed to coding tools so their traffic goes through the gateway. See the CLI reference.
CLI sign-in needs Admin UI SSO to be configured on the gateway. It is part of the free gateway within the same SSO user limit.
A real-world scenario
A developer wants Claude Code to use the company gateway so their usage counts against their team's budget and the security team sees it in Logs. Instead of asking an admin for a key and saving it in a file, they run spanwayctl login --config-claude, sign in with the company identity provider in the browser, pick their team, and Claude Code is configured. The key expires after SPANWAY_CLI_JWT_EXPIRATION_HOURS, 24 hours by default, and nothing long-lived sits on the laptop.
Before you start
When the gateway runs more than one replica or worker, every replica must share a Redis cache, spanway_settings.cache or general_settings.coordination_redis. The sign-in is started on one replica and finished on another, and without a shared cache the second one cannot find it. PKCE refresh tokens and sign-out also need it.
Steps
-
Point the CLI at the gateway.
export SPANWAY_PROXY_URL="https://gateway.example.com" -
Start the sign-in. Behind the scenes the CLI calls
POST /sso/cli/startand the gateway answers with a login id, a poll secret and a short verification code.spanwayctl login{"login_id": "cli-...", "poll_secret": "...", "user_code": "KJ7Q-4M2P", "expires_in": 600} -
The CLI opens your browser at
/sso/key/generatefor that login id and prints the verification code in the terminal. Sign in with your identity provider in the browser. -
The browser page then asks for the Verification code. Type the code from the terminal. This proves the browser and the terminal belong to the same person, so a link opened by someone else cannot complete your login.
-
Back in the terminal the CLI polls
/sso/cli/poll/{login_id}with the poll secret for up to ten minutes, the time the gateway keeps the sign-in open. The/sso/cli/...routes are only forspanwayctl, so they are not listed in the API reference. When you belong to more than one team it asks which team to use, then receives the key and stores it. -
Confirm who you are.
spanwayctl whoami
Expected result
spanwayctl whoami prints your user email, user id and role, and spanwayctl keys list and the other commands run as you. In the Admin UI, Logs attributes every call made with the stored key to your user and team. The key is a session key that expires, so run spanwayctl login again when a command answers 401.
When general_settings.allow_cli_sso_verification_uri_complete is true the browser link pre-fills the code, which is convenient when the terminal and browser are on the same machine. It is off by default.
Browser sign-in with PKCE
spanwayctl login --pkce uses the OAuth authorization code flow with PKCE through your system browser and a loopback redirect to the CLI. There is no code to type, and the CLI receives a refresh token, so the key is renewed automatically instead of expiring at the end of the session.
spanwayctl login --pkce
The CLI discovers the authorization and token endpoints from /.well-known/spanway-cli-auth on the gateway, generates a code verifier and challenge, opens the browser, and exchanges the returned code for tokens on the loopback port. The gateway must serve the discovery document for this mode. If it does not, use the device code flow above. If your identity provider itself requires PKCE for the Admin UI flow, set GENERIC_CLIENT_USE_PKCE=true on the gateway as described in Admin UI SSO.
Configuring Claude Code
spanwayctl login --config-claude writes the new key into ~/.claude/settings.json so Claude Code sends its traffic through the gateway. Other settings in that file are preserved. Run the command again after the key expires, or use --pkce to have it renewed. spanwayctl auth print-token prints the current key for scripts that need it, and Claude Code can use it as its apiKeyHelper, which renews a PKCE token before printing it.
Signing out
spanwayctl logout deletes the stored credential. A key passed in SPANWAY_PROXY_API_KEY takes the place of a login for every command.
Errors
| What you see | Why | Fix |
|---|---|---|
| The browser shows the gateway login page with no SSO button. | SSO is not configured on the gateway. | Configure Admin UI SSO first. |
400 Invalid verification code in the browser. | The code was mistyped, or the login id in the link belongs to another terminal session. | Type the code the terminal printed, or start again with spanwayctl login. |
The CLI keeps printing Still waiting for authentication... and then Authentication timed out. Please try again. | The browser step was never finished, the login expired after expires_in seconds, or the code was not entered. | Run spanwayctl login again and complete the browser step within ten minutes. |
429 from /sso/cli/start. | Too many sign-ins were started from the same address. | Wait a minute. The limit counts the client address described in IP and route allow-lists. |
403 User does not belong to team | The team chosen at sign-in is not one of yours. | Pick a team from the list the CLI offers. |
--pkce fails saying the discovery document is missing. | The gateway does not serve /.well-known/spanway-cli-auth. | Use the default device code flow. |
Commands answer 401 after a while. | The stored key expired after SPANWAY_CLI_JWT_EXPIRATION_HOURS. | Run spanwayctl login again, or use --pkce so it renews itself. |
400 CLI login session not found or expired. Run spanwayctl login again. ... | The login expired, or the gateway runs several replicas without a shared Redis cache. | Start again. If it fails right after starting, configure Redis for every replica. |
400 Your spanway CLI is out of date ... | An old CLI uses a sign-in flow the gateway no longer supports. | Use the spanwayctl from the current image. |
400 Invalid CLI login session id or 400 CLI login is not ready | The login id is malformed, or the browser step is not finished yet. | Run spanwayctl login again and finish the browser step. |
403 Invalid CLI polling secret | The poll came without the secret from /sso/cli/start, or with another session's secret. | Use the CLI, which sends it for you. |
500 Failed to process CLI SSO. The gateway log has the cause. or 500 Error checking session status. The gateway log has the cause. | Something failed inside the gateway, often the cache. | Read the Error with CLI SSO callback or Error polling for CLI JWT log line. |
The /sso/cli/* routes are called only by spanwayctl and the sign-in page, so they are left out of the published API reference, like the other sign-in routes. This page and the error reference are their reference.
Audit
Every credential the gateway issues to spanwayctl login writes an audit row on Spanway_UserTable with action invoked and event cli_sign_in, the user, the team it was issued for and how many hours it lasts, never the credential. A sign-in through the browser and PKCE writes oauth_credential_issued, each renewal of the refresh token writes oauth_credential_refreshed and spanwayctl logout writes oauth_credential_revoked, each with the client and team, never a token, see audit logs.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| WARNING | CLI SSO login session not found in cache for login_id=<id>. If the proxy runs multiple replicas, a shared Redis cache is required ... | A poll or browser step reached a replica that cannot see the login. | Configure a shared Redis cache when this repeats. |
| ERROR | Error with CLI SSO callback: <reason> or Error polling for CLI JWT: <reason> | The browser step or a poll failed. The CLI only saw a fixed message. | Fix what the reason names, usually the cache or the database. |
Related
- Admin UI single sign-on sets up the identity provider the CLI uses.
- CLI reference lists every
spanwayctlcommand. - Team membership from SSO decides which teams the CLI offers at sign-in.
- Virtual keys for service accounts that cannot sign in through a browser.