I am using Laravel Framework 10.15.0
.
I am trying to load my API-Keys the following way:
$apiKeyOpenAI = env('OPENAI_API_KEY');
$client = OpenAI::client($apiKeyOpenAI);
In my .env
file the api key is clearly defined:
OPENAI_API_KEY=xx-xxxxxxxxxxxxxxxxxxxxxxx
However, when executing my application on the server I get that the $apiKeyOpenAI
is null.
Still my .env
file has the OPENAI_API_KEY in it. I checked this!
I tried to clear my cache php artisan config:clear
, I still get the error:
TypeError
OpenAI::client(): Argument #1 ($apiKey) must be of type string, null given, called in /var/www/demo-website/app/Console/Commands/AdminCommand.php on line 151
at vendor/openai-php/client/src/OpenAI.php:13
9▕ {
10▕ /**
11▕ * Creates a new Open AI Client with the given API token.
12▕ */
➜ 13▕ public static function client(string $apiKey, string $organization = null): Client
14▕ {
15▕ return self::factory()
16▕ ->withApiKey($apiKey)
17▕ ->withOrganization($organization)
1 app/Console/Commands/AdminCommand.php:151
OpenAI::client()
2 app/Console/Commands/AdminCommand.php:39
AppConsoleCommandsAdminCommand::generateContentUsingOpenAI()
Any suggesitons what I am doing wrong?
I appreciate your replies!
UPDATE
After deploying to the server I need to run this script so that it seems to work:
Route::get('/clear', function() {
Artisan::call('cache:clear');
Artisan::call('config:clear');
return "Cache, Config is cleared";
})->middleware(['auth', 'admin']);
When deploying this script is also automatically run:
#!/bin/sh
set -e
echo "Deploying application ..."
# Enter maintenance mode
(php artisan down) || true
# Update codebase
git fetch origin deploy
git reset --hard origin/deploy
# Install dependencies based on lock file
composer install --no-interaction --prefer-dist --optimize-autoloader
# Migrate database
php artisan migrate --force
# Note: If you're using queue workers, this is the place to restart them.
# ...
# Clear cache
# php artisan optimize
php artisan config:cache
php artisan route:clear
php artisan route:cache
php artisan view:clear
php artisan view:cache
php artisan auth:clear-resets
php artisan cache:clear
php artisan config:clear
# Generate sitemap
# php artisan sitemap:generate
# Reload PHP to update opcache
echo "" | sudo -S service php8.1-fpm reload
# Exit maintenance mode
php artisan up
echo "Application deployed!"
2
Answers
Don’t use
env()
outside of files inconfig/*.php
. If you ever runphp artisan config:cache
(which should typically be done on Production as you’re doing), thenenv()
will stop working outside of those files (for most situations;env
keys can still be loaded, but that is not typical for most Laravel setups). This is the reason you’re having to runphp artisan config:clear
forenv()
to not returnnull
.Add a Key to
config/app.php
(or any other file inconfig/
):Then, when you want to use this key, use the
config()
helper:Note:
app
is the filename,open_ai_api_key
is the array index. If you use a different file, likeconfig/services.php
, then it would beconfig('services.open_ai_api_key')
See the Documentation for Details:
https://laravel.com/docs/10.x/configuration#configuration-caching
Run this.