# KeyError using Imagen 3 API via Python

**URL:** <https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001>\
**Category:** Gemini API\
**Tags:** python, gemini-15, gemini-api, api\
**Created:** [October 21, 2024, 5:31pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001 "2024-10-21T17:31:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurs](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/gurs/32/22999_2.png) [@gurs](https://discuss.ai.google.dev/u/gurs)\
**Post date:** [October 21, 2024, 5:31pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001/1 "2024-10-21T17:31:19Z")

</div>

I am following this tutorial to generate images via API with Imagen 3: [Generate images using Imagen 3 | Gemini API | Google AI for Developers](https://ai.google.dev/gemini-api/docs/imagen). I am using a clean Python 3.12.7 install via PowerShell 7.4.5 on a Win11 machine. The python install includes pip, pillow, git and winget (among others). My company has access to Imagen 3 which I have confirmed by generating images using it in Vertex AI Studio.

I am able to successfully follow the tutorial (thanks to help from @[afirstenberg](https://discuss.ai.google.dev/u/afirstenberg)) until I get to this line:

```auto
genai.configure(api_key=os.environ['API_KEY'])

```

That line results in an error, which I have searched but can’t figure out how to resolve:

```auto
>>> genai.configure(api_key=os.environ['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen os>", line 714, in __getitem__
KeyError: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>>

```

I have confirmed that my API key is active, and even generated a new one to be sure using Google AI Studio.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![afirstenberg](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/afirstenberg/32/202_2.png) [@afirstenberg](https://discuss.ai.google.dev/u/afirstenberg)\
**Post date:** [October 21, 2024, 6:32pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001/2 "2024-10-21T18:32:55Z")

</div>

In this case, you should leave `API_KEY` exactly as it is.  
This line reads the API Key from the “API\_KEY” environment variable, as suggested in an earlier step of the tutorial:

> This tutorial assumes that you’re accessing your API key as an environment variable.

Since you’re using PowerShell, you would set the environment variable with the key using something like this:

```powershell
$Env:API_KEY = "XXXXX-your-key-here-XXXXX"

```

in the shell _before_ you try to run your python script.

---

<div class="post-metadata">

**Author:** ![afirstenberg](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/afirstenberg/32/202_2.png) [@afirstenberg](https://discuss.ai.google.dev/u/afirstenberg)\
**Post date:** [October 21, 2024, 6:34pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001/3 "2024-10-21T18:34:53Z")

</div>

(And, for the record, it was @OrangiaNebula that pointed you in the right direction about which version of python to use. I was just warning other folks that came along that Imagen 3 on AI Studio is currently in private preview.)

---

<div class="post-metadata">

**Author:** ![OrangiaNebula](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/orangianebula/32/480_2.png) [@OrangiaNebula](https://discuss.ai.google.dev/u/OrangiaNebula)\
**Post date:** [October 21, 2024, 6:37pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001/4 "2024-10-21T18:37:01Z")

</div>

Use Google to search for “environment variables” and how to set them (it depends on operating system and command interpreter / shell).  
The code expects API\_KEY to be the name of the environment variable, and the value assigned to that environment variable, before your process starts up, is supposed to be the api key indecipherable bytes.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![gurs](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/gurs/32/22999_2.png) [@gurs](https://discuss.ai.google.dev/u/gurs)\
**Post date:** [October 21, 2024, 8:11pm UTC](https://discuss.ai.google.dev/t/keyerror-using-imagen-3-api-via-python/45001/5 "2024-10-21T20:11:43Z")

</div>

Indeed! Thanks for pointing that out @afirstenberg, and for the help on both threads @OrangiaNebula.
