Python & Gemini API: Writing Your First Script

If you’ve been using AI through a web interface, you’re only scratching the surface. The real magic happens when you move to code—allowing you to automate repetitive workflows, process data in bulk, and build intelligent features directly into your own toolkits.

Moving forward, you’ll need an API (Application Programming Interface) Key, so that AI calls can be made programmatically. You’ll also need a basic understanding of Python, which you can quickly pick up through this mini Python tutorial.

Now, we will obtain API Key from within Google AI Studio, then set up the Python environment from scratch, write a minimal boilerplate script using the latest google-genai SDK.

Generating Your First API Key

Getting your key takes just a few clicks:

  1. Log into aistudio.google.com with your standard Google account.
  2. Look at the top left corner of Google AI Studio and click the blue “Get API Key” button.
  3. Click “Create API Key”.
  4. Select an existing Google Cloud project or click “Create key in new project”.

A long string of letters and numbers will appear. Copy it immediately and save it securely.

🔒 Developer Security 101: Treat this string exactly like a bank password. Never paste it directly into open text files, public blog comments, or GitHub repositories.

Your First AI Code

Before you move ahead, make sure you have gone through the above referred tutorial and you’ve got Python installed on your system. This tutorial assumes that you are on a Linux based system or WSL in case you have Windows.

Now, let’s write the minimal Python code required to ping the API. We’ll use gemini-3.6-flash, which is the recommended default model for most text generation and multimodal tasks.

Let’s open the terminal and execute following command to create a new folder:

mkdir myapp

Go inside this newly created folder:

cd myapp

Then create virtual environment by executing following command:

python3 -m venv .env

Activate virtual environment:

source .env/bin/activate

(Note: if you are using Windows directly without WSL, then use .env\Scripts\activate command to activate virtual environment)

Install the Google GenAI SDK in order to use the latest models like gemini-3.6-flash;

pip install google-genai

Create a file named first.py and add the following code (remember to replace your_api_key with actual api key that you generated above):

from google import genai

def main():
    # Initialize the client. 
    client = genai.Client(api_key="your_api_key")

    # Define a simple prompt
    prompt = "Explain the concept of Probability Distribution in four sentences."
    
    print("Sending request to Gemini...")
    
    # Generate content using the recommended gemini-3.6-flash model
    response = client.models.generate_content(
        model='gemini-3.6-flash',
        contents=prompt,
    )
    
    # Output the result
    print("\n--- Response ---")
    print(response.text)

if __name__ == "__main__":
    main()

Finally run the script:

python first.py

You should see a concise, cleanly generated response printed directly to your console.

Leave a Comment