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

ultrasamad's avatar

Consuming REST API in PHP with Guzzle

Consuming REST API in PHP with Guzzle. In my situation, I'm consuming a REST API with Guzzle HTTP client. I however not sure the recommended way of storing the authentication token. Is sessions the right way to go? What is the recommended approach in a situation like this?

0 likes
9 replies
bobbybouwmann's avatar

Session or database is fine. It depends on the livecycle of the token. Is it a temporary token then store it in the session. Is it a token you need to use the rest of the year? Store it in the database. Make sure you encrypt the token before you store it ;)

ultrasamad's avatar

@CJJ Why should I use local/session storage at the backend? The purpose was to consume it using PHP and not at the frontend.

Web Confection's avatar

@ultrasamad My apologies, didn't realise we were talking blade. I posted without thinking. I always use a decoupled frontend.

bobbybouwmann's avatar

You can put it in either place. When you reboot your server you lose the token in the session, in the database it's always there :)

1 like
ultrasamad's avatar

@bobbybouwmann I realized I could not write to the session. No error is returned but it wasn't just working. But when I tried it on the web routes, it works. So I was thinking probably session storage is disabled for api routes...

Any help on that?

bobbybouwmann's avatar
Level 88

@ultrasamad It's disabled by default for API's by Laravel. If you open ' app/Http/Kernel.php you can see that the api group does not have a session middleware and that the web group does have a session middleware.

Your API does not need a session most of the time. If it does you can simply add the session middleware and you're done!

1 like

Please or to participate in this conversation.