# Gemini API

> Use Google’s Gemini models in your app to interpret text or images, such as turning a photo of a menu into a list of dishes.

Web page: https://buildladder.com/tools/gemini-api

Suppose you want to make a printed menu easier to read on a phone. Your app can send a photo and an instruction to Gemini and use the response to build a list with larger text.

Try a clear photo in Google AI Studio first. Ask for dish names and prices and tell it to mark anything unreadable. Compare every price with the photo before using the same request in your app.

- Made by: Google
- Official site: https://ai.google.dev/gemini-api/docs
- Cost: A free tier with limits covers some models. Gemini 4 Argon is paid only. It costs $2 per million input tokens and $10 per million output tokens through November 30, 2026. After that, it costs $4 and $20.
- What you need first: A Google account and an API key, which is a secret code your app uses to call Gemini. Gemini 4 Argon needs the paid tier.
- Who it helps: When your app needs to answer questions, read documents or write text with Google’s models, or you want to start on a free tier.
- When to skip it: If you only want an AI tool to write your code. You need it only when your finished app uses AI itself.
- Updated: 2026-11-09

## Add AI features with the Gemini API

Add one AI feature from your plan, like a summary or a helper chat, using Google’s Gemini API. You can start on the free tier, and your key stays on your app’s server.

What you’ll end up with: One AI feature from your plan, working in your app, with your key kept out of your code and your costs under control.

### You’ll need

