Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alinmoai's avatar

what is Correct way to set 3rd api parameter

my project have some API will call 3rd party, and the 3rd party api request some parameter like user, password, i put those parameter in .env.

i set those parameter before i call the 3rd api, so every 3rd party api call will init those 3rd party parameter. i have several controller will call the 3rd api call so have many duplicate code to init 3rd parameter function.

my idea is to move the params init code to the middleware, but is there a more correct way like use singleton for these parameter ? what i am doing now in one of the controller is like

class XXXDataController extends BaseController
    public function index(Request $request)
    {
        $params = new Params([
            'authParams' => 
                'userId' => env('xxx3rd_user_id'), 
                'password' => env('xxx3rd_pass'),
				xxx,
				xxx,
				...
            ],

		$client = new Client($params);
		$client->requestSent();
		...
		...
0 likes
2 replies
tykus's avatar

You should not use the env helper in production; when the config gets cached, the env will not be loaded.

https://laravel.com/docs/10.x/helpers#method-env

As to the problem you are actually trying to solve, you probably want a dedicated Client class specifically for the 3rd party API, which allows you to define the credentials in one place only.

1 like

Please or to participate in this conversation.