Authenticate user on Webhook

Following instructions in: https://docs.8base.com/docs/8base-console/custom-functions

    await ctx.api.gqlRequest(QUERY, VARIABLES, {
        headers: {
        Authorization: "Bearer <MY_API_TOKEN>"
    }
});

I have this example of a webhook for testing purposes:

import gql from 'graphql-tag';

module.exports = async (event, ctx) => {
  console.log("EVENT:", event);
  console.log("CTX:", ctx);

  const query = gql`
   query{
    user{
      id
      email
    }
  }
 `;

  const token = "Bearer eyJhbGciOiJSUzI1NiIsInR5<TRIMMED>"
  const API_TOKEN = "Bearer 4969008e<TRIMMED>"
  const response = await ctx.api.gqlRequest(query, {
    headers: {
      Authorization: token
    }
  });

  console.log(response);
};

The first one is a token from the frontend user, the second one is a token generated in the 8base console. Both raise: You don't have permission to perform this operation when accessing the API.

Is there another way to accessing the API from a webhook, or is this the way and I’m just missing something.

Similar Question: Validate user in webhook

Hi! You are trying to pass the headers as the second parameter. According the documentation it’s the third parameter. Could you try to pass the headers as the third parameter and try one more time?

I tried as suggested:

  const response = await ctx.api.gqlRequest(query, {}, {
    headers: {
      Authorization: API_TOKEN
    }
  });

  console.log(response);

And also:

  const response = await ctx.api.gqlRequest(query, {}, {
    headers: {
      Authorization: token
    }
  });

  console.log(response);

both fail with the same message as before

We have figured out the issue and released the hotfix. Could you try one more time, please?

2 Likes