# Error calling my external API

**URL:** <https://community.auth0.com/t/error-calling-my-external-api/96403>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [December 14, 2022, 4:02am UTC](https://community.auth0.com/t/error-calling-my-external-api/96403 "2022-12-14T04:02:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![solvy-dev](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solvy-dev/32/33580_2.png) [@solvy-dev](https://community.auth0.com/u/solvy-dev)\
**Post date:** [December 14, 2022, 4:02am UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/1 "2022-12-14T04:02:28Z")

</div>

I want to use the token that I get from the getAccessToken function in my Nextjs API endpoint to call my external API that I already created in the Auth0 dashboard but I’m getting this error:

```auto
# This is my NextJS API endpoint /api/books
export default withApiAuthRequired(async function getBooks(req:any, res:any) {
  const { accessToken } = await getAccessToken(req, res); # <---- I see this is not a JWT token but opaque token
  const response = await booksApi('/books?title=Calcu', { headers: { "Authorization": `Bearer ${accessToken}` } })
  res.status(200).json({ accessToken, response })
})

```

I created the books-service in the API section on the Auth0 Dashboard and added ‘read:books’ to the permissions list(scopes) but I don’t know where I should specify that permission in the code.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 14, 2022, 3:56pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/3 "2022-12-14T15:56:36Z")

</div>

Hi @solvy-dev,

Welcome to the Auth0 Community!

It sounds like you are missing the audience parameter. See this FAQ:

[https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028](https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028)

---

<div class="post-metadata">

**Author:** ![solvy-dev](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solvy-dev/32/33580_2.png) [@solvy-dev](https://community.auth0.com/u/solvy-dev)\
**Post date:** [December 15, 2022, 11:44pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/4 "2022-12-15T23:44:00Z")

</div>

Ok I could get the JWT access token sending the audience and I checked the token on the [jwt.io](http://jwt.io) website and I can see the payload

```auto
export default handleAuth({
  login: async (req, res) => {
    await handleLogin(req, res, {
      returnTo: "/dashboard",
      authorizationParams: {
        audience: 'MY_EXTERNAL_API_IDENTIFIER',
      }
    });
  }
});

```

But when I send that token to my external API I get the next response with statusCode

```auto
{
    "message": null
}

```

What type of configuration should I implement in my external API? Do I have to verify something else beside the JWT signature or set some fields?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 16, 2022, 3:34pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/5 "2022-12-16T15:34:09Z")

</div>

From there it is up to you what you want to do with your API. I can point you towards an example if you tell me about your external API (what language, framework, etc).

---

<div class="post-metadata">

**Author:** ![solvy-dev](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solvy-dev/32/33580_2.png) [@solvy-dev](https://community.auth0.com/u/solvy-dev)\
**Post date:** [December 16, 2022, 3:48pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/6 "2022-12-16T15:48:30Z")

</div>

That’d be awesome. To provide some context I’m using Serverless Framework but any NodeJS example I think is helpful. This is the code from my AWS Lambda authorizer which is an endpoint that is hit every time I send a request to other endpoints like `/api/v1/products` in order to get permission to work as expected.

```auto
    const token = event.authorizationToken.replace('Bearer ', '');
    const claims = jwt.verify(token, EnvironmentConfig.auth0PublickKey) as JwtPayload; 
    const principalId = claims.sub as string
    const policy = generatePolicy(principalId, event.methodArn);
    return {
      ...policy,
      context: claims 
    };

```

When I verify the token I use the PEM certificate downloaded from my regular web app on the Auth0 dashboard. That’s all the code that I’m using on my backend to handle the token. Am I missing something?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 16, 2022, 4:24pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/7 "2022-12-16T16:24:40Z")

</div>

Have you seen this blog? It sounds like it runs over most of your setup:

> **[Securing AWS HTTP APIs with JWT Authorizers](https://auth0.com/blog/securing-aws-http-apis-with-jwt-authorizers/)**
>
> Learn about securing HTTP APIs built on AWS API Gateway using JWT authorization

---

<div class="post-metadata">

**Author:** ![solvy-dev](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solvy-dev/32/33580_2.png) [@solvy-dev](https://community.auth0.com/u/solvy-dev)\
**Post date:** [December 16, 2022, 9:46pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/8 "2022-12-16T21:46:11Z")

</div>

Ok I found the solution, the problem is in the Context returned by the authorizer AWS Lambda function. The type of that context is:

```auto
export interface APIGatewayAuthorizerResultContext {
    [name: string]: string | number | boolean | null | undefined;
}

```

And the JWT access token payload returned by Auth0 has the property `aud` which contains an array and that breaks something in AWS and that’s why the response is `{ "message": null }` with status code 500.

Thanks @dan.woda for your help! Now my external API is working as expected 😃

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 16, 2022, 10:12pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/9 "2022-12-16T22:12:22Z")

</div>

Thanks for sharing! 🙂

---

<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:** [December 30, 2022, 10:12pm UTC](https://community.auth0.com/t/error-calling-my-external-api/96403/10 "2022-12-30T22:12:36Z")

</div>

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