f in x
Gemini API from Scratch: Authentication Setup and First Request — Practical Developer Guide
> cd .. / HUB_EDITORIALE
Analisi dei dati e metriche

Gemini API from Scratch: Authentication Setup and First Request — Practical Developer Guide

[2026-06-01] Author: Ing. Calogero Bono

You have a Google Cloud account, an active project, and the Gemini API enabled. Yet when you try to make your first call, you get 403, 429, or simply can't find the right key to use. It happens to everyone.

We, at Meteora Web, have integrated Gemini into several client projects — chatbots, text analysis, content generation. The first step is always the same: set up authentication correctly. If you get it wrong, you waste time and money. Ready? Let's start from the real problem and get to working code in minutes.

Why Authentication Is the Achilles' Heel

The Gemini API is not a public endpoint. It requires a strictly personal key (API Key) or an OAuth 2.0 service account. For beginners, the fastest route is the API Key. But attention: a key exposed in a public repository or a mobile frontend will be used by others at your expense. We've seen this on security audits for clients: hardcoded keys in JavaScript, ready to be scraped by bots.

Second thing: the API Key is not free. Even though Google offers a free tier (60 requests per minute for Gemini 1.5 Flash, up to 1,000 batch per day), you need a billing method enabled. Without billing, the call returns a 403 with "Billing account not found". Annoying but fixable.

Step 1: Obtain an API Key from Google AI Studio

Connect Your Google Cloud Project

Go to Google AI Studio API Keys. If you don't have a project yet, click Create API Key and select Create new project. You'll be prompted to enable billing. Do it immediately: even if you stay within the free quota, it's mandatory. We recommend creating a dedicated project for Gemini, separate from other Google Cloud services, to track costs.

Generate the Key

Once the project is created, click Create API Key. Copy the string starting with AIza.... Don't close the window without saving it — Google won't show it again. If that happens, you can regenerate it from the same page.

Secure the Key

Never write the key in source code. The most common solution: environment variables. On Linux/macOS: export GEMINI_API_KEY='your_key', on Windows PowerShell: $env:GEMINI_API_KEY='your_key'. In production, use a vault or secret management service (e.g., Google Secret Manager, AWS Secrets Manager). We, in Laravel, use the .env file with GEMINI_API_KEY=... and exclude it from the repository via .gitignore.

Step 2: Choose a Client Library — Python vs cURL

Google provides official SDKs for Python, Node.js, Go, Java. The Python library google-generativeai is the simplest to start with. If you prefer testing via REST, you can use cURL. We'll show both so you also learn the underlying logic.

Install the Python Library

pip install google-generativeai

Inside a virtual environment (recommended):

python -m venv venv
source venv/bin/activate  # on Windows: venv\Scripts\activate
pip install google-generativeai

First Request with Python

import google.generativeai as genai
import os

# Read the key from environment
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
    raise ValueError("Missing environment variable GEMINI_API_KEY")

genai.configure(api_key=api_key)

# Choose the model - here we use gemini-1.5-flash for speed
model = genai.GenerativeModel("gemini-1.5-flash")

# Text response
response = model.generate_content("Explain what an API key is in two lines.")
print(response.text)

If everything works, you'll see a brief explanation. This is your first operational contact with Gemini. If you get a google.api_core.exceptions.PermissionDenied: 403, check:

  • Is the key copied correctly?
  • Is billing enabled on the project?
  • Is the Generative Language API (gemini.googleapis.com) enabled? You can verify from Google Cloud Console → APIs & Services → Library.

First Request with cURL (REST)

Sometimes you want to test directly from the terminal for debugging or automation.

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "parts":[{"text": "How do you say API key in Italian?"}]
    }]
  }'

The endpoint uses generateContent on the specific model. The response will be a JSON with candidates[0].content.parts[0].text. This approach is useful for languages without an official SDK.

Step 3: Handle Errors and Quota Limits

Even with correct authentication, you can hit 429 Resource exhausted if you exceed limits. Google provides per-minute and per-day quotas. In the Cloud Console, you can monitor usage and request increases (paid). We recommend implementing exponential backoff retry on the client side, and logging every error to decide if you need to raise the quota.

Example Python Retry

import time
from google.api_core.exceptions import ResourceExhausted

max_retries = 3
for attempt in range(max_retries):
    try:
        response = model.generate_content("Test")
        break
    except ResourceExhausted:
        if attempt == max_retries - 1:
            raise
        time.sleep(2 ** attempt)  # 1,2,4 seconds

Step 4: OAuth 2.0 Authentication for Server-to-Server Environments

If you're building an application that needs to act on behalf of a service account (e.g., batch generation in cron jobs), the API Key is not enough for security. You need OAuth 2.0 credentials. Go to Google Cloud Console → IAM & Admin → Service Accounts → Create. Download the private key JSON file. Then use the google-auth library to obtain an access token.

from google.oauth2 import service_account

SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
credentials = service_account.Credentials.from_service_account_file(
    "path/to/service-account-key.json", scopes=SCOPES)
genai.configure(credentials=credentials)
# Rest is the same

We use this for automated processes in Laravel: the cron job runs a script with the service account credentials, without exposing the API key in plain text.

In Summary — What to Do Now

  1. Get your key from Google AI Studio, with billing enabled.
  2. Save the key in an environment variable, never in code.
  3. Run your first request with Python or cURL, testing the gemini-1.5-flash model.
  4. Check errors: 403 → authorization, 429 → quota, 404 → wrong model.
  5. For production use OAuth 2.0 with a service account and implement retries.

If you get stuck on a specific step, drop us a line. We, at Meteora Web, deal with these setups every day for our clients. The important thing is that your code talks to Gemini — not that you fight with authentication.

Sponsored Protocol

Ing. Calogero Bono

> AUTHOR_EXTRACTED

Ing. Calogero Bono

Co-founder di Meteora Web. Ingegnere informatico, sviluppo ecosistemi digitali ad alte prestazioni. AI, automazione, SEO tecnica e infrastrutture web. Scrivo di tecnologia per rendere complesso… semplice.

[ Read Full Dossier ]

Hai bisogno di applicare questa strategia?

Esegui il protocollo di contatto per iniziare un progetto con noi.

> INIZIA_PROGETTO

Sponsored

> MW_JOURNAL

> READ_ALL()