Repository navigation
Expand file tree
/
Copy pathsetup-api-key.html
More file actions
809 lines (691 loc) · 26.6 KB
/
Copy pathsetup-api-key.html
File metadata and controls
809 lines (691 loc) · 26.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🔑 API Key Setup - Alphabet Badge Bot</title>
<!-- Favicon -->
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🔑</text></svg>">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Fredoka+One:wght@400&family=Open+Sans:wght@300;400;600;700&display=swap" rel="stylesheet">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
:root {
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--primary-color: #667eea;
--secondary-color: #764ba2;
--success-color: #51cf66;
--warning-color: #ffd43b;
--danger-color: #ff6b6b;
--info-color: #339af0;
--white: #ffffff;
--gray-50: #f8fafc;
--gray-100: #f1f5f9;
--gray-200: #e2e8f0;
--gray-300: #cbd5e1;
--gray-400: #94a3b8;
--gray-500: #64748b;
--gray-600: #475569;
--gray-700: #334155;
--gray-800: #1e293b;
--gray-900: #0f172a;
--font-heading: 'Fredoka One', cursive;
--font-body: 'Open Sans', sans-serif;
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
--shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-body);
line-height: 1.6;
color: var(--gray-800);
background: var(--gray-50);
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}
.header {
text-align: center;
margin-bottom: 3rem;
}
.header h1 {
font-family: var(--font-heading);
font-size: 2.5rem;
background: var(--primary-gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.5rem;
}
.header p {
color: var(--gray-600);
font-size: 1.125rem;
}
.nav-links {
margin-top: 1rem;
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
}
.nav-link {
color: var(--primary-color);
text-decoration: none;
font-weight: 500;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
transition: background-color 0.3s ease;
}
.nav-link:hover {
background: var(--gray-100);
}
.setup-card {
background: var(--white);
border-radius: 1rem;
padding: 2rem;
box-shadow: var(--shadow-lg);
margin-bottom: 2rem;
}
.setup-card h2 {
font-family: var(--font-heading);
color: var(--gray-800);
margin-bottom: 1rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.step-number {
background: var(--primary-gradient);
color: var(--white);
width: 2rem;
height: 2rem;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 0.875rem;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
display: block;
font-weight: 600;
color: var(--gray-700);
margin-bottom: 0.5rem;
}
.form-group input {
width: 100%;
padding: 0.75rem;
border: 1px solid var(--gray-300);
border-radius: 0.5rem;
font-size: 1rem;
transition: border-color 0.3s ease;
}
.form-group input:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.input-group {
display: flex;
gap: 0.5rem;
}
.input-group input {
flex: 1;
}
.btn {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 0.5rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
font-size: 1rem;
}
.btn-primary {
background: var(--primary-gradient);
color: var(--white);
}
.btn-primary:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: var(--shadow-lg);
}
.btn-secondary {
background: var(--gray-200);
color: var(--gray-700);
}
.btn-secondary:hover {
background: var(--gray-300);
}
.btn-success {
background: var(--success-color);
color: var(--white);
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none !important;
}
.alert {
padding: 1rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
display: flex;
align-items: flex-start;
gap: 0.5rem;
}
.alert-success {
background: rgba(81, 207, 102, 0.1);
color: var(--success-color);
border: 1px solid rgba(81, 207, 102, 0.2);
}
.alert-error {
background: rgba(255, 107, 107, 0.1);
color: var(--danger-color);
border: 1px solid rgba(255, 107, 107, 0.2);
}
.alert-warning {
background: rgba(255, 212, 59, 0.1);
color: #d69e2e;
border: 1px solid rgba(255, 212, 59, 0.2);
}
.alert-info {
background: rgba(51, 154, 240, 0.1);
color: var(--info-color);
border: 1px solid rgba(51, 154, 240, 0.2);
}
.instructions {
background: var(--gray-100);
border-radius: 0.5rem;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.instructions h3 {
color: var(--gray-800);
margin-bottom: 1rem;
font-size: 1.125rem;
}
.instructions ol {
padding-left: 1.5rem;
}
.instructions li {
margin-bottom: 0.5rem;
color: var(--gray-700);
}
.instructions a {
color: var(--primary-color);
text-decoration: none;
font-weight: 500;
}
.instructions a:hover {
text-decoration: underline;
}
.code-block {
background: var(--gray-900);
color: var(--gray-100);
padding: 1rem;
border-radius: 0.5rem;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.875rem;
overflow-x: auto;
margin: 1rem 0;
}
.status-indicator {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
font-weight: 500;
font-size: 0.875rem;
}
.status-checking {
background: rgba(51, 154, 240, 0.1);
color: var(--info-color);
}
.status-valid {
background: rgba(81, 207, 102, 0.1);
color: var(--success-color);
}
.status-invalid {
background: rgba(255, 107, 107, 0.1);
color: var(--danger-color);
}
.loading-spinner {
display: inline-block;
width: 16px;
height: 16px;
border: 2px solid rgba(51, 154, 240, 0.3);
border-radius: 50%;
border-top-color: var(--info-color);
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.feature-list {
list-style: none;
padding: 0;
}
.feature-list li {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.5rem;
color: var(--gray-700);
}
.feature-list i {
color: var(--success-color);
width: 1rem;
}
.troubleshooting {
background: var(--gray-50);
border-radius: 0.5rem;
padding: 1.5rem;
margin-top: 2rem;
}
.troubleshooting h3 {
color: var(--gray-800);
margin-bottom: 1rem;
}
.troubleshooting details {
margin-bottom: 1rem;
}
.troubleshooting summary {
cursor: pointer;
font-weight: 500;
color: var(--gray-700);
padding: 0.5rem;
border-radius: 0.25rem;
transition: background-color 0.3s ease;
}
.troubleshooting summary:hover {
background: var(--gray-100);
}
.troubleshooting details[open] summary {
margin-bottom: 0.5rem;
}
@media (max-width: 768px) {
.container {
padding: 1rem;
}
.header h1 {
font-size: 2rem;
}
.nav-links {
flex-direction: column;
align-items: center;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔑 API Key Setup</h1>
<p>Configure Google AI Studio for AI-generated badges</p>
<div class="nav-links">
<a href="/" class="nav-link">
<i class="fas fa-home"></i> Main App
</a>
<a href="demo.html" class="nav-link">
<i class="fas fa-play"></i> Demo
</a>
<a href="test-api.html" class="nav-link">
<i class="fas fa-flask"></i> Test API
</a>
<a href="test-image-display.html" class="nav-link">
<i class="fas fa-image"></i> Image Test
</a>
</div>
</div>
<!-- Step 1: Get API Key -->
<div class="setup-card">
<h2>
<span class="step-number">1</span>
Get Your Google AI Studio API Key
</h2>
<div class="alert alert-info">
<i class="fas fa-info-circle"></i>
<div>
<strong>Free API Access:</strong> Google AI Studio provides free access to their AI models with generous usage limits, perfect for learning and personal projects.
</div>
</div>
<div class="instructions">
<h3>📋 Step-by-Step Instructions:</h3>
<ol>
<li>Visit <a href="https://aistudio.google.com/" target="_blank" rel="noopener">Google AI Studio</a></li>
<li>Sign in with your Google account</li>
<li>Click on "Get API Key" in the top navigation</li>
<li>Create a new API key or use an existing one</li>
<li>Copy the API key (it starts with "AIza...")</li>
<li>Paste it in the form below</li>
</ol>
</div>
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle"></i>
<div>
<strong>Keep it secure:</strong> Your API key is stored locally in your browser and never sent to our servers. Only use it on trusted devices.
</div>
</div>
</div>
<!-- Step 2: Configure API Key -->
<div class="setup-card">
<h2>
<span class="step-number">2</span>
Configure Your API Key
</h2>
<div class="form-group">
<label for="api-key-input">Google AI Studio API Key</label>
<div class="input-group">
<input type="password" id="api-key-input" placeholder="AIza..." maxlength="100">
<button id="toggle-visibility" class="btn btn-secondary" type="button">
<i class="fas fa-eye"></i>
</button>
</div>
</div>
<div id="api-status" style="display: none;"></div>
<div class="form-group">
<button id="save-key" class="btn btn-primary">
<i class="fas fa-save"></i>
Save API Key
</button>
<button id="test-key" class="btn btn-secondary">
<i class="fas fa-flask"></i>
Test API Key
</button>
<button id="clear-key" class="btn btn-secondary">
<i class="fas fa-trash"></i>
Clear Key
</button>
</div>
</div>
<!-- Step 3: Enable Features -->
<div class="setup-card">
<h2>
<span class="step-number">3</span>
Enable AI Features
</h2>
<div class="alert alert-success" id="success-message" style="display: none;">
<i class="fas fa-check-circle"></i>
<div>
<strong>Setup Complete!</strong> AI badge generation is now enabled. You can start learning with AI-generated badges!
</div>
</div>
<p>Once your API key is configured, you'll unlock these amazing features:</p>
<ul class="feature-list">
<li><i class="fas fa-magic"></i> AI-generated unique badges for each letter</li>
<li><i class="fas fa-palette"></i> Personalized artwork based on your words</li>
<li><i class="fas fa-sparkles"></i> Beautiful, colorful designs that motivate learning</li>
<li><i class="fas fa-child"></i> Child-friendly, educational imagery</li>
<li><i class="fas fa-shield-alt"></i> Safe content with built-in filters</li>
</ul>
<div class="form-group" style="margin-top: 2rem;">
<a href="/" class="btn btn-success">
<i class="fas fa-rocket"></i>
Start Learning with AI Badges!
</a>
<a href="test-api.html" class="btn btn-secondary">
<i class="fas fa-cog"></i>
Advanced Testing
</a>
</div>
</div>
<!-- Troubleshooting -->
<div class="troubleshooting">
<h3>🛠️ Troubleshooting</h3>
<details>
<summary>My API key isn't working</summary>
<p>Make sure your API key:</p>
<ul>
<li>Starts with "AIza" and is about 39 characters long</li>
<li>Has the Imagen API enabled in Google AI Studio</li>
<li>Hasn't exceeded its usage limits</li>
<li>Is copied correctly without extra spaces</li>
</ul>
</details>
<details>
<summary>I can't access Google AI Studio</summary>
<p>Google AI Studio might not be available in all regions. If you can't access it:</p>
<ul>
<li>Try using a VPN to access from a supported region</li>
<li>Use the app without AI features (fallback badges still work great!)</li>
<li>Check Google's availability documentation</li>
</ul>
</details>
<details>
<summary>The app works without an API key, right?</summary>
<p>Absolutely! The Alphabet Badge Bot works perfectly without an API key:</p>
<ul>
<li>Beautiful fallback badges are generated locally</li>
<li>All learning features remain fully functional</li>
<li>Progress tracking and badge collection work normally</li>
<li>You can add an API key later to upgrade to AI badges</li>
</ul>
</details>
<details>
<summary>Is my API key secure?</summary>
<p>Yes, your security is our priority:</p>
<ul>
<li>API keys are stored only in your browser's local storage</li>
<li>Keys are never sent to our servers or third parties</li>
<li>All API calls go directly from your browser to Google</li>
<li>You can clear your key anytime using the button above</li>
</ul>
</details>
<details>
<summary>How much does the API cost?</summary>
<p>Google AI Studio offers generous free usage:</p>
<ul>
<li>Free tier includes many image generations per month</li>
<li>Perfect for personal learning and family use</li>
<li>Check Google AI Studio for current pricing and limits</li>
<li>You'll be notified if you approach any limits</li>
</ul>
</details>
</div>
</div>
<script>
class APIKeySetup {
constructor() {
this.apiKey = '';
this.proxyUrl = '/api';
this.init();
}
init() {
this.setupEventListeners();
this.loadExistingKey();
}
setupEventListeners() {
document.getElementById('toggle-visibility').addEventListener('click', () => {
this.togglePasswordVisibility();
});
document.getElementById('save-key').addEventListener('click', () => {
this.saveApiKey();
});
document.getElementById('test-key').addEventListener('click', () => {
this.testApiKey();
});
document.getElementById('clear-key').addEventListener('click', () => {
this.clearApiKey();
});
document.getElementById('api-key-input').addEventListener('input', () => {
this.hideStatus();
});
document.getElementById('api-key-input').addEventListener('paste', () => {
setTimeout(() => this.validateKeyFormat(), 100);
});
}
togglePasswordVisibility() {
const input = document.getElementById('api-key-input');
const button = document.getElementById('toggle-visibility');
const icon = button.querySelector('i');
if (input.type === 'password') {
input.type = 'text';
icon.className = 'fas fa-eye-slash';
} else {
input.type = 'password';
icon.className = 'fas fa-eye';
}
}
loadExistingKey() {
try {
const savedKey = localStorage.getItem('alphabetBotApiKey');
if (savedKey) {
document.getElementById('api-key-input').value = savedKey;
this.showStatus('Existing API key loaded', 'valid');
this.showSuccessMessage();
}
} catch (error) {
console.error('Failed to load existing key:', error);
}
}
validateKeyFormat() {
const input = document.getElementById('api-key-input');
const key = input.value.trim();
if (!key) {
this.hideStatus();
return false;
}
if (!key.startsWith('AIza')) {
this.showStatus('API key should start with "AIza"', 'invalid');
return false;
}
if (key.length < 35 || key.length > 45) {
this.showStatus('API key length seems incorrect', 'invalid');
return false;
}
this.showStatus('API key format looks correct', 'valid');
return true;
}
async saveApiKey() {
const input = document.getElementById('api-key-input');
const key = input.value.trim();
if (!key) {
this.showStatus('Please enter an API key', 'invalid');
return;
}
if (!this.validateKeyFormat()) {
return;
}
try {
localStorage.setItem('alphabetBotApiKey', key);
this.apiKey = key;
this.showStatus('API key saved successfully!', 'valid');
this.showSuccessMessage();
// Also save to main app settings if available
try {
const settings = JSON.parse(localStorage.getItem('alphabetBotSettings') || '{}');
settings.apiKey = key;
settings.useRealAI = true;
localStorage.setItem('alphabetBotSettings', JSON.stringify(settings));
} catch (e) {
// Settings not available, that's okay
}
} catch (error) {
this.showStatus('Failed to save API key', 'invalid');
console.error('Save error:', error);
}
}
async testApiKey() {
const input = document.getElementById('api-key-input');
const key = input.value.trim();
if (!key) {
this.showStatus('Please enter an API key to test', 'invalid');
return;
}
this.showStatus('Testing API key...', 'checking');
try {
const response = await fetch(`${this.proxyUrl}/generate-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Test image: a simple colorful circle',
apiKey: key
})
});
const data = await response.json();
if (response.ok && data.success) {
this.showStatus('API key is working perfectly!', 'valid');
this.apiKey = key;
} else {
this.showStatus(`API test failed: ${data.error || 'Unknown error'}`, 'invalid');
}
} catch (error) {
if (error.message.includes('Failed to fetch')) {
this.showStatus('Cannot connect to proxy server. Make sure it\'s running.', 'invalid');
} else {
this.showStatus(`Test failed: ${error.message}`, 'invalid');
}
console.error('Test error:', error);
}
}
clearApiKey() {
if (confirm('Are you sure you want to clear the saved API key?')) {
try {
localStorage.removeItem('alphabetBotApiKey');
// Also clear from main app settings
try {
const settings = JSON.parse(localStorage.getItem('alphabetBotSettings') || '{}');
settings.apiKey = '';
settings.useRealAI = false;
localStorage.setItem('alphabetBotSettings', JSON.stringify(settings));
} catch (e) {
// Settings not available, that's okay
}
document.getElementById('api-key-input').value = '';
this.showStatus('API key cleared', 'valid');
this.hideSuccessMessage();
this.apiKey = '';
} catch (error) {
this.showStatus('Failed to clear API key', 'invalid');
console.error('Clear error:', error);
}
}
}
showStatus(message, type) {
const statusEl = document.getElementById('api-status');
const icons = {
checking: '<span class="loading-spinner"></span>',
valid: '<i class="fas fa-check-circle"></i>',
invalid: '<i class="fas fa-exclamation-circle"></i>'
};
statusEl.innerHTML = `${icons[type]} ${message}`;
statusEl.className = `status-indicator status-${type}`;
statusEl.style.display = 'flex';
}
hideStatus() {
const statusEl = document.getElementById('api-status');
statusEl.style.display = 'none';
}
showSuccessMessage() {
const messageEl = document.getElementById('success-message');
messageEl.style.display = 'flex';
}
hideSuccessMessage() {
const messageEl = document.getElementById('success-message');
messageEl.style.display = 'none';
}
}
// Initialize the setup when the page loads
document.addEventListener('DOMContentLoaded', () => {
window.apiKeySetup = new APIKeySetup();
});
</script>
</body>
</html>