MCPcopy
hub / github.com/simstudioai/sim / getRotatingApiKey

Function getRotatingApiKey

apps/sim/lib/core/config/api-keys.ts:9–51  ·  view source on GitHub ↗
(provider: string)

Source from the content-addressed store, hash-verified

7 * @throws Error if no API keys are configured for rotation
8 */
9export function getRotatingApiKey(provider: string): string {
10 if (
11 provider !== 'openai' &&
12 provider !== 'anthropic' &&
13 provider !== 'gemini' &&
14 provider !== 'cohere'
15 ) {
16 throw new Error(`No rotation implemented for provider: ${provider}`)
17 }
18
19 const keys = []
20
21 if (provider === 'openai') {
22 if (env.OPENAI_API_KEY_1) keys.push(env.OPENAI_API_KEY_1)
23 if (env.OPENAI_API_KEY_2) keys.push(env.OPENAI_API_KEY_2)
24 if (env.OPENAI_API_KEY_3) keys.push(env.OPENAI_API_KEY_3)
25 } else if (provider === 'anthropic') {
26 if (env.ANTHROPIC_API_KEY_1) keys.push(env.ANTHROPIC_API_KEY_1)
27 if (env.ANTHROPIC_API_KEY_2) keys.push(env.ANTHROPIC_API_KEY_2)
28 if (env.ANTHROPIC_API_KEY_3) keys.push(env.ANTHROPIC_API_KEY_3)
29 } else if (provider === 'gemini') {
30 if (env.GEMINI_API_KEY_1) keys.push(env.GEMINI_API_KEY_1)
31 if (env.GEMINI_API_KEY_2) keys.push(env.GEMINI_API_KEY_2)
32 if (env.GEMINI_API_KEY_3) keys.push(env.GEMINI_API_KEY_3)
33 } else if (provider === 'cohere') {
34 if (env.COHERE_API_KEY_1) keys.push(env.COHERE_API_KEY_1)
35 if (env.COHERE_API_KEY_2) keys.push(env.COHERE_API_KEY_2)
36 if (env.COHERE_API_KEY_3) keys.push(env.COHERE_API_KEY_3)
37 }
38
39 if (keys.length === 0) {
40 throw new Error(
41 `No API keys configured for rotation. Please configure ${provider.toUpperCase()}_API_KEY_1, ${provider.toUpperCase()}_API_KEY_2, or ${provider.toUpperCase()}_API_KEY_3.`
42 )
43 }
44
45 // Simple round-robin rotation based on current minute
46 // This distributes load across keys and is stateless
47 const currentMinute = new Date().getMinutes()
48 const keyIndex = currentMinute % keys.length
49
50 return keys[keyIndex]
51}

Callers 7

getApiKeyWithBYOKFunction · 0.90
utils.test.tsFile · 0.90
executeFunction · 0.90
resolveCohereKeyFunction · 0.90
resolveOpenAIKeyFunction · 0.90
resolveGeminiKeyFunction · 0.90
getApiKeyFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected