File size: 8,403 Bytes
c001f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# How to Use Multiple API Keys

## Quick Setup Guide

### Step 1: Set Environment Variables

You can set multiple API keys. The system loads them in order:
- `GEMINI_API_KEY` β†’ **Index 0** (base key)
- `GEMINI_API_KEY_1` β†’ **Index 1**
- `GEMINI_API_KEY_2` β†’ **Index 2**
- And so on...

#### For Gemini API (Google AI)

```bash
# Linux/Mac
export GEMINI_API_KEY="AIzaSyAbc123..."      # Index 0 (base)
export GEMINI_API_KEY_1="AIzaSyDef456..."    # Index 1
export GEMINI_API_KEY_2="AIzaSyGhi789..."    # Index 2
export GEMINI_API_KEY_3="AIzaSyJkl012..."    # Index 3

# Windows (Command Prompt)
set GEMINI_API_KEY_1=AIzaSyAbc123...
set GEMINI_API_KEY_2=AIzaSyDef456...
set GEMINI_API_KEY_3=AIzaSyGhi789...

# Windows (PowerShell)
$env:GEMINI_API_KEY_1="AIzaSyAbc123..."
$env:GEMINI_API_KEY_2="AIzaSyDef456..."
$env:GEMINI_API_KEY_3="AIzaSyGhi789..."
```

#### For NVIDIA API

```bash
# Linux/Mac
export NVIDIA_API_KEY_1="nvapi-abc123..."
export NVIDIA_API_KEY_2="nvapi-def456..."
export NVIDIA_API_KEY_3="nvapi-ghi789..."

# Windows (Command Prompt)
set NVIDIA_API_KEY_1=nvapi-abc123...
set NVIDIA_API_KEY_2=nvapi-def456...

# Windows (PowerShell)
$env:NVIDIA_API_KEY_1="nvapi-abc123..."
$env:NVIDIA_API_KEY_2="nvapi-def456..."
```

#### For OpenRouter API (Amazon Nova)

```bash
# Linux/Mac
export OPENROUTER_API_KEY_1="sk-or-v1-abc123..."
export OPENROUTER_API_KEY_2="sk-or-v1-def456..."
export OPENROUTER_API_KEY_3="sk-or-v1-ghi789..."

# Windows (Command Prompt)
set OPENROUTER_API_KEY_1=sk-or-v1-abc123...
set OPENROUTER_API_KEY_2=sk-or-v1-def456...

# Windows (PowerShell)
$env:OPENROUTER_API_KEY_1="sk-or-v1-abc123..."
$env:OPENROUTER_API_KEY_2="sk-or-v1-def456..."
```

### Step 2: Using .env File (Recommended - Already Configured!)

βœ… **Good news:** The app already has .env support built-in!

Just create a `.env` file in your project root:

```bash
# .env file
# Gemini API Keys (get from: https://aistudio.google.com/app/apikey)
GEMINI_API_KEY=AIzaSyAbc123...      # Index 0 (base key)
GEMINI_API_KEY_1=AIzaSyDef456...    # Index 1
GEMINI_API_KEY_2=AIzaSyGhi789...    # Index 2

# NVIDIA API Keys (get from: https://build.nvidia.com/)
NVIDIA_API_KEY=nvapi-abc123...      # Index 0 (base key)
NVIDIA_API_KEY_1=nvapi-def456...    # Index 1

# OpenRouter API Keys (get from: https://openrouter.ai/keys)
OPENROUTER_API_KEY=sk-or-v1-abc123...    # Index 0 (base key)
OPENROUTER_API_KEY_1=sk-or-v1-def456...  # Index 1
```

**That's it!** Just run the app normally:

```bash
python3 run.py
```

The .env file is automatically loaded. No extra steps needed!

**Quick Start:**
```bash
# 1. Copy the example file
cp .env.example .env

# 2. Edit .env and add your API keys
nano .env

# 3. Run the app
python3 run.py
```

### Step 3: Verify Keys Are Loaded

Run this to check if your keys are loaded correctly:

```python
python3 -c "
from api_key_manager import get_api_key_manager

manager = get_api_key_manager()
status = manager.get_all_services_status()

for service, info in status.items():
    print(f'{service.upper()}: {info[\"total_keys\"]} key(s) loaded')
"
```

Expected output:
```
NVIDIA: 2 key(s) loaded
GEMINI: 3 key(s) loaded
OPENROUTER: 2 key(s) loaded
```

---

## How It Works

### Automatic Rotation

Once you have multiple keys configured, the system automatically:

1. **Rotates** through them (round-robin)
2. **Fails over** when one key fails
3. **Blocks** keys that fail 3 times (for 5 minutes)
4. **Unblocks** keys after cooldown
5. **Tracks** success rates for each key

### Example Flow

Let's say you have 3 Gemini keys configured:

```
Request 1 β†’ Uses Key 1 βœ“
Request 2 β†’ Uses Key 2 βœ“
Request 3 β†’ Uses Key 3 βœ“
Request 4 β†’ Uses Key 1 βœ“ (rotation back to start)
Request 5 β†’ Uses Key 2 βœ— (fails - rate limit)
Request 6 β†’ Uses Key 3 βœ“ (automatically switched)
Request 7 β†’ Uses Key 1 βœ“
Request 8 β†’ Uses Key 2 βœ— (fails again - 2nd failure)
Request 9 β†’ Uses Key 3 βœ“ (automatically switched)
Request 10 β†’ Uses Key 2 βœ— (fails again - 3rd failure, BLOCKED)
Request 11 β†’ Uses Key 3 βœ“ (Key 2 is skipped)
Request 12 β†’ Uses Key 1 βœ“
Request 13 β†’ Uses Key 3 βœ“ (Key 2 still blocked)
... 5 minutes later ...
Request N β†’ Uses Key 2 βœ“ (unblocked and back in rotation)
```

---

## Getting API Keys

### Gemini API (Google AI)
1. Go to https://aistudio.google.com/app/apikey
2. Click "Create API Key"
3. Copy the key (starts with `AIzaSy...`)
4. **Tip:** Create multiple keys from different Google accounts for more quota

### NVIDIA API
1. Go to https://build.nvidia.com/
2. Sign in and navigate to API Keys
3. Generate a new API key
4. Copy the key (starts with `nvapi-...`)

### OpenRouter API
1. Go to https://openrouter.ai/keys
2. Sign up and create an API key
3. Copy the key (starts with `sk-or-v1-...`)
4. **Tip:** OpenRouter gives free credits for Nova model

---

## Common Scenarios

### Scenario 1: Maximize Free Tier Usage

If you have multiple Google accounts, create one Gemini API key from each:

```bash
export GEMINI_API_KEY="key-from-account-1"      # Index 0
export GEMINI_API_KEY_1="key-from-account-2"    # Index 1
export GEMINI_API_KEY_2="key-from-account-3"    # Index 2
export GEMINI_API_KEY_3="key-from-account-4"    # Index 3
```

This gives you 4x the free tier quota!

### Scenario 2: Paid + Free Keys

Mix paid and free keys:

```bash
export GEMINI_API_KEY="paid-key-with-high-quota"    # Index 0 - tried first
export GEMINI_API_KEY_1="free-key-1"                # Index 1 - backup
export GEMINI_API_KEY_2="free-key-2"                # Index 2 - backup
```

The system will rotate through all of them, maximizing your available quota.

### Scenario 3: Single Key (Backward Compatible)

If you only have one key, the old method still works:

```bash
export GEMINI_API_KEY="your-single-key"
```

The system will use this single key without rotation.

---

## Troubleshooting

### Problem: Keys not being loaded

**Check:**
1. Environment variables are set in the same terminal/session where you run the app
2. Variable names match exactly (case-sensitive)
3. No extra spaces in variable values

**Test:**
```bash
# Linux/Mac
echo $GEMINI_API_KEY_1
echo $GEMINI_API_KEY_2

# Windows (Command Prompt)
echo %GEMINI_API_KEY_1%
echo %GEMINI_API_KEY_2%

# Windows (PowerShell)
echo $env:GEMINI_API_KEY_1
echo $env:GEMINI_API_KEY_2
```

### Problem: Only first key is being used

**Likely cause:** Other keys aren't set properly.

**Fix:** Verify all keys are loaded:
```python
import os
print("Key 1:", os.environ.get('GEMINI_API_KEY_1'))
print("Key 2:", os.environ.get('GEMINI_API_KEY_2'))
print("Key 3:", os.environ.get('GEMINI_API_KEY_3'))
```

### Problem: All keys get blocked quickly

**Causes:**
- Invalid API keys
- Insufficient quota/rate limits
- API service issues

**Fix:**
1. Verify each key works individually
2. Check your quota limits with the API provider
3. Add more keys to distribute the load
4. Increase wait times between requests

---

## Best Practices

βœ… **Use at least 2-3 keys per service** for reliability  
βœ… **Get keys from different accounts** to maximize free tier  
βœ… **Keep backup keys** from different providers if possible  
βœ… **Monitor key usage** to identify which ones work best  
βœ… **Store keys securely** in .env file (add to .gitignore)  
βœ… **Don't commit keys to git** - use environment variables  
βœ… **Rotate keys periodically** for security  

---

## Advanced: Persistent Configuration

To make environment variables persist across reboots:

### Linux/Mac - Add to ~/.bashrc or ~/.zshrc:
```bash
export GEMINI_API_KEY_1="..."
export GEMINI_API_KEY_2="..."
export GEMINI_API_KEY_3="..."
```

Then reload: `source ~/.bashrc`

### Windows - Use System Environment Variables:
1. Search for "Environment Variables" in Start Menu
2. Click "Edit system environment variables"
3. Click "Environment Variables" button
4. Add your keys under "User variables"

---

## Summary

**To use multiple API keys:**

1. **Set numbered environment variables:**
   - `GEMINI_API_KEY_1`, `GEMINI_API_KEY_2`, etc.
   - `NVIDIA_API_KEY_1`, `NVIDIA_API_KEY_2`, etc.
   - `OPENROUTER_API_KEY_1`, `OPENROUTER_API_KEY_2`, etc.

2. **That's it!** The system automatically:
   - Loads all keys
   - Rotates through them
   - Handles failures
   - Maximizes availability

No code changes needed - just set the environment variables and the API Key Manager handles everything!