AI agents

Google AI Studio vs the Gemini App vs the Gemini API: Get a Key and Make Your First Request

By 8 min read
A phone, a laptop and a small server joined by glowing lines around a blue Gemini-style star

In short

The problem

The names Gemini, Google AI Studio and Gemini API are used loosely, so people end up in the wrong product. Developers also leak API keys by putting them in front-end code, and then do not know how to read the errors when a first request fails.

What fixed it

Use the Gemini app to chat, Google AI Studio to try prompts and create a key, and the Gemini API to call a model from your own code. Keep the key on the server, read the HTTP status and error body when a request fails, handle rate-limit errors, and check Google's live documentation instead of trusting old numbers.

Gemini, Google AI Studio and the Gemini API are three different things. Gemini is the chat app. Google AI Studio is the developer website where you try prompts and create an API key. The Gemini API is what your own code calls once you have that key. Knowing which one you need saves an afternoon.

The three things, side by side#

NameWhat it isWho it is for
Gemini (the app)Chat assistant in a browser or phone appAnyone who wants answers or help
Google AI StudioWeb tool to test prompts, compare models and create API keysDevelopers and curious builders
Gemini APIThe service your code sends requests toAnyone building it into an app or script

They share the same models underneath. You test a prompt in AI Studio, then reproduce it in code through the API.

A useful way to think about it: the app is a finished product, AI Studio is a workbench, and the API is the pipe your software talks through.

Step 1: get an API key#

  1. Open Google AI Studio and sign in with a Google account.
  2. Go to the API keys section and create a key.
  3. Copy it once and store it somewhere private.

The key is linked to a Google Cloud project. Treat it exactly like a password: anyone with it can use your quota, and on a paid tier, your money.

If a key is ever pasted into a chat, a screenshot, a public repository or a client-side file, assume it is exposed. Delete it in the API keys section, create a new one and update your environment variable.

Step 2: keep the key on the server#

This is the mistake that costs people the most. If you put the key in front-end code, for example in a variable that is bundled into the browser, every visitor can read it.

In Next.js, for instance, a variable whose name starts with NEXT_PUBLIC_ is inlined into the browser bundle. Do not name your key that way. Use a plain name such as GEMINI_API_KEY, read it only in server code (a route handler or a server action), and call the API from there.

Two habits protect you further. Keep your .env.local file out of version control by listing it in .gitignore, and use a different key for local experiments than for anything a customer uses, so you can delete one without breaking the other.

Step 3: your first request#

Google's API also offers an OpenAI-compatible endpoint, which is handy if you already have code written for that format. The request below is the pattern I use in a private tool of mine to check how a model responds and whether it returns valid JSON.

js
// Run with: node --env-file=.env.local first-request.mjs
const res = await fetch(
  'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions',
  {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.GEMINI_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'YOUR_MODEL_NAME', // copy a current name from AI Studio
      temperature: 0,
      response_format: { type: 'json_object' },
      messages: [
        { role: 'system', content: 'Reply with a single JSON object only.' },
        { role: 'user', content: 'Extract skills from: "Engineer. Python, SQL, Docker."' },
      ],
    }),
  }
);
 
const data = await res.json();
console.log(res.status, data.choices?.[0]?.message?.content);

Two notes:

  • Model names change. Copy a current one from AI Studio rather than from an article, including this one.
  • Always check the status. A 200 means success; anything else, read the body.

What a good result looks like#

If everything is set up, the script prints 200 followed by the model's reply. Because the prompt asks for a single JSON object, the reply should be text you can pass to JSON.parse. If it prints something else, do not assume the model is broken. Work through the checks in the next section.

Where to put this in a real app#

Once the script works, move the same call into a route handler or serverless function. Your front end sends the user's input to your own endpoint, your endpoint adds the key and calls the API, and only the result goes back to the browser. Validate and limit what users can send, so a stranger cannot use your key as a free AI service.

What to check first when it fails#

Most first-request failures come from a small set of causes. Work through them in this order.

  1. Read the status code and the body. Print both. The body usually contains an error message that names the problem, which is faster than guessing.
  2. Is the key being read at all? If process.env.GEMINI_API_KEY is empty, the header becomes Bearer undefined. Run the script with the --env-file flag shown above, check the file name and make sure there are no quotes or spaces around the value.
  3. Is the model name valid? A typo or an outdated name is a common reason for a not-found style error. Copy a current name from AI Studio.
  4. Does the URL match the format you are using? The code above uses an OpenAI-compatible path. If you switch to Google's own request format, the URL and the body shape are different, so follow Google's current documentation for that format.
  5. Is the body valid JSON? A stray comma or a missing quote gives a bad-request response. Build the body with JSON.stringify, as above, instead of writing the string by hand.
  6. Did you hit a limit? A 429 means a rate limit, covered in the next step.

