

























Here's how I integrated Google Gemini's free API into a Next.js app - calling it directly from the client with zero backend cost.
I initially used a serverless function (API route) to call Gemini. But on Netlify's free tier, functions have a 10-second timeout. Gemini sometimes takes 5-8 seconds, and with cold starts, it was hitting 502 errors.
Solution: Call the Gemini API directly from the browser. Since I'm using the free tier API key (rate-limited by Google), there's no security concern.
Go to aistudio.google.com. Free tier gives you:
NEXT_PUBLIC_GEMINI_API_KEY=your_key_here
const res = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ parts: [{ text: prompt }] }],
generationConfig: { maxOutputTokens: 2000, temperature: 0.7 },
}),
}
);
const data = await res.json();
const text = data?.candidates?.[0]?.content?.parts?.[0]?.text;
// next.config.ts
const nextConfig = { output: "export" };
No serverless functions needed. Deploy with netlify deploy --prod --dir out.
Source: github.com/x-tahosin/maxai-writer
Questions? Drop them below!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。