I haven't tested but could you do this with the session helper
$securedApiKey = \AlgoliaSearch\Client::generateSecuredApiKey(
'YourSearchOnlyApiKey', // Make sure to use a search key
[
'filters' => 'viewable_by:' . session()->getId()
]
);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I'm trying to implement this:
//example from the page
$currentUserID = 1; // Replace by the current user ID
$securedApiKey = \AlgoliaSearch\Client::generateSecuredApiKey(
'YourSearchOnlyApiKey', // Make sure to use a search key
[
'filters' => 'viewable_by:'.$currentUserID
]
);
However, since the user is not always logged in I don't have a user id. So my guess is just to take the session id, it should work the same right?
But now I'm curious, where should I put this part of code?
My first thought was in config.services. But can it work even though I cache the config?
Should I put it somewhere else?
To fetch the key for the frontend, I'm saving it to window.App.keys
Please or to participate in this conversation.