Gemini API 빠른 시작

이 빠른 시작에서는 라이브러리 를 설치하고 첫 번째 Gemini API 요청을 만드는 방법을 보여줍니다.

시작하기 전에

Gemini API를 사용하려면 API 키가 필요합니다. 무료로 API 키를 만들어 시작할 수 있습니다.

Gemini API 키 만들기

Google 생성형 AI SDK 설치

Python

Python 3.9+ 이상을 사용하여 다음 pip 명령어를 사용하여 google-genai 패키지를 설치합니다.

pip install -q -U google-genai

JavaScript

Node.js v18+을 사용하여 다음 npm 명령어를 사용하여 TypeScript 및 JavaScript용 Google Gen AI SDK를 설치합니다.

npm install @google/genai

Go

go get 명령어를 사용하여 모듈 디렉터리에 google.golang.org/genai를 설치합니다 :

go get google.golang.org/genai

Java

Maven을 사용하는 경우 종속 항목에 다음을 추가하여 google-genai를 설치할 수 있습니다.

<dependencies>
  <dependency>
    <groupId>com.google.genai</groupId>
    <artifactId>google-genai</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

C#

googleapis/go-genai를 모듈 디렉터리에 dotnet add 명령어를 사용하여 설치합니다.

dotnet add package Google.GenAI

Apps Script

  1. 새 Apps Script 프로젝트를 만들려면 script.new로 이동합니다.
  2. 제목 없는 프로젝트 를 클릭합니다.
  3. Apps Script 프로젝트의 이름을 AI Studio 로 바꾸고 이름 바꾸기 를 클릭합니다.
  4. API 키 설정
    1. 왼쪽에서 프로젝트 설정 프로젝트 설정 아이콘을 클릭합니다.
    2. 스크립트 속성 에서 스크립트 속성 추가 를 클릭합니다.
    3. **속성** 에 키 이름 GEMINI_API_KEY를 입력합니다.
    4. 에 API 키 값을 입력합니다.
    5. 스크립트 속성 저장 을 클릭합니다.
  5. Code.gs 파일 콘텐츠를 다음 코드로 바꿉니다.

첫 번째 요청하기

다음은 generateContent 메서드를 사용하여 Gemini 2.5 Flash 모델을 사용하여 Gemini API에 요청을 보내는 예입니다.

API 키를 환경 변수 GEMINI_API_KEY설정하면 Gemini API 라이브러리를 사용할 때 클라이언트에서 자동으로 선택합니다. 그렇지 않으면 클라이언트를 초기화할 때 API 키를 인수로 전달해야 합니다.

Gemini API 문서의 모든 코드 샘플은 환경 변수 GEMINI_API_KEY를 설정했다고 가정합니다.

Python

from google import genai

# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()

response = client.models.generate_content(
    model="gemini-3-flash-preview", contents="Explain how AI works in a few words"
)
print(response.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-3-flash-preview",
    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()
    // The client gets the API key from the environment variable `GEMINI_API_KEY`.
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    result, err := client.Models.GenerateContent(
        ctx,
        "gemini-3-flash-preview",
        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) {
    // The client gets the API key from the environment variable `GEMINI_API_KEY`.
    Client client = new Client();

    GenerateContentResponse response =
        client.models.generateContent(
            "gemini-3-flash-preview",
            "Explain how AI works in a few words",
            null);

    System.out.println(response.text());
  }
}

C#

using System.Threading.Tasks;
using Google.GenAI;
using Google.GenAI.Types;

public class GenerateContentSimpleText {
  public static async Task main() {
    // The client gets the API key from the environment variable `GEMINI_API_KEY`.
    var client = new Client();
    var response = await client.Models.GenerateContentAsync(
      model: "gemini-3-flash-preview", contents: "Explain how AI works in a few words"
    );
    Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
  }
}

Apps Script

// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
  const payload = {
    contents: [
      {
        parts: [
          { text: 'Explain how AI works in a few words' },
        ],
      },
    ],
  };

  const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent';
  const options = {
    method: 'POST',
    contentType: 'application/json',
    headers: {
      'x-goog-api-key': apiKey,
    },
    payload: JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  const data = JSON.parse(response);
  const content = data['candidates'][0]['content']['parts'][0]['text'];
  console.log(content);
}

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'

다음 단계

이제 첫 번째 API 요청을 했으므로 Gemini가 실제로 작동하는 모습을 보여주는 다음 가이드를 살펴보는 것이 좋습니다.