File size: 1,772 Bytes
5f07a23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { GoogleGenerativeAI } from "@google/generative-ai";

export default async function handler(req, res) {
  // Only allow POST requests
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }

  // Extract API key from request
  const { apiKey } = req.body;

  if (!apiKey) {
    return res.status(400).json({ 
      valid: false,
      error: 'No API key provided'
    });
  }

  try {
    // Initialize the Gemini API
    const genAI = new GoogleGenerativeAI(apiKey);
    
    // Use a simple model call with minimal tokens to check key validity
    const model = genAI.getGenerativeModel({
      model: "gemini-1.5-flash",
      generationConfig: {
        temperature: 0,
        maxOutputTokens: 10
      }
    });

    // Make a simple API call to test the key
    const prompt = "Respond with 'valid' and nothing else.";
    const result = await model.generateContent(prompt);
    const response = await result.response;
    
    // If we get here, the key is valid
    return res.status(200).json({
      valid: true
    });
  } catch (error) {
    console.error('API key validation error:', error.message);
    
    // Check if the error is due to an invalid API key
    if (
      error.message?.includes('invalid API key') || 
      error.message?.includes('API key not valid') ||
      error.message?.includes('403')
    ) {
      return res.status(200).json({
        valid: false,
        error: 'Invalid API key'
      });
    }
    
    // For other errors (like network issues), consider the key potentially valid
    // to avoid disrupting users unnecessarily
    return res.status(200).json({
      valid: true,
      warning: 'Could not fully validate key due to error: ' + error.message
    });
  }
}