Shopify app API returns HTML instead of JSON on server errors

I’m building a Shopify app using Remix. From the frontend, I make a POST request to my API expecting a JSON response. When the API fails (e.g., throws a 500 error), I catch it in my backend and return JSON like this:

return json(
  { 
    success: false, 
    error: error.message || "Failed to process request" 
  },
  { status: 500 }
);

However, in the browser, the response is an HTML document, not JSON.

I want to understand:

  1. Why Shopify returns HTML on API errors instead of the JSON I send.

  2. If there’s a recommended way to ensure the frontend always receives JSON, even when errors occur.

Any insights or best practices would be appreciated.

Hi @hassan,

App proxies aren’t exactly suitable for JSON APIs that need to return 5xx error status codes (4xx, 3xx, 2xx are unaffected).

If you need this functionality, I recommend returning a 200 response with error info in the body and the content type set to application/json.

1 Like