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

nickclicksco's avatar

Using api.php routes without the need for an access token

Hi guys, I want to use the api.php file so that I can turn my Spark dashboard into an SPA.

At the moment I have tried using this:

Route::group([
    'prefix' => 'v1',
    'middleware' => 'auth:api',
], function () {
    Route::get('/hello', function () {
        return response()->json(["hello" => "there"]);
    });
}); 

If I hit this api/v1/hello route it will redirect me to the home route unless I set an access token within the dashboard. When I include the access token as a parameter in the URL the route starts to work.

This is no good for me though. I just want to check if a user is logged in and if they are then serve the response from the API route.

I have tried using:

'middleware' => 'auth'

instead of using:

'middleware' => 'auth:api'

However this didn't work, I was just redirected when I tried to visit the route.

Any help would be great, thanks.

0 likes
2 replies
huglester's avatar

have you tried using: 'middleware' => 'api' ?

Please or to participate in this conversation.