Hi everyone,
I’m facing a CORS issue while calling an API endpoint from my Shopify Customer Account UI extension.
Here’s the API call I’m making from the extension:
const fetchCustomerId = async () => {
try {
const response = await fetch(‘https://peak-keith-manor-reduce.trycloudflare.com/api/customer’, { method: ‘GET’ });
if (!response.ok) {
console.error(‘Error fetching customer ID:’, response.statusText);
return null;
}
const data = await response.json();
return data.customerId;
} catch (error) {
console.error(‘Fetch Error:’, error);
return null;
}
};
// Get Customer ID
const customerId = await fetchCustomerId();
And here’s my API route (api.customer.jsx) in the Remix app:
import { json } from “@remix-run/node”;
export async function loader({ context }) {
const customer = await context.shopify.authenticated.customer();
return json(
{ customerId: customer?.id || null },
{
headers: {
“Access-Control-Allow-Origin”: “*”,
},
}
);
}
Even though I have added “Access-Control-Allow-Origin”: “*”, I’m still encountering CORS errors.
Has anyone faced a similar issue with Customer Account UI extensions? How can I properly handle CORS in this case?