Drew Angell
Drew Angell

Reputation: 26056

How to work with environment variables on Windows/Linux..?

I am trying to work with an OpenAI library (https://github.com/orhanerday/open-ai) that uses environment variables for key storage, but it doesn't seem to be finding the key when I run it.

On my local Windows machine I ran the following command: setx OPENAI_API_KEY “mykey”

On the Linux web server I ran the following command: export OPENAI_API_KEY=mykey

Now on the server when I run the following, I see the correct key value printed back to me: printenv OPENAI_API_KEY

In my script I'm using $open_ai_key = getenv('OPENAI_API_KEY'); but I'm getting no value back..??

Any information on how I can resolve this would be greatly appreciated. Thanks!

Upvotes: 1

Views: 1044

Answers (1)

ORHAN ERDAY
ORHAN ERDAY

Reputation: 1076

Thank you for using orhanerday/OpenAI PHP SDK,

Let's try to set your ‘OPENAI_API_KEY’ Environment Variable through the Control Panel

  1. Open System properties and select Advanced system settings enter image description here

  2. Select Environment Variables... enter image description here

  3. Select New… from the User variables section(top). Add your name/key-value pair, replacing with your API key.

Variable name: OPENAI_API_KEY
Variable value: <yourkey>
  1. Sign out and then log in to your PC.

enter image description here

  1. Create a PHP file;
<?php
    $open_ai_key = getenv("OPENAI_API_KEY");
    print("OPENAI_API_KEY is; $open_ai_key");
  1. run the PHP file
$ php index.php 
> OPENAI_API_KEY is: sk-gjtv.....

After running the app you should get the value.

Upvotes: 2

Related Questions