MCPcopy Index your code
hub / github.com/codeaashu/claude-code / getAnthropicClient

Function getAnthropicClient

src/services/api/client.ts:88–316  ·  view source on GitHub ↗
({
  apiKey,
  maxRetries,
  model,
  fetchOverride,
  source,
}: {
  apiKey?: string
  maxRetries: number
  model?: string
  fetchOverride?: ClientOptions['fetch']
  source?: string
})

Source from the content-addressed store, hash-verified

86}
87
88export async function getAnthropicClient({
89 apiKey,
90 maxRetries,
91 model,
92 fetchOverride,
93 source,
94}: {
95 apiKey?: string
96 maxRetries: number
97 model?: string
98 fetchOverride?: ClientOptions['fetch']
99 source?: string
100}): Promise<Anthropic> {
101 const containerId = process.env.CLAUDE_CODE_CONTAINER_ID
102 const remoteSessionId = process.env.CLAUDE_CODE_REMOTE_SESSION_ID
103 const clientApp = process.env.CLAUDE_AGENT_SDK_CLIENT_APP
104 const customHeaders = getCustomHeaders()
105 const defaultHeaders: { [key: string]: string } = {
106 'x-app': 'cli',
107 'User-Agent': getUserAgent(),
108 'X-Claude-Code-Session-Id': getSessionId(),
109 ...customHeaders,
110 ...(containerId ? { 'x-claude-remote-container-id': containerId } : {}),
111 ...(remoteSessionId
112 ? { 'x-claude-remote-session-id': remoteSessionId }
113 : {}),
114 // SDK consumers can identify their app/library for backend analytics
115 ...(clientApp ? { 'x-client-app': clientApp } : {}),
116 }
117
118 // Log API client configuration for HFI debugging
119 logForDebugging(
120 `[API:request] Creating client, ANTHROPIC_CUSTOM_HEADERS present: ${!!process.env.ANTHROPIC_CUSTOM_HEADERS}, has Authorization header: ${!!customHeaders['Authorization']}`,
121 )
122
123 // Add additional protection header if enabled via env var
124 const additionalProtectionEnabled = isEnvTruthy(
125 process.env.CLAUDE_CODE_ADDITIONAL_PROTECTION,
126 )
127 if (additionalProtectionEnabled) {
128 defaultHeaders['x-anthropic-additional-protection'] = 'true'
129 }
130
131 logForDebugging('[API:auth] OAuth token check starting')
132 await checkAndRefreshOAuthTokenIfNeeded()
133 logForDebugging('[API:auth] OAuth token check complete')
134
135 if (!isClaudeAISubscriber()) {
136 await configureApiKeyHeaders(defaultHeaders, getIsNonInteractiveSession())
137 }
138
139 const resolvedFetch = buildFetch(fetchOverride, source)
140
141 const ARGS = {
142 defaultHeaders,
143 maxRetries,
144 timeout: parseInt(process.env.API_TIMEOUT_MS || String(600 * 1000), 10),
145 dangerouslyAllowBrowser: true,

Callers 8

sideQueryFunction · 0.85
refreshModelCapabilitiesFunction · 0.85
makeTestQueryFunction · 0.85
verifyApiKeyFunction · 0.85
queryModelFunction · 0.85

Calls 15

getCustomHeadersFunction · 0.85
getUserAgentFunction · 0.85
getSessionIdFunction · 0.85
logForDebuggingFunction · 0.85
isEnvTruthyFunction · 0.85
configureApiKeyHeadersFunction · 0.85
buildFetchFunction · 0.85
getProxyFetchOptionsFunction · 0.85
getSmallFastModelFunction · 0.85
getAWSRegionFunction · 0.85

Tested by

no test coverage detected