Current location: Home> Gemini Tutorial> How to get Gemini API key

How to get Gemini API key

Author: LoRA Time:

To use the Gemini API, you need an API key. You can create a key in Google AI Studio in a few clicks.

Get Gemini API Key in Google AI Studio

1. Set API key

For initial testing, you can hardcode the API key, but this should be temporary, as it is not safe to do so. The rest of this section will explain how to set the API key as an environment variable to a different operating system locally.

1. Linux/macOS - Bash

Bash is a common Linux and macOS terminal configuration. You can check if there is a corresponding configuration file by running the following command:

 ~/.bashrc

If the response is "No such file or directory" (no such file or directory), you need to run the following command to create and open this file, or use zsh :

 touch ~/.bashrc
open ~/.bashrc

Next, you need to add the following export command to set the API key:

 export GEMINI_API_KEY=<YOUR_API_KEY_HERE>

After saving the file, apply the changes by running the following command:

 source ~/.bashrc

2. macOS - Zsh

Zsh is a common Linux and macOS terminal configuration. You can check if there is a corresponding configuration file by running the following command:

 ~/.zshrc

If the response is "No such file or directory" (no such file or directory), you need to run the following command to create and open this file, or use bash:

 touch ~/.zshrc
open ~/.zshrc

Next, you need to add the following export command to set the API key:

 export GEMINI_API_KEY=<YOUR_API_KEY_HERE>

After saving the file, apply the changes by running the following command:

 source ~/.zshrc

3. Windows

1. Search for "Environment Variables" in the system settings

2. Modify "User Variable" (applicable to the current user) or "System Variable" (applicable to all users, please use it with caution).

3. Create variables and add export GEMINI_API_KEY=your_key_here

4. Apply changes

2. Send the first Gemini API request

You can verify settings using the curl command:

 curl "https://generatedlanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${GEMINI_API_KEY}" 
   -H 'Content-Type: application/json' 
   -X POST 
   -d '{
     "contents": [{
       "parts":[{"text": "Write a story about a magic backpack."}]
       }]
      }'

3. Ensure the security of API keys

Be sure to make sure your Gemini API key is secure. When using Gemini API keys, please note the following:

1. Google AI Gemini API uses API keys for authorization. If others gain access to your Gemini API key, they can not only use the quota for your project, but may also cause quota churn or incur additional costs (for projects with billing enabled),

2. Adding API key restrictions helps limit the range each API key can use.

3. It is your responsibility to ensure the security of the Gemini API key.

Do not check the Gemini API key into the source control system.

Client applications (Android, Swift, Web, and Dart/Flutter) may compromise API keys. We do not recommend using the Google AI Client SDK in official applications to invoke the Google AI Gemini API directly from mobile and web applications.