# APIConnectionError: Error communicating with OpenAI

**URL:** <https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334>\
**Category:** API\
**Created:** [June 30, 2023, 6:03am UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334 "2023-06-30T06:03:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon45304976](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon45304976](https://community.openai.com/u/anon45304976)\
**Post date:** [June 30, 2023, 6:03am UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/1 "2023-06-30T06:03:31Z")

</div>

Hi  
just start using api of openai  
i cannot get answer from my request via a python script ,i tried everything but still the error persist

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [June 30, 2023, 6:23am UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/2 "2023-06-30T06:23:32Z")

</div>

Welcome to the forum!

Can you show your source code please?

---

<div class="post-metadata">

**Author:** ![anon45304976](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon45304976](https://community.openai.com/u/anon45304976)\
**Post date:** [June 30, 2023, 6:37am UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/3 "2023-06-30T06:37:27Z")

</div>

simple code just starting using openai

```auto
openai.api_key = "sk-XXXXXX"
import openai
import os
response = openai.Completion.create(
  model="text-davinci-003",
  prompt="how do you say hello in italian"
  
)

```

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [June 30, 2023, 12:06pm UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/4 "2023-06-30T12:06:33Z")

</div>

ok, give this a try, I think you were missing a print statement 😃

```python
openai.api_key = "sk-XXXXXX"
import openai
import os
response = openai.Completion.create(
  model="text-davinci-003",
  prompt="how do you say hello in italian"
)
print(response.choices[0].text.strip())
```

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [June 30, 2023, 12:14pm UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/5 "2023-06-30T12:14:06Z")

</div>

I want to also point out that storing API keys in your source code is a bad idea, anyone with coding skills could find your key and misuse it this way, one of the common ways to do it is to store your key in environment variable and then pull them back in your code, that way the code itself never knows what the key is until it gets it from the system it’s on.

that would make your code look like this

```python
import os
import openai

openai.api_key = os.getenv('OPENAI_API_KEY')

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="how do you say hello in italian"
)

print(response.choices[0].text.strip())

```

In a UNIX/Linux system, you can set environment variables in the terminal as follows:

In bash, Copy this code

```auto
export OPENAI_API_KEY="sk-XXXXXX"

```

If you are using a Windows system, you can set an environment variable in the command line like this:

Run cmd and Copy this code

```auto
setx OPENAI_API_KEY "sk-XXXXXX"

```

With linux you can put the export command into your .bashrc so it gets executed whenever you start the server, in windows the setx will remain memorised over restarts.

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 24, 2023, 6:38pm UTC](https://community.openai.com/t/apiconnectionerror-error-communicating-with-openai/283334/6 "2023-12-24T18:38:07Z")

</div>

Closed on December 24, 2023, 6:38pm UTC


