File size: 6,414 Bytes
5b6e956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Google Gemini API Key Setup

Complete guide to getting and using your free Google Gemini API key.

## Why Do You Need an API Key?

Character Forge uses Google's Gemini 2.5 Flash Image API for AI image generation. You need an API key to:

- Authenticate your requests
- Track usage and costs
- Access the service

## Step 1: Create Your API Key

### Visit Google AI Studio

1. Go to https://aistudio.google.com/app/apikey
2. Sign in with your Google account
3. Accept the terms of service (if prompted)

### Create API Key

1. Click **"Create API Key"**
2. Choose **"Create API key in new project"**
   - Or select an existing Google Cloud project
3. Wait a few seconds for creation
4. Your key will appear (starts with `AIzaSy...`)

### Copy Your Key

1. Click the copy icon next to your key
2. Store it somewhere safe
3. **IMPORTANT**: Never share this key publicly!

## Step 2: Use Your API Key

You have several options:

### Option A: Environment Variable (Recommended for Local)

**Windows (Command Prompt):**
```cmd
set GEMINI_API_KEY=AIzaSy...your_key_here
```

**Windows (PowerShell):**
```powershell
$env:GEMINI_API_KEY="AIzaSy...your_key_here"
```

**Linux/Mac:**
```bash
export GEMINI_API_KEY=AIzaSy...your_key_here
```

**Make it permanent:**

**Windows:**
1. System Properties β†’ Environment Variables
2. Add new user variable
3. Name: `GEMINI_API_KEY`
4. Value: Your key

**Linux/Mac:**
Add to `~/.bashrc` or `~/.zshrc`:
```bash
export GEMINI_API_KEY=AIzaSy...your_key_here
```

### Option B: In the UI

1. Start the app
2. Look in the sidebar
3. Find "Gemini API Key" input
4. Paste your key
5. Continue using the app

**Note**: This is temporary, key won't persist after restart

### Option C: HuggingFace Secrets (For HF Spaces)

1. Go to your Space settings
2. Click "Repository secrets"
3. Add new secret:
   - Name: `GEMINI_API_KEY`
   - Value: Your API key
4. Save

**The app will automatically use this secret**

## Step 3: Verify It Works

### Test in the App

1. Start Character Forge
2. Backend selector should show "Gemini API (Cloud)" with a βœ…
3. Try generating a simple image
4. If it works, you're all set!

### Check Status

In the app:
- Green checkmark (βœ…) = API key is valid
- Red X (❌) = API key missing or invalid
- Yellow warning (⚠️) = API key not tested

## Understanding Costs

### Pricing (as of January 2025)

**Gemini 2.5 Flash Image:**
- ~$0.03 per image generation
- Exact pricing at: https://ai.google.dev/pricing

**Free Tier:**
- 15 requests per minute
- 1,500 requests per day
- Good for testing and personal use

### Cost Examples

| Task | Images | Cost |
|------|--------|------|
| Single image | 1 | ~$0.03 |
| Character sheet | 5 | ~$0.15 |
| Composition | 1-2 | ~$0.03-0.06 |
| 100 images | 100 | ~$3.00 |

### Monitoring Usage

**Check your usage:**
1. Go to https://aistudio.google.com/
2. Click on your project
3. View quotas and usage

**Set up billing alerts:**
1. Go to Google Cloud Console
2. Set up budget alerts
3. Get notified when approaching limits

## Security Best Practices

### βœ… DO

- Store key in environment variables
- Use Secrets for HuggingFace Spaces
- Keep key private and secret
- Rotate key if compromised
- Monitor usage regularly

### ❌ DON'T

- Commit key to Git repositories
- Share key publicly
- Hardcode key in your code
- Upload key in screenshots
- Email key in plain text

### If Key is Compromised

1. Go to https://aistudio.google.com/app/apikey
2. Find your compromised key
3. Click delete/revoke
4. Create a new key
5. Update your applications

## Troubleshooting

### "API Key Not Set"

**Check:**
- Environment variable is set correctly
- Key is spelled exactly right (case-sensitive)
- No extra spaces before/after key
- You've restarted terminal/app after setting

**Try:**
- Set it directly in the UI
- Verify key at https://aistudio.google.com/app/apikey

### "Invalid API Key"

**Causes:**
- Key was revoked
- Key has typo
- Project was deleted
- Billing not set up (for paid features)

**Solution:**
1. Verify key at Google AI Studio
2. Create new key if needed
3. Update your configuration

### "Quota Exceeded"

**Free tier limits:**
- 15 requests/minute
- 1,500 requests/day

**Solutions:**
- Wait for quota to reset
- Reduce generation frequency
- Upgrade to paid tier

**Check quotas:**
https://aistudio.google.com/ β†’ Your project β†’ Quotas

### "Billing Required"

**If you hit free tier limits:**
1. Go to Google Cloud Console
2. Enable billing for your project
3. Set up payment method
4. Set budget alerts

**Don't worry**: You can set spending limits!

## Advanced: Multiple Keys

### For Team Use

Create separate keys for:
- Development
- Testing
- Production
- Different team members

**Benefits:**
- Track usage separately
- Revoke individual keys
- Better security

### Rotating Keys

**Best practice:**
1. Create new key
2. Update applications
3. Test everything works
4. Delete old key

**Do this:**
- Periodically (every 90 days)
- If team members leave
- If key might be compromised

## API Limits Reference

### Rate Limits (Free Tier)

- **Requests per minute**: 15
- **Requests per day**: 1,500
- **Concurrent requests**: 5

### Rate Limits (Paid)

- Higher limits available
- Contact Google for enterprise limits
- Custom quotas possible

### Image Limits

- **Max file size**: 20 MB
- **Max images per request**: 3 (for composition)
- **Supported formats**: PNG, JPEG, WebP

## FAQ

**Q: Is the API key free?**
A: Yes! Free tier includes 1,500 requests/day. Paid tiers available for more.

**Q: How long does the key last?**
A: Indefinitely, until you revoke it. But rotate regularly for security.

**Q: Can I use one key for multiple apps?**
A: Yes, but separate keys are better for tracking and security.

**Q: What if I lose my key?**
A: Create a new one at https://aistudio.google.com/app/apikey

**Q: Can I share my key with my team?**
A: Not recommended. Create separate keys or use service accounts.

**Q: Does the key expire?**
A: No automatic expiration, but revoke and recreate periodically.

**Q: What happens if I go over free tier?**
A: Requests will fail. Set up billing to continue, or wait for reset.

## Getting Help

**Google AI Studio:**
- Docs: https://ai.google.dev/
- Support: https://support.google.com/

**Character Forge:**
- Documentation: See `/docs` folder
- Issues: Report on GitHub

---

**Keep your key safe and happy generating! πŸ”‘**