api routes do not use sessions or csrf - they are stateless
if you want sessions put your routes in web.php
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm seeking some clarity and guidance about when I need to use sessions and use the StartSession middleware. I have some forms on pages that are behind auth. Submissions trigger axios requests (GET, POST, and PATCH) in api routes to interact with a database. Aside from csrf protection, these routes have no extra security or authentication. Yet in testing I see at least one of these submissions triggering a session not set in store error -- unless I have \Illuminate\Session\Middleware\StartSession::class in the api middleware group. The
Laravel default for this group does not contain this middleware.
My understanding of when sessions are needed is fuzzy. Why would a new session be required for one of these api routes doing an axios request to the server, when the user generating the request is already logged in? Should I put \Illuminate\Session\Middleware\StartSession::class in the global middleware group?
Typical API endpoints are stateless, meaning the server does not hold any information about the endpoint's consumer. It also does not make sense anyway, because REST in essence is stateless. That's why the default configuration does not contain anything about cookies, CSRF, sessions.
Your use case of "API" is probably an extension to the existing application that is being served.
"when the user generating the request is already logged in" implies persistent state, and that very state has to be retrieved from somewhere: the session storage.
Take a look at this middleware from Sanctum: https://github.com/laravel/sanctum/blob/2.x/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php
Please or to participate in this conversation.