I’m trying to fetch products of a store via API using Remix for that I’m created a file api.products.jsx but I’m unable to get the session and it is throwing error.
Here is my code:
import shopify from ‘../shopify.server’;
import { json } from ‘@remix-run/node’;
export async function loader({ request }) {
try {
const { admin, session } = await shopify.authenticate.admin(request);
const data = await admin.rest.resources.Product.all({ session, limit: 200 });
// Extract only the necessary product data
const products = data.map(product => ({
id: product.id,
title: product.title,
image: product.image ? product.image.src : null
}));
return json(products);
} catch (error) {
console.error(“Error fetching products:”, error);
return json({ error: ‘Failed to fetch products’ }, { status: 500 });
}
}