Claude Code Series Tutorial 6: How to Authenticate - Browser Login and Manual API Key Configuration
Authentication Flow: Browser Login and Manual API Key Configuration
After installing Claude Code, you need to complete authentication before using it. You need to bind the tool to your Anthropic account. There are two ways to do this: browser login and manual API key configuration. Both methods achieve the same result—choose whichever is more convenient for you.
Method 1: Browser Login (Recommended)
Claude Code provides an automatic authentication command claude login, which opens a browser for you to authorize on the web page and then automatically writes the credentials back to the terminal.
Steps:
- Enter in the terminal:
bash
claude login
-
The system will output a message and automatically open the Anthropic authorization page in your default browser.
If it doesn't open automatically, a link will be printed in the terminal—you can manually copy and open it in a browser. -
Log in to your Anthropic account in the browser (if already logged in, the page will directly request authorization).
-
Click "Allow" or "Authorize", and the browser page will display "Authorization successful, you can close this page".
-
Return to the terminal, and you will see a prompt indicating authentication is complete. Now running the
claudecommand will be associated with your account.
Method 2: Manual API Key Configuration
If you are using a server or container without a graphical interface, or prefer not to go through browser authorization, you can obtain an API key from the Anthropic console and configure it in Claude Code.
Steps:
-
Open your browser, visit
https://console.anthropic.com, and log in to your Anthropic account. -
Go to the API Keys page (usually under Settings or Developer options), and click "Create API Key".
-
Give the key a name (e.g.,
claude-code-local), and copy it immediately after generation.
Note: This key is only shown once; you won't see it again after closing the page. If you forget to save it, delete the old one and regenerate. -
Return to the terminal. There are two ways to provide the key to Claude Code:
A. Environment Variable Method:
bash
export ANTHROPIC_API_KEY=your-api-key
Add this line to your ~/.bashrc or ~/.zshrc to make it automatically effective every time.
B. Configuration File Method:
Claude Code also reads its own configuration file. Manually edit ~/.claude/config.json (create it if it doesn't exist), and write:
json
{
"apiKey": "your-api-key"
}
Note: It is recommended to set the file permissions to 600 (chmod 600 ~/.claude/config.json) to prevent other users from reading it.
- Test the connection:
bash
claude "Hello, please reply with 'OK'"
If you receive a normal response, the key is configured correctly.
Common mistakes with manual configuration:
- The key should not start with
sk-ant-orsk-? Actually, Anthropic API keys do start withsk-ant-api03-followed by a long string. Be careful not to miss any characters when copying. - If you set both the environment variable and the file, the environment variable takes precedence. Keep only one method to avoid confusion.
- The key is a billing credential. Do not commit it to a Git repository. It is recommended to add
~/.claudedirectory or configuration files to.gitignore.
Switching Accounts or Logging Out
To switch accounts or clear local credentials:
- If you used
claude login: Runclaude logout, then runclaude loginagain. - If you manually configured a key: Delete the old key from the environment variable or configuration file and replace it with the new one.
Testing Authentication
claude "Explain what the current directory is for"
If it can read your project overview, the environment is ready.
评论
暂无已展示的评论。
发表评论(匿名)