تجاوز إلى المحتوى الرئيسي

Setting Up Your Gemini Environment 🔑

LOGO DXT

Welcome to the first part of our Gemini tutorial series! Before you can start building amazing things with Gemini, you need to set up your development environment. This guide will walk you through the essential steps: getting your API key and installing the necessary SDKs for Python, JavaScript, Go, and Java.

Step 1: Get Your API Key

Your API key is your personal credential for accessing the Gemini API. It's a critical piece of information that you should always keep secure.

  1. Go to Google AI Studio: Navigate to the Google AI Studio website. This is the primary interface for managing your API keys and interacting with Gemini models.
  2. Create an API Key: In the left-hand navigation menu, select "Get API key". You may be prompted to log in with your Google account.
  3. Generate a Key: Click on "Create API key in new project" or "Create API key" if you already have a project. The system will instantly generate a new, unique key for you.
  4. Copy and Secure Your Key: Immediately copy the key and store it somewhere safe. You will need to use this key in your code to authenticate your requests. Never expose your API key in client-side code (like in a web browser) or check it into a public version control system like GitHub.

Step 2: Install the Gemini SDKs

Once you have your API key, you need to install the SDK for your preferred programming language. The SDK simplifies the process of making API calls by handling the request and response logic for you.

Python

The official Python library for the Gemini API is google-generativeai. You can install it using pip, Python's package installer.

Batch 
pip install -q -U google-generativeai

After installation, you can configure your API key in your code like this:

import os
import google.generativeai as genai
 # Set your API key from an environment variable for security 
 genai.configure(api_key=os.environ.get("GOOGLE_API_KEY")) 
 # Now you're ready to start using the Gemini API!

JavaScript (Node.js)

For Node.js projects, use npm to install the @google/generative-ai package.

npm install @google/generative-ai

You can then set up your API key and initialize the SDK in your code: 

const { GoogleGenerativeAI } = require("@google/generative-ai"); 
// Get your API key from an environment variable 
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY); 
// Now you can start interacting with Gemini

Go

Use the go get command to add the Go SDK to your project.

go get github.com/google/generative-ai-go/genai

Here's how to configure the client in Go:

package main
import (
    "context"
    "fmt"
    "log"
    "os"
    "github.com/google/generative-ai-go/genai"
    "google.golang.org/api/option"
)
func main() {
    ctx := context.Background()
    // Get your API key from an environment variable
    apiKey := os.Getenv("GEMINI_API_KEY")
    if apiKey == "" {
        log.Fatal("GEMINI_API_KEY environment variable not set")
    }
    client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()
    fmt.Println("Gemini client initialized successfully!")
}

Java

If you're using Maven or Gradle, you can add the Java SDK as a dependency.

Maven:

<dependency>
  <groupId>com.google.generativeai</groupId>
  <artifactId>java-genai</artifactId>
  <version>0.0.1</version>
</dependency>

Gradle:

##Groovy:

implementation 'com.google.generativeai:java-genai:0.0.1'

Once installed, you can configure the client in your Java code:

import com.google.cloud.generativeai.v1.GenerativeModelServiceClient;

public class App {
    public static void main(String[] args) {
        try (GenerativeModelServiceClient client = GenerativeModelServiceClient.create()) {
            System.out.println("Gemini client initialized successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Now that your environment is set up, you're ready to start building! In the next tutorial, we'll dive into making your first API call to generate text.