Home » Solution: CORS Error When Consuming External APIs from Shopify Templates
Web & Code

Solution: CORS Error When Consuming External APIs from Shopify Templates

Quick Diagnostics

To permanently fix the Shopify CORS policy in your store when the Access-Control-Allow-Origin (CORS) error appears in the browser console when trying to make a fetch request from a script in your Shopify template to your own external server (for example, to load digital planners or custom diaries from an external database), you must configure authorization at the source. The browser blocks the response because the external server does not explicitly authorize your Shopify store's domain to read its data.

🚀 Cómo solucionar el error paso a paso

Paso 1: Configurar las cabeceras en tu servidor externo

The actual solution is not done inside Shopify, but rather on the backend server receiving the request. You must respond by adding the corresponding CORS header.

  • If you are using Node.js (Express), install and configure the official middleware:
    BASH
    npm install cors
    
    Then apply it, restricting access only to your store:
    JAVASCRIPT
    const cors = require('cors');
    app.use(cors({
      origin: 'https://tu-tienda-shopify.com'
    }));
    

Paso 2: Utilizar los Proxies de Aplicación de Shopify (Alternative Premium)

If you do not have access to modify the external server, you can use Shopify's App Proxy. This makes your script query Shopify's own servers directly, and they make the request on the backend, completely bypassing browser CORS restrictions.

🛡️ Consejo de Prevención

Recommended safety practices:

  • Never use the wildcard asterisk Access-Control-Allow-Origin: * on production servers that handle confidential user data or paid digital downloads. Doing so opens the door for any malicious website to make requests on behalf of your users and steal information from your database.

Prevention Advice

Recommended security practices: