MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / getAnthropicApiKeyWithSource

Function getAnthropicApiKeyWithSource

src/utils/auth.ts:227–349  ·  view source on GitHub ↗
(
  opts: { skipRetrievingKeyFromApiKeyHelper?: boolean } = {},
)

Source from the content-addressed store, hash-verified

225}
226
227export function getAnthropicApiKeyWithSource(
228 opts: { skipRetrievingKeyFromApiKeyHelper?: boolean } = {},
229): {
230 key: null | string
231 source: ApiKeySource
232} {
233 // --bare: hermetic auth. Only ANTHROPIC_API_KEY env or apiKeyHelper from
234 // the --settings flag. Never touches keychain, config file, or approval
235 // lists. 3P (Bedrock/Vertex/Foundry) uses provider creds, not this path.
236 if (isBareMode()) {
237 if (process.env.ANTHROPIC_API_KEY) {
238 return { key: process.env.ANTHROPIC_API_KEY, source: 'ANTHROPIC_API_KEY' }
239 }
240 if (getConfiguredApiKeyHelper()) {
241 return {
242 key: opts.skipRetrievingKeyFromApiKeyHelper
243 ? null
244 : getApiKeyFromApiKeyHelperCached(),
245 source: 'apiKeyHelper',
246 }
247 }
248 return { key: null, source: 'none' }
249 }
250
251 // On homespace, don't use ANTHROPIC_API_KEY (use Console key instead)
252 // https://anthropic.slack.com/archives/C08428WSLKV/p1747331773214779
253 const apiKeyEnv = isRunningOnHomespace()
254 ? undefined
255 : process.env.ANTHROPIC_API_KEY
256
257 // Always check for direct environment variable when the user ran claude --print.
258 // This is useful for CI, etc.
259 if (preferThirdPartyAuthentication() && apiKeyEnv) {
260 return {
261 key: apiKeyEnv,
262 source: 'ANTHROPIC_API_KEY',
263 }
264 }
265
266 if (isEnvTruthy(process.env.CI) || process.env.NODE_ENV === 'test') {
267 // Check for API key from file descriptor first
268 const apiKeyFromFd = getApiKeyFromFileDescriptor()
269 if (apiKeyFromFd) {
270 return {
271 key: apiKeyFromFd,
272 source: 'ANTHROPIC_API_KEY',
273 }
274 }
275
276 if (
277 !apiKeyEnv &&
278 !process.env.CLAUDE_CODE_OAUTH_TOKEN &&
279 !process.env.CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR
280 ) {
281 throw new Error(
282 'ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN env var is required',
283 )
284 }

Callers 13

isAnthropicAuthEnabledFunction · 0.85
getAnthropicApiKeyFunction · 0.85
hasAnthropicApiKeyAuthFunction · 0.85
getAccountInformationFunction · 0.85
buildSystemInitMessageFunction · 0.85
authStatusFunction · 0.85
isPolicyLimitsEligibleFunction · 0.85
getAuthHeadersFunction · 0.85

Calls 9

isBareModeFunction · 0.85
isRunningOnHomespaceFunction · 0.85
getGlobalConfigFunction · 0.85
normalizeApiKeyForConfigFunction · 0.85
isEnvTruthyFunction · 0.70

Tested by

no test coverage detected