0
votes

I'm attempting to setup a Shopify recurring subscription app written in React/Node, but I get a FetchError when I call the following code from my server.js

The Error:

FetchError: request to https://[ACCESS_TOKEN]/admin/api/2020-10/graphql.json failed, reason: getaddrinfo ENOTFOUND [ACCESS_TOKEN]
  at ClientRequest.<anonymous> (/Users/cormachayden/Desktop/apps/easy-tok/node_modules/node-fetch/lib/index.js:1461:11)
  at ClientRequest.emit (events.js:315:20)
  at TLSSocket.socketErrorListener (_http_client.js:469:9)
  at TLSSocket.emit (events.js:315:20)
  at emitErrorNT (internal/streams/destroy.js:106:8)
  at emitErrorCloseNT (internal/streams/destroy.js:74:3)
  at processTicksAndRejections (internal/process/task_queues.js:80:21)

My Code:

  const getSubscriptionUrl = async (accessToken, shop, returnUrl = process.env.HOST) => {
  const subscirptionQuery = JSON.stringify({
    query: `mutation {
      appSubscriptionCreate(
        name: "Easy Tok Premium"
        lineItems: {
          plan: {
            appRecurringPricingDetails: {
              price: { amount: 4.0, currencyCode: USD}
            }
          }
        }
        test: true
        returnUrl: "https://${shop}.myshopify.com/admin/apps/easy-tok"
      ) {
        confirmationUrl
      }
    }`
  });

  const response = await fetch(`https://${shop}/admin/api/2020-10/graphql.json`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      "X-Shopify-Access-Token": accessToken,
    },
    body: subscirptionQuery
  })

  const responseJson = await response.json();
  return responseJson.data.appSubscriptionCreate.confirmationUrl;
};

module.exports = getSubscriptionUrl;

The same error occurs both locally and when I deploy to Heroku

1

1 Answers

1
votes

Your shop variable is returning your AccessToken and not shop_name.myshopify.com. So your request is becoming https://ACESS_TOKEN/admin/api/2020-10/graphql.json which is invalid.

Please double check your shop variable and where are you setting this and please generate a new access token immediately since you shouldn't provide your AccessToken publicly at any cost.

PS: I edited your question to remove the token for security reasons! (have in mind that the history will still keep it, so please change it)