# Invalid Token when fetching from protected API

**URL:** <https://community.auth0.com/t/invalid-token-when-fetching-from-protected-api/126167>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [January 31, 2024, 12:13pm UTC](https://community.auth0.com/t/invalid-token-when-fetching-from-protected-api/126167 "2024-01-31T12:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lars1337](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lars1337/32/50484_2.png) [@lars1337](https://community.auth0.com/u/lars1337)\
**Post date:** [January 31, 2024, 12:13pm UTC](https://community.auth0.com/t/invalid-token-when-fetching-from-protected-api/126167/1 "2024-01-31T12:13:18Z")

</div>

Hello,

i have 2 projects connected with auth0. One is my NextJS Frontend which runs as a regular web application. This one works perfectly fine. Now i added a expressJS API to APIs and followed the guideline. When i try to fetch from a protected express route, i always get InvalidToken error. I dont use any claims or scopes. i just want the basic fetch to work.

The error i get: InvalidTokenError: Invalid Compact JWS

Here is the express part:

```auto
import express from 'express';
const app = express();
const { auth } = require('express-oauth2-jwt-bearer');

const port = process.env.PORT || 8000;

const jwtCheck = auth({
  audience: 'http://localhost:8000/api',
  issuerBaseURL: 'MY-DOMAIN',
  tokenSigningAlg: 'RS256'
});

app.get('/open', function (req, res) {
    res.json({ message: 'open' });
});

app.get('/protected', jwtCheck, function (req, res) {
    res.json({ message: 'protected' });
});

app.listen(port);

console.log('Running on port ', port);

```

And this is how i fetch inside NextJS Server Component:

```auto
import { getAccessToken } from "@auth0/nextjs-auth0";

sync function getData() {
    const accessToken = getAccessToken();
    const res = await fetch('http://localhost:8000/protected', {
        headers: {
          'Authorization': `Bearer ${accessToken}`
        },
      })
   
    if (!res.ok) {
      throw new Error('Failed to fetch data')
    }
   
    return res.json()
  }

```

Im out of Ideas right now. Is my code wrong? Did i forget any settings in auth0 Dashboard?

---

<div class="post-metadata">

**Author:** ![lars1337](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lars1337/32/50484_2.png) [@lars1337](https://community.auth0.com/u/lars1337)\
**Post date:** [January 31, 2024, 1:54pm UTC](https://community.auth0.com/t/invalid-token-when-fetching-from-protected-api/126167/2 "2024-01-31T13:54:15Z")

</div>

I could solve the problem following the following guide. Im not sure what did the trick aber if anyone else stumbles upon the same problem, this guide helps a lot. So all credits to Benjamin Chavez.

[Auth0 Guide with Epress API and NextJS](https://benjamin-chavez.com/blog/integrating-next.js-with-express.js-using-auth0-for-authentication)

---

<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:** [February 14, 2024, 1:54pm UTC](https://community.auth0.com/t/invalid-token-when-fetching-from-protected-api/126167/3 "2024-02-14T13:54:55Z")

</div>

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