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

mmickells's avatar

How to use API Bearer Token with Config('services')

I'm working in the Video Game Aggregator however the API has changed quite a bit since the series was published causing some confusion.

In the series they use they use a config('services.key') approach which storing the key in the .env file.

Now that the API is updated to V4 it requires a "Client-ID" and Authorization bearer token.

I can just bypass the config option and enter the API details directly as shown below and works but that doesn't seem like a good idea from a security perspective.

$gameRankings = Http::withHeaders([
            'Client-ID' => 'CLIENT_API_KEY_HERE',
            'Authorization' => 'Bearer ACCESS_TOKEN_HERE',
        ])

I tried the following approach to combine bearer + key with a space between but still get an API Error. Good news is at least the API sees I'm trying, guess that's a start.

$gameRankings = Http::withHeaders([
            'Client-ID' => config('services.api.client-id'),
            'Authorization' => config('bearer ' . 'services.api.key')
        ])

Error:

array:6 [▼
  "message" => "Authorization Failure. Have you tried:"
  "Tip 1" => "Ensure you are sending Authorization and Client-ID as headers."
  "Tip 2" => "Ensure Authorization value starts with 'Bearer ', including the space"
  "Tip 3" => "Ensure Authorization value ends with the App Access Token you generated, NOT your Client Secret."
  "Docs" => "https://api-docs.igdb.com/#authentication"
  "Discord" => "https://discord.gg/FrvfwQg"
]

My question is how can I use a config/env solution and still include the bearer + key?

0 likes
2 replies
piljac1's avatar
piljac1
Best Answer
Level 28

Yes you can. You have a major error (breaking) and two minor errors (not breaking) going on.

The major error comes from this line:

'Authorization' => config('bearer ' . 'services.api.key')

Which in the end, gives you:

'Authorization' => config('bearer services.api.key')

What you really want to do is:

'Authorization' => 'Bearer '.config('services.api.key')

Your two minor errors are convention-wise.

  1. Your config keys should be snake case. That means that client-id should be client_id.
  2. In the Laravel ecosystem, string concatenations should not have spaces around dots.
1 like
mmickells's avatar

@piljac1 Thank you for your response. You were spot on. Thanks for explaining the snake case. I didn't realize that. Learn something new everyday! Thanks again for the comment, it worked perfectly.

Please or to participate in this conversation.