- A website or app you started, open in your AI tool.
- PLAN.md from the Plan your idea guide.
- Google account (https://aistudio.google.com): You sign in to Google AI Studio with it

### Your part

- Sign in to Google AI Studio and choose the free or paid tier.
- Create the API key and paste it into your .env file.
- Try the feature and check your usage.

### Step 1 of 5: Sign in and choose free or paid

This step is mine to do. Walk me through it, one action at a time:
- Go to aistudio.google.com and sign in with your Google account.
- Start on the free tier. Google uses what you send on the free tier to improve its products, so test with made-up examples.
- To pay instead, open the Projects page. Find your Free Tier project and click Set up billing in the Billing Tier column.
- Choose or create a billing account, then add your country, contact details and card. You may be asked to prepay at least $5.
- If you pay, leave auto-reload off. Then open the Spend page, find Monthly spend cap, and click Edit spend cap.
- Set up billing before real people use the feature. On the paid tier, Google doesn’t use your requests to improve its products.

It worked when: You’re signed in to Google AI Studio, on the free tier or with a spend cap set.

Before we move on, check with me:
- Does the Projects page show your project and its billing tier?

If something goes wrong:
- You’re offered Postpay instead of Prepay. Google is still rolling Prepay out, so some accounts get Postpay. Either works. Set a spend cap either way.
- The spend cap let you go slightly over. That can happen. Google says the cap can run about 10 minutes behind. Set it a little below your real limit.

### Step 2 of 5: Create a key and save it in .env

This step is mine to do. Walk me through it, one action at a time:
- In Google AI Studio, open the API Keys page and click Create API key.
- Copy the key. Paste it only into your .env file, never into the chat.
- In your product’s folder, open the file named .env. If there isn’t one, ask your AI tool to create an empty one, then open it yourself.
- Add the line GEMINI_API_KEY=your-key, with your real key and no spaces. Save the file.
- Don’t save a version yet. “Add one AI feature” first checks that Git leaves your .env file out.

It worked when: Your .env file holds your key, and it is saved only on your computer.

Before we move on, check with me:
- Does the line start with GEMINI_API_KEY= and have no spaces?

If something goes wrong:
- Your key has an Unrestricted label. Gemini rejects unrestricted keys. Hover over the label, click Add restrictions, and choose Restrict to Gemini API only.
- Your key has a Blocked tag. Google blocks unrestricted keys that sat unused. Create a new key and update the line in your .env file.

### Step 3 of 5: Add one AI feature

Read PLAN.md in this folder. It has my plan and the tools I chose.

I want to add one AI feature that uses the Gemini API. Pick the one feature from my plan that fits best, like a summary or a helper chat. Tell me which one and wait for my OK.

Before you write code:
- Check that .env is listed in .gitignore. If it isn’t, add it. Don’t open or print .env.
- Create .env.example with only the variable name GEMINI_API_KEY and no value.
- Check Google’s current official docs for the right library and a current, low-cost model that has a free tier.

Then build the feature:
- Call Gemini only from server code, like an API route or a server function. Never call it from the browser or the phone app.
- Read the key from the GEMINI_API_KEY environment variable. Never put the key in code, logs or Git.
- Never use a variable name the browser or app can read, such as one starting with NEXT_PUBLIC_ or EXPO_PUBLIC_.
- If my app has no server part yet, suggest the simplest one for my setup and ask me first.
- Cap the answer length with the API’s maximum output setting, so each answer stays short and cheap.

Keep everything that already works.

It worked when: Your app shows an answer from Gemini when you try the feature on your computer.

Before we move on, check with me:
- Does your AI tool say the file that calls the API is server code, not a page or screen?

If something goes wrong:
- It says the API key is missing or invalid. Check that the line in your .env file starts with GEMINI_API_KEY= and has no spaces. Then restart your app so it reads the file again.
- It uses a different key than the one in your .env file. If GOOGLE_API_KEY is also set on your computer, Gemini’s library uses that one first. Remove it, or ask your AI tool to check.
- You get a 429 RESOURCE_EXHAUSTED error on the free tier. You hit the free tier’s limit. Wait a minute and try again. Daily limits reset at midnight Pacific time.

### Step 4 of 5: Handle waiting, errors and limits

My AI feature works. Now make it safe and friendly to use.

Check Google’s current official Gemini API docs for the errors the API can return, including 429 RESOURCE_EXHAUSTED. Then:
- Show a clear loading message while the answer is on its way, and stop people from sending the same request twice.
- Show a short, friendly message when something fails. Keep the real error in the server logs only, and never log the key.
- Give these cases their own plain message: too many requests (a rate limit), out of credits or over my spend cap, and the service being busy.
- Limit how long a person’s input can be, and keep the answer length capped.
- Add a simple limit on how many times one person can use the feature in a short time, so nobody can use up my free limit or run up my bill.

Keep everything that already works. Make only these changes. Then give me a short list of things to try, and what I should see for each one.

It worked when: Your feature shows a loading message and friendly errors, and stops one person from using it too often.

Before we move on, check with me:
- When you send an empty box, is the message helpful?
- When you paste in a very long text, does it stop you politely?

If something goes wrong:
- You can’t make an error happen to test it. Ask your AI tool to show you how to fake each error for a moment on your computer, then undo it.

### Step 5 of 5: Try it and check the cost

This step is mine to do. Walk me through it, one action at a time:
- Use the feature five times with made-up examples that match your plan.
- Make a mistake on purpose, like sending an empty box. Check the message makes sense.
- Ask your AI tool to build the app and check that your key is in no file a visitor’s browser or phone downloads.
- On the free tier, open the Rate Limit page in Google AI Studio to see your limits. On the paid tier, open the Spend page to see what those tries cost.
- Write the cost of one use into PLAN.md. If it’s too high, ask your AI tool for a cheaper model or shorter answers.
- When you put your app online, add the same key in your host’s settings. The .env file doesn’t go online.

It worked when: The feature works with made-up examples, your key stays hidden, and you know your limits or roughly what one use costs.

If something goes wrong:
- A second key didn’t give you more free requests. Limits count per project, not per key. Wait for the limit to reset, or set up billing.
- The feature works on your computer but not online. Your host needs the key too. Add GEMINI_API_KEY in its settings, then put your app online again.

Compare Gemini API with the other options: https://buildladder.com/concepts/ai-features.md
