Google AI Studio Adds Import from GitHub to Build a Deployable App

Editor
1 Min Read


// Discouraged: calling the Gemini API from the browser exposes the key
const res = await fetch(
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-goog-api-key": MY_KEY, // visible in the client bundle
    },
    body: JSON.stringify({ contents: [{ parts: [{ text: "Hello" }] }] }),
  }
);

// Recommended: read the key from the server environment, call the API server-side
// GEMINI_API_KEY lives in the server-side runtime, not in shipped client code.
export async function handler(req) {
  const apiKey = process.env.GEMINI_API_KEY; // server-side only
  const r = await fetch(
    "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent",
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "x-goog-api-key": apiKey,
      },
      body: JSON.stringify({ contents: [{ parts: [{ text: "Hello" }] }] }),
    }
  );
  return new Response(await r.text(), { status: r.status });
}
Share this Article
Please enter CoinGecko Free Api Key to get this plugin works.