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.
│
├── .env
└── examples
└── Apollo_11.ipynb
Initialize SDK Client
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).
After the file has been uploaded, you can make client.models.generateContent requests that reference the File API URI. Then you will ask the model to find a few lighthearted moments.
const response =await ai.models.generateContent({ model: MODEL_ID, contents: ["Find four lighthearted moments in this text file.", google.createPartFromUri(textFile.uri??"", textFile.mimeType??"text/plain"), ],});tslab.display.markdown(response.text??"No response text available");
Here are four lighthearted moments from the text:
A Lost Bet Over Coffee:
00 00 54 13 CMP And tell Glenn Parker down at the Cape that he lucked out.
00 00 54 17 CC Understand. Tell Glenn Parker he lucked out.
00 00 54 22 CMP Yes. He lucked out. He doesn't owe me a cup of coffee. This exchange reveals a personal bet between Michael Collins and Glenn Parker, adding a touch of everyday human interaction to the high-stakes mission.
Crew Distracted by the View:
01 03 15 30 CDR Yes, and he is eyeballing the Earth.
01 03 15 32 CMP He's got his head out the window.
01 03 15 35 CC I understand, I had trouble on 12 with him, too. The crew’s playful comments about a fellow astronaut “eyeballing the Earth” and the CAPCOMM’s relatable admission of having “trouble with him, too” highlight the human element of being captivated by the view from space.
A Navy Term for Grayness:
01 03 22 57 LMP Yes. Is there a Navy term for that?
01 03 23 00 CC (Laughing.) A lot of gray paint. Buzz Aldrin asks a humorous, casual question about a “Navy term” for a visual phenomenon, and the CAPCOMM responds with laughter and a witty, simple answer, breaking the technical jargon.
Zero-G Exercise and a TV Request:
01 06 51 33 CMP Ever alert and ... Hey, you got any medics down there watching high grade? I'm trying to do some running in place down here, and I'm wondering just out of curiosity whether it brings my heart rate up.
01 06 52 26 CC I'd like to see that sight. Why don't you give us a TV picture of that one. A crew member playfully asks about their heart rate while “running in place” in zero-g, leading the CAPCOMM to humorously request a TV broadcast of the unusual sight.
Delete File
Files are automatically deleted after 2 days or you can manually delete them using files.delete().