# API authentication not possible from SPA

**URL:** <https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [August 13, 2023, 2:11pm UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542 "2023-08-13T14:11:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![manuel.poelzl1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@manuel.poelzl1](https://community.auth0.com/u/manuel.poelzl1)\
**Post date:** [August 13, 2023, 2:11pm UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542/1 "2023-08-13T14:11:01Z")

</div>

Hi there,  
I am in the process of securing my API using Auth0, so far everything seemed to work fine, I followed the steps in this guide here: [Tutorial](https://auth0.com/blog/node-js-and-express-tutorial-building-and-securing-restful-apis/) (Except that I used MySQL instead of MongoDB, but I don’t think that should make any difference).

The middleware works because requests without tokens return an error that no token was provided.

But now I have the question how to get a token from my SPA that is valid for the API.

I called the `getAccessTokenSilently` method and it returned a token.

I have sent this token as an authorization header in my requests as described in the instructions, but I still get a message that no token was provided.

Am I using the wrong token or do I need to configure something differently?

And one last question, I have seen that as soon as I create an API, an application is created with the name of the API + (Test Application) and it is marked as authorized, what exactly is going on with this application and do I possibly need to set my normal SPA app in Auth0 to authorized as well?

It would be great if someone could help me here.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 23, 2023, 12:37am UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542/3 "2023-08-23T00:37:17Z")

</div>

Hey there @manuel.poelzl1!

> [@manuel.poelzl1](#):
>
> The middleware works because requests without tokens return an error that no token was provided.

Good to know!

> [@manuel.poelzl1](#):
>
> I called the `getAccessTokenSilently` method and it returned a token.
> 
> I have sent this token as an authorization header in my requests as described in the instructions, but I still get a message that no token was provided.
> 
> Am I using the wrong token or do I need to configure something differently?

Which SDK are you using in particular? What does the token look like when you decode it at [jwt.io](http://jwt.io) (it needs to be an [access token](https://auth0.com/docs/secure/tokens/access-tokens#sample-access-token))? You’ll need to make sure however you are requesting the token you’re including an `audience` param - This should match the API identifier of your API registered in Auth0. This [guide](https://auth0.com/docs/quickstart/spa/react/02-calling-an-api#set-up-the-auth0-service) may be helpful.

> [@manuel.poelzl1](#):
>
> And one last question, I have seen that as soon as I create an API, an application is created with the name of the API + (Test Application) and it is marked as authorized, what exactly is going on with this application and do I possibly need to set my normal SPA app in Auth0 to authorized as well?

The system is just creating an application to use to “test” against the API - That is, in API settings there is a “Test” tab that needs an authorized M2M app in order to function. Any M2M app can be used to test, but it creates a default one. Only M2M apps can be “authorized” so no need to worry about the SPA in that sense.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![manuel.poelzl1](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@manuel.poelzl1](https://community.auth0.com/u/manuel.poelzl1)\
**Post date:** [August 27, 2023, 4:03pm UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542/4 "2023-08-27T16:03:43Z")

</div>

Hi @ty.frith

I got it to work!  
Looks like I had some configuration problem in the Node backend, with the right Audience and the Token as Authorization parameter in my SPA everything works now as planned!

Thanks again for your help!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 28, 2023, 4:20pm UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542/5 "2023-08-28T16:20:03Z")

</div>

That’s great to hear! Thanks for following up with the community 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 11, 2023, 4:20pm UTC](https://community.auth0.com/t/api-authentication-not-possible-from-spa/113542/6 "2023-09-11T16:20:23Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
