|
import { GoogleGenerativeAI } from "@google/generative-ai"; |
|
|
|
export default async function handler(req, res) { |
|
|
|
if (req.method !== 'POST') { |
|
return res.status(405).json({ error: 'Method not allowed' }); |
|
} |
|
|
|
|
|
const { apiKey } = req.body; |
|
|
|
if (!apiKey) { |
|
return res.status(400).json({ |
|
valid: false, |
|
error: 'No API key provided' |
|
}); |
|
} |
|
|
|
try { |
|
|
|
const genAI = new GoogleGenerativeAI(apiKey); |
|
|
|
|
|
const model = genAI.getGenerativeModel({ |
|
model: "gemini-1.5-flash", |
|
generationConfig: { |
|
temperature: 0, |
|
maxOutputTokens: 10 |
|
} |
|
}); |
|
|
|
|
|
const prompt = "Respond with 'valid' and nothing else."; |
|
const result = await model.generateContent(prompt); |
|
const response = await result.response; |
|
|
|
|
|
return res.status(200).json({ |
|
valid: true |
|
}); |
|
} catch (error) { |
|
console.error('API key validation error:', error.message); |
|
|
|
|
|
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' |
|
}); |
|
} |
|
|
|
|
|
|
|
return res.status(200).json({ |
|
valid: true, |
|
warning: 'Could not fully validate key due to error: ' + error.message |
|
}); |
|
} |
|
} |