Google's Gemini models are fast, capable and have a free tier for development, which makes them a good first model for an in-app assistant. This guide gets a key, keeps it safe, and calls Gemini from FastAPI, including letting the model call your own Python functions.
1. Create the key in Google AI Studio
- Go to aistudio.google.com/apikey and sign in with a Google account.
- Click Create API key. New accounts get a default Google Cloud project; if you already use Google Cloud, choose Import projects and pick the project that should own the key and its billing.
- Copy the key into your server's environment:
GEMINI_API_KEY=...
Every key belongs to a Google Cloud project, which is where billing, quotas and limits live. Before you launch, read Google's current free-tier limits and data-use terms: Google has treated free-tier and paid-tier prompts differently, so turn on billing before real users' data goes through it.
2. Keep it on the server
Never put the key in frontend code or a mobile app; anyone can read it there and spend your quota. The browser talks to your API, and your API talks to Gemini. That also lets you check who's asking and apply per-user limits.
3. Call Gemini from FastAPI
uv add google-genai
from google import genai
from pydantic import BaseModel
client = genai.Client() # reads GEMINI_API_KEY from the environment
class Ask(BaseModel):
message: str
@app.post("/assistant")
async def assistant(body: Ask, user: CurrentUser):
response = await client.aio.models.generate_content(
model="gemini-2.5-flash",
contents=body.message,
)
return {"reply": response.text}
Use the async client (client.aio) so a slow model call doesn't block other requests. Check Google's models page for the current model names; flash models are the fast, inexpensive default.
4. Let the model call your functions
Tool calling turns a chatbot into an assistant that can look things up. With the SDK, a typed Python function with a docstring becomes a tool:
from google.genai import types
def list_items(limit: int = 10) -> list[dict]:
"""List the signed-in user's most recent items."""
return fetch_items_for_current_user(limit=min(limit, 50))
response = await client.aio.models.generate_content(
model="gemini-2.5-flash",
contents="What did I add this week?",
config=types.GenerateContentConfig(tools=[list_items]),
)
The SDK runs the function when the model asks for it and sends the result back. Two rules keep this safe:
- Tools run as the user. Query with the user's own credentials so the model can never reach other people's data. With Supabase, pass the user's token so row-level security applies. How.
- Tools are read-only or additive. The model acts without asking for confirmation, so don't give it a "delete everything" tool.
5. Put limits on it
AI calls cost money. Count requests per user per day and refuse past a plan's limit with a 429. Store conversations if users expect history, and log token usage so you can see costs per user.
Using it with FastAPI Supabase Starter
The assistant page, saved conversations, tool calling and per-plan daily and monthly limits are built in. Set GEMINI_API_KEY (and optionally GEMINI_MODEL, default gemini-2.5-flash), and the same tools are served over MCP so Claude and Cursor can use your app as the signed-in user. Add a tool by writing a typed function in backend/app/ai/tools.py and appending it to TOOLS. See AI assistant and MCP.
Top comments (0)