You can create your API key using Google AI Studio with a single click.
Remember to treat your API key like a password. Don’t accidentally save it in a notebook or source file you later commit to GitHub. In this notebook we will be storing the API key in a .env file. You can also set it as an environment variable or use a secret manager.
Another option is to set the API key as an environment variable. You can do this in your terminal with the following command:
$ export GEMINI_API_KEY="<YOUR_API_KEY>"
Load the API key
To load the API key from the .env file, we will use the dotenv package. This package loads environment variables from a .env file into process.env.
$ npm install dotenv
Then, we can load the API key in our code:
const dotenv =require("dotenv") astypeofimport("dotenv");dotenv.config({ path:"../.env",});const GEMINI_API_KEY =process.env.GEMINI_API_KEY??"";if (!GEMINI_API_KEY) {thrownewError("GEMINI_API_KEY is not set in the environment variables");}console.log("GEMINI_API_KEY is set in the environment variables");
GEMINI_API_KEY is set in the environment variables
Note
In our particular case the .env is is one directory up from the notebook, hence we need to use ../ to go up one directory. If the .env file is in the same directory as the notebook, you can omit it altogether.
With the new SDK, now you only need to initialize a client with you API key (or OAuth if using Vertex AI). The model is now set in each call.
const google =require("@google/genai") astypeofimport("@google/genai");const ai =new google.GoogleGenAI({ apiKey: GEMINI_API_KEY });
Select a model
Now select the model you want to use in this guide, either by selecting one in the list or writing it down. Keep in mind that some models, like the 2.5 ones are thinking models and thus take slightly more time to respond (cf. thinking notebook for more details and in particular learn how to switch the thiking off).
const videoFile =awaitdeferredFileUpload(VIDEO_FILE_PATH, { displayName:"President Reagan's Speech at the Berlin Wall",});
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): PROCESSING
File is still processing, retrying in 5 seconds
current file status (President Reagan's Speech at the Berlin Wall): ACTIVE
File is still processing, retrying in 5 seconds
The uploaded video is ready for processing. This prompt instructs the model to provide basic information about the historical events portrayed in the video.
const SYSTEM_PROMPT =` You are historian who specializes in events caught on film. When you receive a video answer following questions: When did it happen? Who is the most important person in video? How the event is called?`;
Some historic events touch on controversial topics that may get flagged by Gemini API, which blocks the response for the query.
Because of this, it might be a good idea to turn off safety settings.
When did it happen? This event took place on June 12, 1987. President Reagan references President Kennedy’s visit 24 years prior (June 1963) and the 40th anniversary of the Marshall Plan (June 1947), both pointing to 1987.
Who is the most important person in the video? The most important person in the video is Ronald Reagan, who was the President of the United States at the time. He is delivering the address and is the central focus of the recording.
How the event is called? This iconic event is widely known as the “Tear Down This Wall” speech or Reagan’s Brandenburg Gate speech. In it, President Reagan directly challenged Soviet leader Mikhail Gorbachev to dismantle the Berlin Wall.
As you can see, the model correctly provided information about the dates, Ronald Reagan, who was the main subject of the video, and the name of this event.
You can delete the video to prevent unnecessary data storage.