Build an AI API with Google Gemini

Wrapping Gemini in an endpoint takes ten lines. Doing it without leaving your API key open to the internet takes a few more — here are both.

Step 1 — Key and model, as variables

Create a key at aistudio.google.com/app/apikey, then set two managed variables:

Step 2 — The endpoint

->json_escape is not optional. The request body is JSON being assembled from user input, so a prompt containing a quote character produces malformed JSON and a 400 from Google — and a prompt built to close the string early could append fields to the payload you never intended to send. The escape runs before substitution, which turns that from a live problem into a non-issue. Any user text going into a JSON body needs it.

Step 3 — Return an answer, not a payload

Gemini's response is deeply nested — candidates, parts, safety ratings, token counts. Useful when you want it, noise when you don't:

Step 4 — Constrain the answer

The cheapest way to control output is to prepend an instruction to every prompt:

Step 5 — Don't ship it open

Here's where the tutorial version stops and the real one starts. What you have so far is a public URL that spends your money. Two things fix it.

Step 6 — Watch what it costs you

LLM endpoints fail differently to normal ones. They rarely go down; they go slow, and the slowness is the model's, not yours. Every request produces a trace showing the Gemini call as its own span, so "is it us or them" is a question you answer by looking rather than guessing. Prometheus metrics per route give you the request rate to compare against your quota before you hit it.

The gotcha that looks like a bug

A 429 with limit: 0 in the error body is not rate limiting. It means the model you asked for isn't on your key's free tier at all — gemini-2.0-flash often isn't. Switch GEMINI_MODEL to gemini-2.5-flash or gemini-flash-latest, or enable billing. Because the model is a managed variable, that's a one-field change with no redeploy.

The shortcut

All three variants ship as one pack — full payload, text-only, and the instruction-prefixed version. Set two variables and deploy.

Related packs