Creating session for external application

Hello Shopify community, I’m in my last semester at university and I’m starting a Shopify project in Next.js.
Maybe this question is a bit easy to solve, but like everything else, I’m new to Shopify and GraphQL.
I’m not sure why when I send my query to get my products I get the error “GraphQL Client: Not Found” is there any way to know if I’m on the right track?

This is how I’m doing my test

// app/api/prouducts/route.js
import “@shopify/shopify-api/adapters/node”;
import { shopifyApi, LATEST_API_VERSION, Session } from “@shopify/shopify-api”;

const shopify = shopifyApi({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET_KEY,
scopes: [“write_orders”, “read_products”],
hostName:
https://bc57-2806-102e-21-72fe-8557-a0f8-ed75-af14.ngrok-free.app/”,
apiVersion: LATEST_API_VERSION,
});
console.log(“shopify”, shopify);

export async function POST(params) {
const session = new Session({
id: “”,
shop: “bambulomas.myshopify.com”,
state: “”,
isOnline: false,
accessToken: process.env.SHOPIFY_ACCESS_TOKEN,
});
console.log(“session”, session);

const client = new shopify.clients.Graphql({ session });

console.log(“client”, client);

try {
const response = await client.request(
{ products (first: 10) { edges { node { id title descriptionHtml } } } }
);
console.log(response.data, response.extensions);
return response.data;
} catch (error) {
console.log(“Error read products”, error);
}
}

Hi Vicent,

Great to see you’re working on this university project :slight_smile: There’s a couple of things you can do here to help troubleshoot this issue:

  • First off, you can use the GraphiQL app to test queries. This will help narrow down where the issue is (eg: if it doesn’t return as expected you can tell the issue is with the query).
  • You can log full details of the error with console.error("Error reading products:", error.response?.errors || error.message);
  • Ensure the API key, secret key, and access token you are using have the appropriate scopes (read_products and write_orders) and have been granted during app installation, and confirm that SHOPIFY_API_KEY, SHOPIFY_API_SECRET_KEY, and SHOPIFY_ACCESS_TOKEN are correctly set in your environment. If using a .env file, double-check that it’s loaded properly.

Try the above and see if you can resolve the issue - if you’re still having problems post back here and we’ll look into this further.

1 Like

Hi Liam, thanks for answering my topic, I follow your recommendations.

First I made the same query in the GrapQl App as you suggested and this was the result:

Then I checked the permissions that the token has and they are these:

And when using the .env file, it does return the keys: