External API: Reuse across components

I want to consume an external API for reuse over several components. Where should i implement this in Laravel with Livewire. Traits?
And where should i store access tokens. Session?

Hi,

You’ve probably already sorted this by now but if not then yes Traits are a good place for functionality that needs to be used in several components.

As for API tokens I would create environment variables in the .env file then add them to the configuration.

https://laravel.com/docs/8.x/configuration

Hope this helps.

1 Like

Hi Nigel,

Thank you for your response!

I agree about the .env, but I have two types of tokens. The first one is a static access token so that’s fine to put in the .env, but further down the API it issues time limited scoped access tokens. These needs to be cached somehow. I am thinking session cookie, but maybe there are better ways?

Hi,

Sorry for the late reply. Did you manage to get everything sorted?

I don’t know how the API you’re using works. Is there authentication in your application? Could you set the timed API key on the authenticated user and then update them when they’re reset?