MCPcopy
hub / github.com/codeaashu/claude-code / verifyApiKey

Function verifyApiKey

src/services/api/claude.ts:530–586  ·  view source on GitHub ↗
(
  apiKey: string,
  isNonInteractiveSession: boolean,
)

Source from the content-addressed store, hash-verified

528}
529
530export async function verifyApiKey(
531 apiKey: string,
532 isNonInteractiveSession: boolean,
533): Promise<boolean> {
534 // Skip API verification if running in print mode (isNonInteractiveSession)
535 if (isNonInteractiveSession) {
536 return true
537 }
538
539 try {
540 // WARNING: if you change this to use a non-Haiku model, this request will fail in 1P unless it uses getCLISyspromptPrefix.
541 const model = getSmallFastModel()
542 const betas = getModelBetas(model)
543 return await returnValue(
544 withRetry(
545 () =>
546 getAnthropicClient({
547 apiKey,
548 maxRetries: 3,
549 model,
550 source: 'verify_api_key',
551 }),
552 async anthropic => {
553 const messages: MessageParam[] = [{ role: 'user', content: 'test' }]
554 // biome-ignore lint/plugin: API key verification is intentionally a minimal direct call
555 await anthropic.beta.messages.create({
556 model,
557 max_tokens: 1,
558 messages,
559 temperature: 1,
560 ...(betas.length > 0 && { betas }),
561 metadata: getAPIMetadata(),
562 ...getExtraBodyParams(),
563 })
564 return true
565 },
566 { maxRetries: 2, model, thinkingConfig: { type: 'disabled' } }, // Use fewer retries for API key verification
567 ),
568 )
569 } catch (errorFromRetry) {
570 let error = errorFromRetry
571 if (errorFromRetry instanceof CannotRetryError) {
572 error = errorFromRetry.originalError
573 }
574 logError(error)
575 // Check for authentication error
576 if (
577 error instanceof Error &&
578 error.message.includes(
579 '{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}',
580 )
581 ) {
582 return false
583 }
584 throw error
585 }
586}
587

Callers 1

useApiKeyVerificationFunction · 0.85

Calls 8

getSmallFastModelFunction · 0.85
returnValueFunction · 0.85
getAnthropicClientFunction · 0.85
getAPIMetadataFunction · 0.85
getExtraBodyParamsFunction · 0.85
withRetryFunction · 0.70
createMethod · 0.65
logErrorFunction · 0.50

Tested by

no test coverage detected