您必須具備 API 金鑰,才能使用 Gemini API。您只需在 Google AI Studio 中點按幾下,即可免費建立金鑰。
取得 API 金鑰後,您可以透過下列選項連線至 Gemini API:
在初始測試期間,您可以將 API 金鑰硬式編碼,但這只是暫時做法,因為這不安全。如需 API 金鑰硬式編碼範例,請參閱「明確提供 API 金鑰」一節。
將 API 金鑰設為環境變數
如果您設定環境變數 GEMINI_API_KEY
或 GOOGLE_API_KEY
,用戶端在使用其中一個 Gemini API 程式庫 時,就會自動擷取 API 金鑰。建議您只設定其中一個變數,但如果兩者都設定,則 GOOGLE_API_KEY
會優先採用。
如果您使用的是 REST API 或瀏覽器上的 JavaScript,就必須明確提供 API 金鑰。
以下說明如何在本機設定 API 金鑰,做為不同作業系統的環境變數 GEMINI_API_KEY
。
Linux/macOS - Bash
Bash 是常見的 Linux 和 macOS 終端機設定。您可以執行下列指令,檢查是否有設定檔:
~/.bashrc
如果回應是「No such file or directory」,您必須建立這個檔案,然後執行下列指令或使用 zsh
來開啟檔案:
touch ~/.bashrc
open ~/.bashrc
接下來,您必須新增下列匯出指令,設定 API 金鑰:
export GEMINI_API_KEY=<YOUR_API_KEY_HERE>
儲存檔案後,請執行以下命令套用變更:
source ~/.bashrc
macOS - Zsh
Zsh 是常見的 Linux 和 macOS 終端機設定。您可以執行下列指令,檢查是否有設定檔:
~/.zshrc
如果回應是「No such file or directory」,您必須建立這個檔案,然後執行下列指令或使用 bash
來開啟檔案:
touch ~/.zshrc
open ~/.zshrc
接下來,您必須新增下列匯出指令,設定 API 金鑰:
export GEMINI_API_KEY=<YOUR_API_KEY_HERE>
儲存檔案後,請執行以下命令套用變更:
source ~/.zshrc
Windows
- 在系統設定中搜尋「環境變數」
- 編輯「使用者變數」(適用於目前使用者) 或「系統變數」(適用於所有使用者,請謹慎使用)。
- 建立變數並新增
export GEMINI_API_KEY=your_key_here
- 套用變更
明確提供 API 金鑰
在某些情況下,您可能需要明確提供 API 金鑰。例如:
- 您要執行簡單的 API 呼叫,並且偏好將 API 金鑰硬式編碼。
- 您想要明確控制,而不需要依賴 Gemini API 程式庫自動探索環境變數
- 您使用的環境不支援環境變數 (例如網頁),或是您正在發出 REST 呼叫。
以下是如何明確提供 API 金鑰的範例:
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-2.5-flash", contents="Explain how AI works in a few words"
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
Go
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: "YOUR_API_KEY",
Backend: genai.BackendGeminiAPI,
})
if err != nil {
log.Fatal(err)
}
result, err := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Explain how AI works in a few words"),
nil,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text())
}
Java
package com.example;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
public class GenerateTextFromTextInput {
public static void main(String[] args) {
Client client = Client.builder().apiKey("YOUR_API_KEY").build();
GenerateContentResponse response =
client.models.generateContent(
"gemini-2.5-flash",
"Explain how AI works in a few words",
null);
System.out.println(response.text());
}
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'
妥善保管 API 金鑰
請務必妥善保護 Gemini API 金鑰。使用 Gemini API 金鑰時,請注意下列事項:
Google AI Gemini API 會使用 API 金鑰進行授權。如果其他人取得 Gemini API 金鑰,他們就能使用專案配額提出呼叫,這可能會導致配額耗盡或產生額外費用 (針對已計費的專案),此外還會存取經過調整的模型和檔案。
新增 API 金鑰限制有助於限制可透過每個 API 金鑰使用的表面積。
您有責任妥善保管 Gemini API 金鑰。
- 請勿將 Gemini API 金鑰簽入原始碼控管。
- 用戶端應用程式 (網頁和 Dart/Flutter) 可能會洩漏 API 金鑰。我們不建議在正式版應用程式中使用 Google AI 用戶端 SDK,直接從網頁應用程式呼叫 Google AI Gemini API。
如需瞭解一些一般最佳做法,也可以參閱這篇支援文章。