FREE AI INDEX / 统一产品界面我的收藏提交资源 ↗

Free AI Index / 从零开始Getting started / Google AI Studio / Gemini API

FIRST API CALL / google-ai-studio

Google AI Studio / Gemini:跑通第一次 API 调用Make your first Google AI Studio / Gemini API call

先确认注册与免费条件,再按步骤获取密钥、安装依赖并验证返回结果。教程只使用环境变量读取密钥。Check signup and free terms first, then create a key, install dependencies and validate the response. Examples read credentials from environment variables.

官方文档复核Official docs reviewed: 2026-10-07独立 API 实测Independent API test: 尚未记录Not recorded

注册与网络前提Signup and network requirements

网络 / 地区
仅在 Google AI Studio 官方支持的地区开放;大陆注册与 API 连通状态未实测,先核对官方地区列表。
账号
Google 账号;在 Google AI Studio 的 API Keys 页面创建或查看 API Key。
手机号
官方快速开始文档未将手机号列为统一前置条件;以账号注册页面提示为准。
实名认证
官方快速开始文档未将单独实名认证列为统一前置条件;以账号页面提示为准。
信用卡 / 付款方式
Free Tier 与 Paid Tier 分开;不要绑定付费项目的密钥来验证免费条件。付款要求依账号与地区而定,当前未确认。

这些条件会因账号、地区和产品计划而变化。标注“待核验”表示 FreeLLM 尚未从当前链接的官方资料确认;不要据此推断为不需要。These requirements can vary by account, region and plan. “Unverified” means FreeLLM has not confirmed the condition from the linked official sources; it does not mean the requirement is absent.

获取并安全设置 API KeyGet and safely set your API key

在终端设置密钥Set the key in your terminal

以下命令只为当前终端会话设置环境变量。请在自己的电脑上把尖括号内容替换成真实密钥,不要把密钥发给 FreeLLM 或粘贴到公开代码。These commands set a variable for the current terminal session. Replace the angle-bracket text with your key on your own device; never send it to FreeLLM or commit it to source code.

Windows PowerShellWindows PowerShell

$env:GEMINI_API_KEY = "<在此粘贴本地 API Key>"

macOS / LinuxmacOS / Linux

export GEMINI_API_KEY="<在此粘贴本地 API Key>"

FreeLLM 资源记录FreeLLM resource records

详细操作步骤Detailed operation paths

每条路径都拆成前置条件、步骤、可复制命令和验证动作;免费条件仍以官方页面实时状态为准。curl 命令按 macOS / Linux shell 编写,Windows 用户可使用同页的 Python 或 Node.js 示例。Each path includes prerequisites, steps, copyable commands and a validation action; free terms still follow the provider's live official policy. The curl examples use a macOS/Linux shell; Windows users can run the Python or Node.js examples on this page.

Gemini API 第一次调用

api

EndpointEndpoint
https://generativelanguage.googleapis.com/v1beta
认证Authentication
x-goog-api-key: API Key
额度与限制Limits

官方 Free Tier 按模型与账号项目分别设置限额;具体 RPM、token/minute 与每日限制在 AI Studio Usage / 官方定价页核对。

前置条件Prerequisites

  • Google AI Studio 支持地区内可用的 Google 账号Google AI Studio Google
  • 从 Google AI Studio API Keys 页面获取密钥Google AI Studio API Keys
  • 从当前模型列表确认目标 Gemini 模型及其 Free Tier 状态Gemini Free Tier

操作步骤Steps

  1. 创建或查看 API Key

    登录 Google AI Studio,在 API Keys 页面创建或查看项目密钥。将密钥保存到 GEMINI_API_KEY 环境变量,不要写进源码、截图或公开仓库。

    操作Action:打开 https://aistudio.google.com/app/apikey

  2. 确认模型和免费层级

    先在官方模型与定价页面确认模型是否有 Free Tier,以及账号所在地区能否使用。模型清单和速率限制会变化。

    操作Action:打开 https://ai.google.dev/gemini-api/docs/pricing

  3. 发送 REST 请求

    将模型名替换为当前官方模型页确认的可用 ID。

    可复制命令Copyable command
    export GEMINI_API_KEY="你的 Key"
    curl https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:generateContent -H 'Content-Type: application/json' -H "x-goog-api-key: $GEMINI_API_KEY" -d '{"contents":[{"parts":[{"text":"用一句话解释 API 是什么。"}]}]}'

    预期结果Expected:返回 candidates[0].content.parts 中的文本;到 Google AI Studio Usage 页面检查请求与用量。

  4. Python SDK 示例

    Python 3.9+;SDK 从 GEMINI_API_KEY 环境变量读取密钥。

    可复制命令Copyable command
    python -m pip install -U google-genai
    
    from google import genai
    
    client = genai.Client()
    response = client.models.generate_content(
        model="gemini-3.8-flash",
        contents="用一句话解释 API 是什么。",
    )
    print(response.text)

    预期结果Expected:终端打印模型生成的文本;若模型不可用,先从当前官方模型清单复制模型 ID。

  5. Node.js SDK 示例

    安装官方 Google Gen AI SDK;确保启动终端的环境中已设置 GEMINI_API_KEY。

    可复制命令Copyable command
    npm install @google/genai
    
    import { GoogleGenAI } from "@google/genai";
    
    const ai = new GoogleGenAI({});
    const response = await ai.models.generateContent({
      model: "gemini-3.8-flash",
      contents: "用一句话解释 API 是什么。",
    });
    console.log(response.text);

    预期结果Expected:Node.js 打印模型生成的文本。

验证动作Validation

响应包含 candidates 文本;在 AI Studio Usage 页面确认请求记入预期项目与层级。

常见问题Common issues
  • 400 或 403:检查 API Key、项目权限、模型 ID 与账号地区支持情况。400 403: API Key、 、 ID 。
  • 429:查看 AI Studio Usage 中该模型的速率限制与配额重置时间;降低请求频率后再试。429: AI Studio Usage ; 。
  • 免费层级不用于敏感数据;Google 官方定价说明 Free Tier 内容可能用于改进产品。;Google Free Tier 。

官方来源Official sources