Why does 'No shop provided' error occur when making API calls on Shopify app development?

isferrei
Shopify Partner
1 0 0

I'm developing an app on shopify, but when I make api calls after the line 

app.use("/api/*", shopify.validateAuthenticatedSession()); of index file, it gets the issue "No shop provided". I could see that shop is undefined in payload. When I put my api calls before this line it works. I'm sharing the file code below.

isferrei_0-1680608718431.png


// @TS-check
import { join } from "path";
import { readFileSync } from "fs";
import express from "express";
import serveStatic from "serve-static";

import shopify from "./shopify.js";
import GDPRWebhookHandlers from "./gdpr.js";
import applyAttributesApiEndpoints from "./middleware/note-attributes-api.js";

const PORT = parseInt(process.env.BACKEND_PORT || process.env.PORT, 10);

const STATIC_PATH =
  process.env.NODE_ENV === "production"
    ? `${process.cwd()}/frontend/dist`
    : `${process.cwd()}/frontend/`;

const app = express();

// Set up Shopify authentication and webhook handling
app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  shopify.redirectToShopifyOrAppRoot()
);
app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers })
);

// If you are adding routes outside of the /api path, remember to
// also add a proxy rule for them in web/frontend/vite.config.js

app.use("/api/*", shopify.validateAuthenticatedSession());

app.use(express.json());

applyAttributesApiEndpoints(app);

app.use(serveStatic(STATIC_PATH, { index: false }));

app.use("/*", shopify.ensureInstalledOnShop(), async (_req, res, _next) => {
  return res
    .status(200)
    .set("Content-Type", "text/html")
    .send(readFileSync(join(STATIC_PATH, "index.html")));
});

app.listen(PORT);

 

Replies 2 (2)

Eliazar
Shopify Partner
2 0 0

Just happened to me because I forgot the "/" at the beginning of my request url.

 

this line ----> app.use("/api/*"shopify.validateAuthenticatedSession());

ensures your requests get automatic validation (headers and shit, API_key and whatnots, idk)

 

so just make sure every one of your requests start with "/api/", with both dashes in front and after...

 

noiseymur
Shopify Partner
20 2 8

Hello. I have the same issue. I've used slash before every api url. Any other ideas to try ?