axelbol
2 weeks ago
118
2
Laravel

Help calling key from folder config file services.php

Posted 2 weeks ago by axelbol

I am trying to fetch data from an external API through Token, I tested with Insomnia and everything works fine but here is the problem. In my controller I call this token to get the data like this...

public function index()
{
  $popularMovies = Http::withToken('HereGoesTokenFromAPI')
  	->get('https://api.something.org/popular')
        ->json();

    dd($popularMovies);
    return view('index');
}

This works fine. But I want to refactor the code and do not put the TokenFromAPI in my controller I want to call it from .env so I tried writing and calling it from config/services

'someName' => [
        'token' => env('TOKEN_API'),
    ],

and controller like...

public function index()
    {
        $popularMovies = Http::withToken(config('services.someName.token'))
	    ->get('https://api.something.org/popular')
            ->json();

        dd($popularMovies);
        return view('index');
    }

and of course writing in .env the token

TOKEN_API=SOMEKEYAPI

But for some reason when I refactor this way the API KEY does not call it. So please help me how can I call the API the way I was trying to do. Calling it from the folder config and file services.

Please sign in or create an account to participate in this conversation.