"Laravel 10 - API key not recognized in .env file"
P粉198814372
2023-08-30 11:26:11
<p>I'm using <code>Laravel Framework 10.15.0</code>. </p>
<p>I tried loading my API key in the following way: </p>
<pre class="brush:php;toolbar:false;">$apiKeyOpenAI = env('OPENAI_API_KEY');
$client = OpenAI::client($apiKeyOpenAI);</pre>
<p>In my <code>.env</code> file, the API key is clearly defined: </p>
<p><code>OPENAI_API_KEY=xx-xxxxxxxxxxxxxxxxxxxxxxx</code></p>
<p>However, when executing my application on the server, I get that <code>$apiKeyOpenAI</code> is null. </p>
<p>However, I do have OPENAI_API_KEY in my <code>.env</code> file.I've checked it! </p>
<p>I tried clearing the cache <code>php artisan config:clear </code> but I still get the error: </p>
<pre class="brush:php;toolbar:false;">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
App\Console\Commands\AdminCommand::generateContentUsingOpenAI()</pre>
<p>Any suggestions what I'm doing wrong? </p>
<p>Thanks for your reply! </p>
<p><strong>Update</strong></p>
<p>After deploying to the server, I need to run this script to make it work: </p>
<pre class="brush:php;toolbar:false;">Route::get('/clear', function() {
Artisan::call('cache:clear');
Artisan::call('config:clear');
return "Cache, Config is cleared";
})->middleware(['auth', 'admin']);</pre>
<p>On deployment, this script will also run automatically: </p>
<pre class="brush:php;toolbar:false;">#!/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!"</pre></p>
Do not use
env()
outside ofconfig/*.php
files. If you ever runphp artisan config:cache
(which should usually be done in a production environment) thenenv()
will stop working outside of these files (for most cases says; theenv
key can still be loaded, but this is not typical for most Laravel setups). This is why you need to runphp artisan config:clear
so thatenv()
does not returnnull
.Add a key in
config/app.php
(or any other file in theconfig/
directory):Then, when you want to use this key, use
config()
Helper function:Note:
app
is the file name,open_ai_api_key
is the array index. If you use a different file, such asconfig/services.php
, then you should useconfig('services.open_ai_api_key')
Please refer to the documentation for details:
https://laravel.com/docs/10.x/configuration#configuration-caching