# NextJS with Express API, InvalidTokenError: Invalid URL

**URL:** <https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732>\
**Category:** Get Help\
**Tags:** other, test\
**Created:** [January 12, 2024, 1:45pm UTC](https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732 "2024-01-12T13:45:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinassaid](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@martinassaid](https://community.auth0.com/u/martinassaid)\
**Post date:** [January 12, 2024, 1:45pm UTC](https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732/1 "2024-01-12T13:45:08Z")

</div>

Hi there.

I have NextJS frontend using the Pages Router and Express API. Some API actions require the user to be authenticated. The API request is returning an “InvalidTokenError: Invalid URL”. Curiously, the access token I get back has an array for the audience in the payload - one value is the API identifier (which I would expect) ([http://localhost:5500/api/v1/example](http://localhost:5500/api/v1/example)) and the other is the application domain with the path “userinfo” ([https://example.ca.auth0.com/userinfo](https://example.ca.auth0.com/userinfo)).

The general setup is as follows:

```auto
// pages/api/auth/[...auth0].ts
import { handleAuth, handleLogin } from "@auth0/nextjs-auth0";

export default handleAuth({
	login: handleLogin({
		authorizationParams: {
			audience: "http://localhost:5500/api/v1/example", // taken from API identifier in Auth0 dashboard
			scope: "openid profile email read:example",
		},
	}),
});

// pages/index.tsx
import { Button } from "@/components/ui/button";
import { getAccessToken } from "@auth0/nextjs-auth0";

export default function Home({ accessToken }) {
	return (
		<>
			<Button
				onClick={async () => {
					await fetch("http://localhost:5500/api/v1/example", {
						headers: {
							Authorization: `Bearer ${accessToken}}`,
						},
					});
				}}
			>
				Test Button
			</Button>
		</>
	);
}

export async function getServerSideProps(ctx) {
	try {
		const { accessToken } = await getAccessToken(ctx.req, ctx.res);
		return { props: { accessToken } };
	} catch (error) {
		console.log(error);
		return { props: { stories: [], accessToken: "" } };
	}
}

// middleware/auth0.middleware.ts (express middleware)
import { auth } from "express-oauth2-jwt-bearer";
import dotenv from "dotenv";

dotenv.config();

export const validateAccessToken = auth({
	issuerBaseURL: process.env.AUTH0_DOMAIN, // http://localhost:5500/api/v1/example
	audience: process.env.AUTH0_AUDIENCE, // example.ca.auth0.com/userinfo
});

```

Any thoughts what might be causing the error? Much thanks for your help!

---

<div class="post-metadata">

**Author:** ![martinassaid](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@martinassaid](https://community.auth0.com/u/martinassaid)\
**Post date:** [January 12, 2024, 11:30pm UTC](https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732/3 "2024-01-12T23:30:42Z")

</div>

I found the error. An environment variable in the express application was incorrect. For AUTH0\_DOMAIN I had “[mydomain.ca.auth0.com](http://mydomain.ca.auth0.com)” instead of “[https://mydomain.ca.auth0.com](https://mydomain.ca.auth0.com)”.

---

<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:** [January 12, 2024, 11:41pm UTC](https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732/4 "2024-01-12T23:41:06Z")

</div>

Hey @martinassaid that’s great I’m glad you were able to get this sorted, and thanks for sharing here 🙂

---

<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:** [January 26, 2024, 11:42pm UTC](https://community.auth0.com/t/nextjs-with-express-api-invalidtokenerror-invalid-url/124732/5 "2024-01-26T23:42:05Z")

</div>

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