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

mvind's avatar
Level 1

Consume own api get 401 unauthenticated error

Hi everyone!

I'm really struggling to setup consumption of my own api. I have followed the guide and added the middleware groups, and when I log in I do get a generated laravel token.

my bootstrap looks like this:


  window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
var token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
  window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
  console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

And I can see in the axios post config that I do send the correct tokens and csrf tokens. But I keep getting this message: {message: "Unauthenticated."}

I have tested my api in postman and that works fine so the problem is only with my consuming the api from the website.

Do anyone have any tips?

0 likes
1 reply
mvind's avatar
Level 1

Even when I remove middleware('auth:api') from my api routes I still get {message: "Unauthenticated."}.

Please or to participate in this conversation.