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:"Wing It! - Full Movie",});
current file status (Wing It! - Full Movie): PROCESSING
File is still processing, retrying in 5 seconds
current file status (Wing It! - Full Movie): PROCESSING
File is still processing, retrying in 5 seconds
current file status (Wing It! - Full Movie): PROCESSING
File is still processing, retrying in 5 seconds
current file status (Wing It! - Full Movie): ACTIVE
File is still processing, retrying in 5 seconds
To demonstrate the video content, display the one of the frames:
The video is now ready to be summarized by the model.
const response =await ai.models.generateContent({ model: MODEL_ID, contents: ["Summarise this video please.", google.createPartFromUri(videoFile.uri??"", videoFile.mimeType??"video/webm"), ], config: { systemInstruction:"You should provide a quick 2 or 3 sentence summary of what is happening in the video." },});tslab.display.markdown(response.text??"No response text available.");
This animated short features a cat meticulously building an airplane in a barn, only for his boisterous dog companion to eagerly commandeer the controls. The dog’s chaotic attempts to fly cause the plane to fall apart mid-air, but the resourceful cat manages to reassemble it into a makeshift flying machine. After successfully soaring through the sky, they hilariously crash back into the barn, completing their “pet project.”
The model correctly describes the plot of the short movie.
Now, you can delete the no longer necessary uploaded file.
Gemini API takes only one frame per second of the video. It may cause models not to see everything that is happening, especially if something is visible only for a fraction of a second.
Summary
Now you know how you can use Gemini models to summarize what is happening in videos.
This notebook shows only one of many use cases. Check the Video understanding notebook for more examples of using the Gemini API with videos.