# Api connection -ConnectionError

**URL:** <https://discuss.ai.google.dev/t/api-connection-connectionerror/35639>\
**Category:** Gemini API\
**Created:** [August 8, 2024, 11:45am UTC](https://discuss.ai.google.dev/t/api-connection-connectionerror/35639 "2024-08-08T11:45:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Ski](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/tom_ski/32/21268_2.png) [@Tom\_Ski](https://discuss.ai.google.dev/u/Tom_Ski)\
**Post date:** [August 8, 2024, 11:45am UTC](https://discuss.ai.google.dev/t/api-connection-connectionerror/35639/1 "2024-08-08T11:45:48Z")

</div>

have a problem with Api connection to my app, in py. turned off firewall, testet and nothing no respose, Someone know whats going on ?

**import requests**  
**import os**

**# my API**  
**api\_key = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxx”**

**# example “prompt” (zastąp swoim tekstem)**  
**prompt = “Napisz krótki wiersz o wiośnie.”**

**url = “[https://api.gemini.google.com/v1/generateText](https://api.gemini.google.com/v1/generateText)”**  
**headers = {**  
\*\* “Authorization”: f"Bearer {api\_key}"\*\*  
**}**  
**data = {**  
\*\* “prompt”: prompt,\*\*  
\*\* # Dodaj inne parametry żądania, jeśli są wymagane (sprawdź dokumentację)\*\*  
**}**

**response = requests.post(url, headers=headers, json=data)**

**if response.status\_code == 200:**  
\*\* print(“Sukces! Odpowiedź od API Gemini:”)\*\*  
\*\* print(response.json()[‘generated\_text’])\*\*  
**else:**  
\*\* print(f"Błąd: {response.status\_code}")\*\*  
\*\* print(response.text)\*\*

_ **Terminal:** _

File “C:\Users\User\anaconda3\Lib\site-packages\requests\adapters.py”, line 519, in send  
raise ConnectionError(e, request=request)  
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘[api.gemini.google.com](http://api.gemini.google.com)’, port=443): Max retries exceeded with url: /v1/generateText (Caused by NewConnectionError(‘\<urllib3.connection.HTTPSConnection object at 0x000001A0E87930D0\>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed’))  
PS C:\Users\User\Desktop\Projekt-ARC\>

---

<div class="post-metadata">

**Author:** ![LindaLawton](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lindalawton/32/16_2.png) [@LindaLawton](https://discuss.ai.google.dev/u/LindaLawton)\
**Post date:** [August 8, 2024, 11:51am UTC](https://discuss.ai.google.dev/t/api-connection-connectionerror/35639/2 "2024-08-08T11:51:00Z")

</div>

For starters api key is not a bearer token.

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

Second why not use the python client libary.

---

<div class="post-metadata">

**Author:** ![Tom\_Ski](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/tom_ski/32/21268_2.png) [@Tom\_Ski](https://discuss.ai.google.dev/u/Tom_Ski)\
**Post date:** [August 8, 2024, 11:53am UTC](https://discuss.ai.google.dev/t/api-connection-connectionerror/35639/3 "2024-08-08T11:53:59Z")

</div>

that is true [LindaLawton], thank You for advice, I think also the problem is that: Gemini API free tier is not currently available in your country. Please enable billing via Settings once your API key has been created. Read more [here](https://ai.google.dev/gemini-api/docs/billing#is-Gemini-free-in-EEA-UK-CH). 🙂 latter I’LL check that,

---

<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:** [August 8, 2024, 1:12pm UTC](https://discuss.ai.google.dev/t/api-connection-connectionerror/35639/4 "2024-08-08T13:12:55Z")

</div>

As an aside, if you **did** want the API Key in the header for whatever reason, you should use the header

```plaintext
   X-Goog-Api-Key

```

with the API key as the value.
