• 訊息
  • 託管代理
  • 管理
Search...
⌘K
CLI、SDK 與函式庫
概覽
ant CLI
快速入門驗證選項使用 CLI指令碼與自動化
用戶端 SDK
中介軟體PythonTypeScriptC#GoJavaPHPRuby
函式庫與整合
Apple Foundation ModelsOpenAI SDK 相容性
Log in
驗證選項
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
CLI、SDK 與函式庫/ant CLI

CLI 驗證選項

使用互動式登入、API 金鑰、具名設定檔和 Workload Identity Federation 來驗證 ant CLI。

ant CLI 支援多種憑證來源。快速入門涵蓋了單一命令的快速路徑(ant auth login)。本頁面則完整介紹所有選項。

互動式登入

ant auth login 讓您無需建立或管理 API 金鑰即可呼叫 API。它會針對 Claude Console 開啟基於瀏覽器的 OAuth 流程,並將產生的憑證儲存在 $ANTHROPIC_CONFIG_DIR 下(請參閱設定目錄以了解各作業系統的預設值)。在遠端主機或任何沒有本機瀏覽器的環境中,傳遞 --no-browser 以列印授權 URL,然後將傳回的代碼貼回終端機。

CLI
ant auth login

# 在沒有瀏覽器的遠端主機上:
ant auth login --no-browser

# 綁定至特定工作區並略過瀏覽器選擇器:
ant auth login --workspace-id wrkspc_01...

# 若您透過 --profile 傳入的具名設定檔不存在,
# 系統將以該名稱建立新的具名設定檔。
ant auth login --profile <profile-name>

在瀏覽器流程中,您會選擇一個組織,然後選擇一個 workspace(工作區)。發行的權杖會限定於該工作區,因此 CLI 只能看到屬於該工作區的資源。傳遞 --workspace-id 可直接綁定並跳過選擇器。若要在多個工作區中工作,請參閱在工作區之間切換。

互動式登入適用於本機開發以及在您自己的機器上編寫指令碼。對於非互動式工作負載(例如 CI、伺服器和容器),請改用 Workload Identity Federation。

登入會將憑證寫入 credentials/<profile>.json。設定檔的首次登入也會建立 configs/<profile>.json 並將其設為使用中的設定檔。若要移除已儲存的憑證,請執行 ant auth logout,或執行 ant auth logout --all 以清除所有設定檔。

API 金鑰

CLI 也會從 ANTHROPIC_API_KEY 環境變數讀取您的 API 金鑰。請從 Claude Console 取得金鑰。

若要針對單次呼叫覆寫金鑰,請傳遞 --api-key。若要指向不同的 API 主機,請設定 ANTHROPIC_BASE_URL 或傳遞 --base-url。

檢查驗證狀態

ant auth status 會列印 CLI 所選擇的憑證來源(API 金鑰環境變數、OAuth 登入、聯合身分或設定檔)、使用中的設定檔、使用中權杖所綁定的工作區,以及設定目錄路徑。使用此命令來診斷工作負載為何選擇了錯誤的憑證或工作區。

CLI
ant auth status
Active profile:  default
Config dir:      ~/.config/anthropic
Profile config:  ~/.config/anthropic/configs/default.json
Credentials:     ~/.config/anthropic/credentials/default.json

Credentials
  (active) * Profile (user_oauth) [via active_config]       sk-ant-oat01-EXA...
...

Workspace
  (active) * Workspace                                      wrkspc_01... (Engineering)

閱讀 (active) 列以查看哪個憑證來源和工作區勝出。此命令回報的是狀態而非執行健康檢查,因此請勿根據結束狀態編寫指令碼。如需憑證來源的完整排序,請參閱憑證優先順序。

在工作區之間切換

互動式登入的權杖會綁定至單一工作區。若要針對多個工作區使用 CLI,請在各自的具名設定檔下登入每個工作區,然後在它們之間切換:

CLI
# 1. 建立設定檔(互動式;在瀏覽器中選取另一個工作區,
#    或傳入 --workspace-id 以略過選取器):
# ant auth login --profile other-ws

# 2. 將其設為後續命令的預設值:
ant profile activate other-ws

# 3. 或僅針對單一命令選用它,而不變更預設值:
ant --profile other-ws models list
ANTHROPIC_PROFILE=other-ws ant models list

執行 ant auth status 以確認哪個設定檔和工作區處於使用中狀態。

只有在未設定 API 金鑰時才會查詢設定檔。如果您的環境中存在 ANTHROPIC_API_KEY,它會覆寫所有設定檔,且這些命令都會使用該金鑰所限定的工作區。在切換設定檔之前,請先取消設定該變數。

管理設定檔

ant profile 子命令可直接檢查和編輯設定檔狀態:

CLI
ant profile list
ant profile get --profile other-ws
ant profile set workspace_id wrkspc_01... --profile other-ws

ant profile set 的可寫入鍵為 workspace_id、base_url、organization_id、scope、client_id 和 console_url。設定 workspace_id 會在設定檔組態中記錄目標工作區,但不會重新綁定已發行的憑證;請在該設定檔下再次執行 ant auth login,以為新工作區產生權杖。

如需設定檔的檔案結構描述和聯合身分區塊,請參閱設定檔組態檔案。如需 Workload Identity Federation,請參閱驗證概觀和 WIF 參考。

Was this page helpful?

  • 互動式登入
  • API 金鑰
  • 檢查驗證狀態
  • 在工作區之間切換
  • 管理設定檔