What common status codes usually mean#

These are general HTTP meanings. The exact message and cause for your request will be in the response body.

StatusUsually meansFirst thing to try
400The request is malformed or a field is not acceptedCheck the JSON, field names and values against the docs
401 or 403The key is missing, wrong or not allowed to do thisRecheck the key, the header and your project settings
404The URL or the model name was not foundCheck the path and copy the model name from AI Studio
429You sent too many requests or used too muchWait, retry with a delay, reduce calls
500 or 503A problem on the service side, often temporaryRetry after a short wait, and check Google's status information

If the model returns text that is not valid JSON even though you asked for it, log the raw string. Sometimes the reply is wrapped in extra text or cut short, and parsing inside a try and catch block lets your program fail gracefully instead of crashing.

Step 4: handle limits#

Free and paid tiers both have limits on requests per minute and per day. Go over, and the API returns HTTP 429.

A simple pattern:

  1. On a 429, wait a moment and retry.
  2. Cap how many retries you allow.
  3. Cache answers you already paid for or waited for.

Here is a small retry helper you can wrap around the request. It waits a little longer each time and stops after a few attempts.

js
async function withRetry(makeRequest, maxAttempts = 3) {
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    const res = await makeRequest();
    if (res.status !== 429 && res.status < 500) return res;
    if (attempt === maxAttempts) return res;
    await new Promise((r) => setTimeout(r, attempt * 2000));
  }
}

Which models are free and what the limits are changes often, and third-party guides disagree with each other. Check the official Gemini API rate limits page and your own project's limits inside AI Studio before you plan around any figure.

How to check what you are using#

Rather than trusting a number from an article, confirm things in the places that stay current.

  • Model name: open AI Studio and copy the name shown for the model you chose.
  • Limits: read the rate limits page in Google's documentation and the usage information for your own project.
  • Cost: check the current pricing page for the model you plan to use before you build anything that runs at volume.
  • Behaviour: log the status code, the model name you sent and the first part of each response while you are developing, so you can see what changed if something stops working.

Common mistakes#

  • Putting the key in front-end code or a public repository. Anyone can copy it.
  • Copying a model name from an old tutorial. Names change, and the request fails.
  • Planning around a quoted limit. Limits differ by model and tier, and they change. Verify.
  • No retry or error handling. A single temporary failure should not break your app.
  • Sending sensitive data on an unreviewed tier. Read the data terms first.
  • Skipping the prompt test. Try the prompt in AI Studio first, so you know whether the problem is the prompt or the code.

Which one should you actually use?#

Your next step#

Create a key, run the script above with a current model name from AI Studio, and confirm you get a 200. Then move the call behind your own server endpoint and add the retry helper. Environment variables can also fail in surprising ways, as in how one missing env var took down an entire site, so confirm the key is actually loaded before you debug anything else.

If you want an AI model connected to your own site or tools, with the approval steps and key handling done properly, that is covered on the AI automation service page. For the bigger idea behind it, see what agentic AI is.

  • gemini
  • ai-agents
  • nodejs

Common questions

What is the difference between Gemini and Google AI Studio?

Gemini is the chat assistant for everyday use. Google AI Studio is a web tool for developers to try prompts, compare models and create an API key. The Gemini API is what your own code calls. They use the same family of models but serve different jobs.

Is the Gemini API free?

Google offers a free tier with usage limits, and paid tiers beyond it. Which models are free and what the limits are changes often, and third-party guides disagree. Check the rate limits page in Google's own documentation and your project's limits in AI Studio.

Where do I get a Gemini API key?

Sign in to Google AI Studio with a Google account and create an API key from the API keys section. The key is tied to a Google Cloud project. Treat it like a password.

Can I use my API key in front-end JavaScript?

No. Anything in browser code can be read by every visitor. Call the API from your server or a serverless function, and keep the key in an environment variable that is not exposed to the client.

What does a 429 error mean?

You have hit a rate limit, either per minute or per day. Wait and retry with a short delay, reduce how often you call, or move to a paid tier if you need more.

Why is my first Gemini API request failing?

Read the HTTP status code and the error message in the response body first. A 401 or 403 usually points to the key or its permissions, a 404 to a wrong model name or URL, a 400 to a malformed request body, and a 429 to a rate limit. Compare the model name against the current list in AI Studio.

Share this

Post it to LinkedIn

One click opens LinkedIn with a ready-written post: a hook, the short version of this guide and the link. The caption is also copied, so you can paste it if it does not appear.

Post it to Instagram

Instagram has no web share link, so this gives you both pieces: copy the caption, save the card, then post it.

Save the card

Got this problem too?

Automation that takes repetitive work off a team: document pipelines, AI-assisted content, automatic social publishing and scheduled jobs.

see automation and ai work