# How to authenticate API based on the access token?

**URL:** <https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [March 20, 2023, 2:11pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939 "2023-03-20T14:11:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sympi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sympi](https://community.auth0.com/u/sympi)\
**Post date:** [March 20, 2023, 2:11pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/1 "2023-03-20T14:11:18Z")

</div>

I have an electron desktop app that communicates with my Node.js API (not express.js tho). The electron app sends access token in a `Authorization` header, like this: `Authorization: Bearer <access_token>` . I set it manually for each http request, not a big deal. The access token comes from Auth0. How can I verify this token in my Node.js server?

So, I just have access token string on my API server and would like to verify it somehow.

---

<div class="post-metadata">

**Author:** ![osauceda](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@osauceda](https://community.auth0.com/u/osauceda)\
**Post date:** [March 20, 2023, 8:33pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/3 "2023-03-20T20:33:44Z")

</div>

Hi @sympi

You can certainly do that and it’s fairly simple.

You can follow this guide [Full-Stack TypeScript Apps: Developing a Secure API with NestJS](https://auth0.com/blog/developing-a-secure-api-with-nestjs-adding-authorization/)

I was using NestJS as my backend framework, should be pretty similar for any other framework.

Let us know if you have any questions

---

<div class="post-metadata">

**Author:** ![sympi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sympi](https://community.auth0.com/u/sympi)\
**Post date:** [March 20, 2023, 9:06pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/4 "2023-03-20T21:06:30Z")

</div>

Hi @osauceda,

Do I have to use express-jwt with jwks-rsa? It looks like it is designed to be used with express. I don’t really have a concept of middlewares and req, res, next thing. All I have is an accessToken, I get it from Authorization header.

---

<div class="post-metadata">

**Author:** ![osauceda](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@osauceda](https://community.auth0.com/u/osauceda)\
**Post date:** [March 20, 2023, 10:03pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/5 "2023-03-20T22:03:40Z")

</div>

Can you share what’s the structure of your node backend project? Or is it a vanilla application?

---

<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:** [March 20, 2023, 11:04pm UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/6 "2023-03-20T23:04:06Z")

</div>

Hey there @sympi welcome to the community! Howdy @osauceda! 🤠

For Node in particular, I definitely recommend taking a look at [express-oauth2-jwt-bearer](https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer) - Here’s a quick blog post on it as well:

> **[Introducing the OAuth 2.0 Express SDK for Protecting APIs with JWT Bearer Tokens](https://auth0.com/blog/introducing-oauth2-express-sdk-protecting-api-with-jwt/)**
>
> Learn how the OAuth 2.0 Express SDK released by Auth0 makes protecting your Express API easier.

This library makes it easy to validate access tokens tokens included in the authorization header as well as more granular authorization such as [checking for specific scopes, claims, etc.](https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer#api-reference)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![sympi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sympi](https://community.auth0.com/u/sympi)\
**Post date:** [March 21, 2023, 8:20am UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/7 "2023-03-21T08:20:34Z")

</div>

@osauceda yes, it’s a vanilla node.js server.

This is what I came up with to verify accessToken. Simple function that takes accessToken as a string and verifies it. Can you confirm if that’s correct and this is what we should do?

```auto
import jwt from 'jsonwebtoken';
import jwkToBuffer from 'jwk-to-pem';

type JWKFile = {
    keys: jwkToBuffer.JWK[];
};

const ISSUER = 'https://example.com'; // specify real one

export const verifyJwt = (token: string) => {
    return new Promise(async (resolve, reject) => {
        // make a request to get the JWKs
        const jwks = (await (await fetch(`${ISSUER}/.well-known/jwks.json`)).json()) as
            | JWKFile
            | undefined;

        if (!jwks || jwks.keys?.[0] == undefined) {
            return undefined;
        }

        // convert the jwks to a pem string
        const pem = jwkToBuffer(jwks.keys[0]);
        jwt.verify(
            token,
            pem,
            {
                algorithms: ['RS256'],
            },
            (err, decoded) => {
                if (err) {
                    return reject(err);
                }
                return resolve(decoded);
            },
        );
    });
};

```

---

<div class="post-metadata">

**Author:** ![sympi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sympi](https://community.auth0.com/u/sympi)\
**Post date:** [March 24, 2023, 8:07am UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/8 "2023-03-24T08:07:14Z")

</div>

can somebody help? I have an `accessToken` string and would like to verify if it’s correct. How can I do this? All the above examples assume that I have express style middleware, but I don’t.

---

<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:** [March 28, 2023, 12:37am UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/9 "2023-03-28T00:37:44Z")

</div>

Hey @sympi!

Please see our documentation on how to [validate access tokens](https://auth0.com/docs/secure/tokens/access-tokens/validate-access-tokens).

---

<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:** [April 11, 2023, 12:37am UTC](https://community.auth0.com/t/how-to-authenticate-api-based-on-the-access-token/102939/10 "2023-04-11T00:37:56Z")

</div>

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