Gemini API key setup: auth keys, restrictions and where the key lives

Create a Gemini API key in Google AI Studio, keep it on your server as GEMINI_API_KEY, restrict it to the Gemini API, and avoid the key changes of 2026.

Simon, Founder of Shipyard

By · Founder of Shipyard

4 minute read

The short version

Create the key in Google AI Studio on the API keys page, store it on your host as GEMINI_API_KEY, and call Gemini only from server code. The official @google/genai SDK reads that variable by itself, so the key never needs to appear in your code. Then make sure the key is restricted to the Gemini API, because Google now rejects requests from keys that are not, and keep an eye on billing alerts in the Google Cloud console, since a leaked Gemini key bills the project it belongs to.

That is the whole job. The rest of this page explains each step, because Gemini keys work differently from Anthropic or OpenAI keys in one important way: every Gemini key belongs to a Google Cloud project, and in 2026 Google changed what kind of key the Gemini API accepts.

Every key belongs to a Google Cloud project

Google's documentation says it plainly: every Gemini API key is associated with a Google Cloud project, and the project is where billing, collaborators and permissions live. If you have never used Google Cloud, accepting the AI Studio terms creates a default project and a key for you. If you already have Google Cloud projects, they do not appear in AI Studio until you import them from the Projects page on the dashboard.

This matters for two reasons. Usage and charges are counted per project, so one project per product keeps the bills and limits apart, the same way you would give each product its own Anthropic or OpenAI key. And anyone with editor access to the project can create keys in it, so check who has access before you put a card on it. AI Studio lets you hold at most 10 projects at a time, which is plenty for a handful of products.

Auth keys, standard keys and the 2026 changes

Gemini now has two kinds of key. A standard key ties requests to a project for billing and quota. An auth key is bound to a Google Cloud service account. According to Google's key documentation, every new key created in AI Studio since 28 May 2026 is an auth key, so if you create a key today you get the newer kind without doing anything.

Older keys are where people get caught. Google has said the Gemini API rejects requests from unrestricted standard keys, and since 7 May 2026 it blocks unrestricted keys that sat unused for a while, which then show a Blocked tag in AI Studio. If an app that worked last year suddenly fails with an authentication error and nobody touched the code, an old key is the first thing to check. The fix is to create a fresh key in AI Studio and swap it in, or to restrict the old one to the Gemini API.

Create and restrict the key

Call it from the server

Install @google/genai and create a client with no arguments. The Gemini client libraries look for GEMINI_API_KEY or GOOGLE_API_KEY in the environment and use whichever they find. If both are set, GOOGLE_API_KEY wins, which is a common source of confusion when an old Google variable is left on the host and the app quietly uses a different key from the one you just created. Set one name only.

Keep the call in a server route behind a signed-in user and a per-user rate limit. Google's guidance is explicit that keys must not be hard-coded into web or mobile apps, and that browser or mobile clients should go through a backend that makes the actual call. A route like the one below is that backend. Read the model name from a variable too, so moving to a newer or cheaper model is a settings change rather than a deploy.

TypeScript
// app/api/ask/route.ts
import { GoogleGenAI } from '@google/genai'

// Reads GEMINI_API_KEY from the environment by itself.
const ai = new GoogleGenAI({})

export async function POST(req: Request) {
  // Check the session and a per-user rate limit here.
  const { question } = await req.json()
  const result = await ai.models.generateContent({
    model: process.env.GEMINI_MODEL!, // set on your host
    contents: question,
  })
  return Response.json({ text: result.text ?? '' })
}

If the key leaks

Google's order of steps is the right one: create a new key, deploy it to your host, then disable the old key in the Cloud console, and only then look at billing and usage to see what the leaked key did. Doing it in that order means your app keeps working while you close the hole. Because the key bills a project rather than a prepaid balance, a leak can cost more than an Anthropic or OpenRouter key with a credit limit would, which is the honest weakness of Gemini here. Billing alerts tell you about a spike; they are not a cap.

What Shipyard does here

Shipyard's AI step currently writes Anthropic, OpenAI or OpenRouter keys into your host, not Gemini keys. If you want Gemini today, two routes work. Add GEMINI_API_KEY to your host's variables yourself after the launch, using the steps above. Or choose OpenRouter as the provider, which gives you Gemini models behind one key with a per-key credit limit, and Shipyard writes that key for you on the server side only.

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

Get started See how it works

Read next