-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-gemini.js
More file actions
71 lines (55 loc) · 2.19 KB
/
test-gemini.js
File metadata and controls
71 lines (55 loc) · 2.19 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
#!/usr/bin/env node
/**
* Test script to verify GEMINI_API_KEY works correctly
* Usage: node test-gemini.js
* Or with custom key: GEMINI_API_KEY=your_key node test-gemini.js
*/
const { GoogleGenAI } = require("@google/genai");
require("dotenv").config({ path: ".env.local" });
async function testGeminiAPI() {
console.log("🔍 Testing Gemini API...\n");
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.error("❌ ERROR: GEMINI_API_KEY not found!");
console.log("\nPlease set it in .env.local file:");
console.log("GEMINI_API_KEY=your_api_key_here");
process.exit(1);
}
console.log(`✅ API Key found: ${apiKey.slice(0, 10)}...${apiKey.slice(-4)}`);
console.log("");
try {
const ai = new GoogleGenAI({ apiKey });
console.log("📡 Sending test request to Gemini...\n");
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{
role: "user",
parts: [{ text: "Say 'Hello! Gemini API is working!' in exactly those words." }],
},
],
});
const text = response.text;
console.log("✅ SUCCESS! Gemini responded:");
console.log("─".repeat(40));
console.log(text);
console.log("─".repeat(40));
console.log("\n🎉 Your GEMINI_API_KEY is working correctly!\n");
} catch (error) {
console.error("❌ ERROR: API call failed!\n");
if (error.message?.includes("429") || error.message?.includes("quota")) {
console.error("🚫 Quota exceeded - Your free tier limit has been reached.");
console.log(" Wait a few minutes or create a new API key.");
} else if (error.message?.includes("401") || error.message?.includes("403")) {
console.error("🔐 Authentication failed - Invalid API key.");
console.log(" Please check your GEMINI_API_KEY in .env.local");
} else if (error.message?.includes("fetch failed") || error.message?.includes("ECONNREFUSED")) {
console.error("🌐 Network error - Cannot connect to Gemini API.");
console.log(" Check your internet connection or VPN.");
} else {
console.error("Error details:", error.message);
}
process.exit(1);
}
}
testGeminiAPI();