Skip to content

4. Appendix: Issuing and Using API Keys

In the CLI and SDK steps up to this page, we authenticated using the login credentials from ant auth login. No API key was issued. This page explains how authentication works and how to issue and configure an API key when you need one.

No API Key Needed If You Are Logged In via the CLI

Section titled “No API Key Needed If You Are Logged In via the CLI”

The ant CLI and the anthropic package (SDK) look for credentials in the following order of precedence:

  1. The ANTHROPIC_API_KEY environment variable
  2. The ANTHROPIC_AUTH_TOKEN environment variable
  3. The login credentials saved by ant auth login (%APPDATA%\Anthropic on Windows, ~/.config/anthropic/ on macOS)

Therefore, on a machine where you have already run ant auth login, both the CLI and an SDK client created with a no-argument Anthropic() work as-is with no extra configuration. Token expiration is also refreshed automatically.

If you are unsure which credentials are being used, you can check with the following command.

Terminal window
./ant auth status

Use an API key instead of login credentials in situations like these:

  • Running in environments where browser-based login is not possible, such as CI or servers
  • Using only the SDK on a machine where ant is not installed
  • Embedding into an application shared by your team
  1. Select the “API keys” menu in the Console and click “Create key”.

  2. Enter a name and click “Add”. Note down the displayed API key (a string starting with sk-ant-).

Set it in the ANTHROPIC_API_KEY environment variable, and both the CLI and the SDK will use it automatically.

Terminal window
$env:ANTHROPIC_API_KEY = "YOUR_API_KEY_HERE"