StatusCodeError: 403 - {"errors":[{"message":"Access Denied

Hello there,

I am creating a metafield for store using following URL

[https://snippet-trps.myshopify.com/admin/api/2023-01/metafields.json ](https://snippet-trps.myshopify.com/admin/api/2023-01/metafields.json)

the payload for this URL is

{
  metafield: {
    namespace: 'tt_schema',
    key: 'tt_schema_premium',
    value: true,
    type: 'boolean'
  }
}

Hit POST request

Now I got this error:
StatusCodeError: 403 - {“errors”:[{“message”:“Access Denied: You do not have permission to access this
website.”,“extensions”:{“code”:“ACCESS_DENIED”}}]}

Can anyone help me out what is the issue actually?

Hi @troopstech23 – 403 is almost certainly related to how you are authenticating. Are you passing in the auth token in the X-Shopify-Access-Token header? Also keep in mind if you uninstalled the app and re-installed it, the auth token will have changed. Can you try generating a new auth token and ensuring it’s being sent in the correct header?

Yes, I am passing a auth token in header X-Shopify-Access-Token : token.
I used my updated auth token always because I used it from the shopify app bridge session.

here is my code for detail

const handleshopifyRequest = async (type, url, token, body) => {
  try {
    let options = {
      method: type,
      url: url,
      json: true,
      body: body,
      resolveWithFullResponse: true, //added this to view status code
      headers: {
        "X-Shopify-Access-Token": token,
        "content-type": "application/json",
      },
    };
    return request(options);
  } catch (err) {
    throw err;
  }
};
const updateMetafieldPremium = async (userData, value) => {
  try {
    let metafieldPayload = {
      metafield: {
        namespace: "tt_schema",
        key: "tt_schema_premium",
        value: value,
        type: "boolean",
        owner_resource: "shop",
      },
    };
    console.log(metafieldPayload, "Metafield Payload");
    console.log(
      "https://rewmar@" +
        userData.shopUrl +
        "/admin/api/" +
        process.env.SHOPIFY_API_VERSION +
        "/metafields.json",
      "URLURLURLURLURL",
      userData.accessToken,
      "Access Token"
    );
    if (userData.metafield && userData.metafield.id) {
      return await handleshopifyRequest(
        "put",
        "https://" +
          userData.shopUrl +
          "/admin/api/" +
          process.env.SHOPIFY_API_VERSION +
          "/metafields/" +
          userData.metafield.id +
          ".json",
        userData.accessToken,
        metafieldPayload
      );
    } else {
      return await handleshopifyRequest(
        "post",
        "https://" +
          userData.shopUrl +
          "/admin/api/" +
          process.env.SHOPIFY_API_VERSION +
          "/metafields.json",
        userData.accessToken,
        metafieldPayload
      );
    }
  } catch (error) {
    console.log("error-=-=-=-=-=-=", error.error);
    throw error;
  }
};

So in this my console log is completely fine! and If I take this console log URL and access token and try to create metafields from postman it is working correctly. however it is not working in my code only which I’ve provided.

Can you make handleshopifyRequest log the URL it’s hitting? If you’re sure about the auth token then I would guess that postman and your code are hitting different URLs.

here is the handleShopify logs
post
https://snippet-trps.myshopify.com/admin/api/2023-01/metafields.json

it is the same URL, I came to know new thing after this when I try to do same thing with windows there no error the same code is completely working.

But when I try in my macbook air m1 it is giving me the error. is this something causing trouble?

Finally I resolved an issue my self the issue with request library.
I used

axios

and it worked for me.