Authentication Methods
The CLI supports three ways to provide authentication credentials, in order of precedence:Configuration File
When you runhc auth login, credentials are saved to ~/.harness/auth.json:
The config file is created with
0600 permissions (read/write for owner only) to protect your credentials.Authentication Commands
Login
Authenticate with Harness and save credentials:- Validates credentials by calling the Harness API
- Extracts the account ID from the token (format:
pat.AccountID.Random.Random) - Saves the configuration to
~/.harness/auth.json
Check Status
Verify your authentication status:Logout
Remove saved credentials:~/.harness/auth.json file and clears the session.
Environment Variables
You can override configuration file values using environment variables:| Environment Variable | Description | Config File Field |
|---|---|---|
HARNESS_API_URL | Base URL for the API | base_url |
HARNESS_API_KEY | Authentication token | token |
HARNESS_ORG_ID | Organization identifier | org_id |
HARNESS_PROJECT_ID | Project identifier | project_id |
- Linux/macOS
- Windows (PowerShell)
Command-line Flags
Flags provide the highest precedence and override both environment variables and the config file:--api-url: Base URL for the API--token: Authentication token--account: Account identifier--org: Organization identifier--project: Project identifier
Precedence Order
When the CLI loads configuration, it follows this precedence (highest to lowest):Token Format
Harness personal access tokens follow this format:--account flag during login.
API Validation
During login, the CLI validates credentials by making a GET request to:- Header:
x-api-key: {token} - Timeout: 10 seconds
CI/CD Usage
For CI/CD pipelines, use environment variables instead ofhc auth login:
Store tokens as secrets in your CI/CD platform. Never commit tokens to version control.
Security Best Practices
- Protect your config file: The CLI sets
~/.harness/auth.jsonwith0600permissions - Use environment variables in CI/CD: Don’t save credentials to files in automated environments
- Rotate tokens regularly: Generate new API tokens periodically
- Use minimal permissions: Create tokens with only the permissions needed
- Never commit credentials: Add
.harness/to your.gitignore
Troubleshooting
Error: Not logged in
Error: Not logged in
Run
hc auth login to authenticate, or set environment variables:Error: authentication failed with status 401
Error: authentication failed with status 401
Your token may be invalid or expired. Generate a new token from the Harness UI:
- Go to Account Settings → Access Control → API Keys
- Create a new Personal Access Token
- Run
hc auth loginwith the new token
Error: token does not contains accountID
Error: token does not contains accountID
Your token format is invalid. Harness tokens should follow the format:
pat.{AccountID}.{Random}.{Random}Which authentication method is being used?
Which authentication method is being used?
Run with the
--verbose flag to see configuration details: