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

webfuelcode's avatar

Where to put API key

I am working on a project with some content from another site using API. I got the URL and API key and did not get where to put the API key in the controller.

function index()
{
     $collection = Http::get("https://apiwesiteaddress...../stats");
      return view('stats', ['collection'=>$collection]);
}
0 likes
5 replies
Sinnbeck's avatar

Do you mean "Where do I store it safely"? Or "Where to I put it in Http::get" ?

webfuelcode's avatar

@Sinnbeck I am just started learning it. So I want to know where to put it in Http::get and also the safe place to store it.

First, tell me where to put the API key in Http::get, and then please tell me how to store it safely.

Sinnbeck's avatar

@webfuelcode

Ok. Safely storing it first add this to your env

SOMESITE_API="myapikey"

and in a config file (you could create a new one, or use one of the existing), add this

'somesite_api_key' = env('SOMESITE_API'),

and use in code

$key = config('nameofconfigfile.somesite_api_key');

Now for the "how to". That depends on the API docs. Can you provide them?

1 like
Sinnbeck's avatar

@webfuelcode Check the docs and see that it uses a header

$response = Http::withHeaders([
   'X-Access-Token' => $key
])->get(...)

Please or to participate in this conversation.