← Integrations

No-code / CMS · JavaScript

Gemstone suggestions on a Shopify store, through an app proxy

Stores that sell gemstones, rudraksha or puja items often want a "what suits my chart" step. Theme code is public, so the AstroNode call has to live on your app's server; Shopify's app proxy lets the storefront reach that server on the shop's own domain. Here is the server side, executed with a signed request.

Updated

How this page was checked: The signature check and handler were executed on Node 24 against the in-memory API with a query signed the way Shopify signs app-proxy requests; changing one parameter after signing returned 403. Shopify itself (the proxy hop, the theme fetch) was not exercised.

How the request flows

  • In your app's configuration, an app proxy maps a storefront path such as /apps/astro to your server, e.g. https://app.example.com/proxy.
  • Theme JavaScript calls /apps/astro/gemstone?date=...&time=...&tz=...&lat=...&lng=... on the shop's own domain - no CORS, no key.
  • Shopify forwards the request to your server and adds shop, path_prefix, timestamp, logged_in_customer_id and a signature to the query string.
  • Your server verifies the signature, calls AstroNode with its key, and returns JSON the theme renders.

The server

proxy.js - a web-standard Request → Response handler, mountable in any fetch-style server:

js
import { createHmac, timingSafeEqual } from "node:crypto";

const ASTRONODE = process.env.ASTRONODE_BASE_URL ?? "https://api.astronode.dev";

// Shopify signs every proxied request with your app's API secret.
export function isFromShopify(url, secret) {
  const params = new URL(url).searchParams;
  const signature = params.get("signature") ?? "";
  const message = [...new Set(params.keys())]
    .filter((k) => k !== "signature")
    .map((k) => `${k}=${params.getAll(k).join(",")}`)
    .sort()
    .join("");
  const digest = createHmac("sha256", secret).update(message).digest("hex");
  return signature.length === digest.length && timingSafeEqual(Buffer.from(signature), Buffer.from(digest));
}

// Map the engine's stone ids to your product handles.
const PRODUCT_FOR = {
  "mercury.emerald": "natural-emerald-panna",
  "mercury.peridot": "peridot-ring",
};

export async function gemstoneHandler(req) {
  if (!isFromShopify(req.url, process.env.SHOPIFY_API_SECRET)) {
    return new Response("forbidden", { status: 403 });
  }
  const q = new URL(req.url).searchParams;
  const res = await fetch(`${ASTRONODE}/v1/remedies/gemstone`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.ASTRONODE_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      date: q.get("date"),
      time: q.get("time"),
      tz_offset_minutes: Number(q.get("tz")),
      lat: Number(q.get("lat")),
      lng: Number(q.get("lng")),
    }),
  });
  const json = await res.json();
  if (!res.ok) return Response.json({ error: json.error.code }, { status: res.status });
  const stones = [json.data.primary, ...json.data.secondary];
  return Response.json(
    stones.map((s) => ({ name: s.name, planet: s.planet, rank: s.rank, product: PRODUCT_FOR[s.id] ?? null })),
  );
}

Response for the sample birth (Virgo ascendant, so the lagna lord is Mercury):

json
[
  { "name": "Emerald (Panna)",   "planet": "mercury", "rank": "primary",    "product": "natural-emerald-panna" },
  { "name": "Peridot (Olivine)", "planet": "mercury", "rank": "secondary",  "product": "peridot-ring" },
  { "name": "Green Tourmaline",  "planet": "mercury", "rank": "tertiary",   "product": null },
  { "name": "Green Jade",        "planet": "mercury", "rank": "quaternary", "product": null }
]

Map on the stable id (mercury.emerald), not the display name. The full response also gives each stone's source text, metals and contraindications ("Avoid when Mercury is combust.") - show the contraindications next to any product you recommend, and keep the recommendation a suggestion rather than a claim.

The mistakes that bite first

  • The key in theme code or a metafield. Theme assets and storefront-readable metafields are public. The key belongs only in your app server's environment.
  • Trusting the query string. Anyone can call your proxy URL directly. Without the signature check, logged_in_customer_id is whatever the caller typed; with it, a parameter changed after signing fails (403 in the run above). Compare digests with timingSafeEqual, not ===.
  • Expecting the customer's session. Shopify does not forward the storefront's cookies through the proxy. Identify the customer by the signed logged_in_customer_id (empty when nobody is logged in) and keep birth details in your own store or a customer metafield your server reads.

Questions

Which AstroNode endpoints does the Shopify example call?
1 endpoint: Gemstone Recommendation (POST /v1/remedies/gemstone).
Was the Shopify code on this page actually run?
The signature check and handler were executed on Node 24 against the in-memory API with a query signed the way Shopify signs app-proxy requests; changing one parameter after signing returned 403. Shopify itself (the proxy hop, the theme fetch) was not exercised.