How to use Gemini API in Python

First, this article is writen japanese man that is studing english now. so i think that use wrong english gramer and phrase. i would appreciate your kind understanding.

This article summarize that how to use Google Gemini API.
Let’s get down to the main topic.

Use Gemini API

The steps required to use Gemini API are as follows.

Work Steps
  1. Get API Key
  2. Installing the required libraries
  3. Coding and Excute the code

Step1. Get API Key

Obtain an API key by referring to the official Google Cloud page.

Step2. Installing the required libraries

Build an environment to use Gemini API. I used Flask to use Gemini API.

Summarize how to use Flask as a reminderFirst, this article is writen japanese man that is studing english n...

But you don’t need to use Flask. Create a virtual environment using venv and then you create and .env and then install google-generativeai.

Create a virtual environment.

python -m venv env
source env/Scripts/activate

Create and .env.

touch .env

Install google-generativeai and python-dotenv. Installing python-dotenv is optional.

pip install google-generativeai python-dotenv

Coding and Excute the code

Write following code.

import os
from dotenv import load_dotenv
import google.generativeai as genai

# Loading .env file

# Setting API Key

gemini_pro = genai.GenerativeModel("gemini-pro")
prompt = "Hello World!"
res = gemini_pro.generate_content(prompt)

Write the API key in .env.



Then run the below command.


Error setting environment variables

An error occurred when setting the environment variable “GOOGLE_APPLICATION_CREDENTIALS” when using GCP.

The solution for this error is as follows:

1. Create a GCP service account key(json)

How to create service account key



GOOGLE_APPLICATION_CREDENTIALS=[your GCP service account key’s path]

self-made app using Gemini API

Lastly, I would like to introduce a self-made application using Gemini API.

It’s a simple application using flask. If you’d like, check this app’s source code and please tell me your feedback.