Closed zohaibarsalan closed 10 months ago
Thank you for the kind words! Can you link me the docs you're following? Also, what's your API version?
Can you try out this snippet:
import shopify from "utils/shopify.js" //Update this value to match your path
const asset = new shopify.rest.Asset({ session: res.locals.user_session }); //Double check verifyRequest middleware to ensure this value exists
asset.theme_id = themeid; //replace with theme id
asset.key = `${folder}/${fileName}`; // example: assets/script.js
asset.value = "<p>Value goes here</p>";
await asset.save({ update: true });
I tried out your snippet:
userRoutes.get("/getTheme", async (req, res) => {
console.log("GET Themes API Hit");
const { client } = await clientProvider.restClient({
req,
res,
isOnline: true,
});
const getTheme = await client.get({ path: "themes" });
const themeID = getTheme.body.themes[0].id;
const themeName = crypto.createHash("sha-256", getTheme.body.themes[0].name);
console.log(getTheme.body.themes[0]);
const getAssets = await client.get({ path: `themes/${themeID}/assets` });
console.log("PUT Assets API Hit");
const asset = new shopify.rest.Asset({ session: res.locals.user_session });
asset.theme_id = themeID;
asset.key = `sections/announcement-bar.liquid`;
asset.value =
"<p>This site is under works. Please try again in a few hour</p>";
await asset.save({ update: true });
console.log(asset);
return res.status(200).json({ theme: getTheme.body.themes });
});
I get this error now:
I have imported the shopify
2023-07
(to test test)@shopify/shopify-api
package version? I'm on 8.1.1
1) Downgraded to 2023-07
Getting the same error:
[server] const asset = new shopify.rest.Asset({ session: res.locals.user_session });
[server] ^
[server]
[server] TypeError: shopify.rest.Asset is not a constructor
[server] at file:///Users/zohaibarsalan/Developer/bannerito/server/routes/index.js:32:17
[server] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I am also on version 8.1.1
This is the shopify.js
import { DeliveryMethod, shopifyApi } from "@shopify/shopify-api";
import "dotenv/config";
import appUninstallHandler from "../server/webhooks/app_uninstalled.js";
const isDev = process.env.NODE_ENV === "dev";
// Setup Shopify configuration
const shopify = shopifyApi({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET,
scopes: process.env.SHOPIFY_API_SCOPES,
hostName: process.env.SHOPIFY_APP_URL.replace(/https:\/\//, ""),
hostScheme: "https",
apiVersion: process.env.SHOPIFY_API_VERSION,
isEmbeddedApp: true,
logger: { level: isDev ? 1 : 0 }, //Error = 0,Warning = 1,Info = 2,Debug = 3
});
shopify.webhooks.addHandlers({
APP_UNINSTALLED: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: "/api/webhooks/app_uninstalled",
callback: appUninstallHandler,
},
});
export default shopify;
Is it possible for you to invite me to this repo?
Alright
Sent you the invite
OK I see the problem - the snippet I sent is fine but I forgot to add restResources
in shopify.js
.
import { restResources } from "@shopify/shopify-api/rest/admin/2023-07"; // <------ Add this
const shopify = shopifyApi({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET,
scopes: process.env.SHOPIFY_API_SCOPES,
hostName: process.env.SHOPIFY_APP_URL.replace(/https:\/\//, ""),
hostScheme: "https",
apiVersion: process.env.SHOPIFY_API_VERSION,
isEmbeddedApp: true,
restResources, // <------ Add this
logger: { level: isDev ? 1 : 0 }, //Error = 0,Warning = 1,Info = 2,Debug = 3
})
Also just a heads up, if you don't have permissions for using Asset API, you'll get the 404
error from Shopify. If you downgrade to 2023-07
this should be fine because from 2023-10
you need perms. Get in touch with Shopify support for permissions.
Thanks mate the original error got resolved, but I now have another error 😢. I really appreciate your help and am very thankful to you man. I haven't come across a guy such as yourself, who resolved my issue so fast and responded within an hour of my first comment.
I am getting this now:
[server] /Users/zohaibarsalan/Developer/bannerito/node_modules/@shopify/shopify-api/lib/clients/http_client/http_client.js:193
[server] throw new ShopifyErrors.HttpResponseError({
[server] ^
[server]
[server] HttpResponseError: Received an error response (404 Not Found) from Shopify:
[server] "Not Found"
[server] If you report this error, please include this id: 85ce4a5c-2059-4ad7-966a-d6a72501c1e5
[server] at NewRestClient.throwFailedRequest (/Users/zohaibarsalan/Developer/bannerito/node_modules/@shopify/shopify-api/lib/clients/http_client/http_client.js:193:23)
[server] at NewRestClient.<anonymous> (/Users/zohaibarsalan/Developer/bannerito/node_modules/@shopify/shopify-api/lib/clients/http_client/http_client.js:220:22)
[server] at Generator.next (<anonymous>)
[server] at fulfilled (/Users/zohaibarsalan/Developer/bannerito/node_modules/tslib/tslib.js:166:62)
[server] at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[server] response: {
[server] code: 404,
[server] statusText: 'Not Found',
[server] body: { errors: 'Not Found' },
Do you reckon this is something to do with the asset.key? I tried changing the key but I got the same error. I am pushing the repo with the current changes made, you can have a look there.
And one more time, thanks, man.
Also just a heads up, if you don't have permissions for using Asset API, you'll get the
404
error from Shopify. If you downgrade to2023-07
this should be fine because from2023-10
you need perms. Get in touch with Shopify support for permissions.
This is exactly what I sent in, I think you missed it^
Edit: Double check the REST call from the docs. I think you were missing the restResources
so your older code should theoretically work. I don't work with the asset API at all so my knowledge on this could be flimsy^
But I am using 2023-07
I even reinstalled the app to make sure it was 2023-07
Do you have any better way to interact with the storefront or the merchant's online store? The best way I found was using assets API, if there is any better way I would gladly take it
The PUT
request doesn't seem to be working nor is the DEL
one. I tried the previous code and the new one, but nothing works. Sticking with the code you gave me. The error remains even after I switch my API versions. Any idea why this is happening? I just want to be able to add a code snippet in the merchant's online store, and this is taking so much time and effort. If this doesn't work and there is a better way can you please tell me? I would appreciate this mate. So far you have been a godsend. I am going to push the repo, please have a look. Even though you don't work with REST you are still better than me. @kinngh
Sorry closing the repo - this is about as far as I can spend time on^
Hey mate,
First off thanks for the template, this is far more understandable and better than the ones Shopify generates, even though they are great I don't understand them. Now for the problem at hand.
I am trying to use the REST Client to do a
PUT
request, but I am not getting what I want. Meaning, I am not getting an error nor am I getting an actual result. I am first doing aGET
request, which works perfectly, to get the theme id and then trying to upload a snippet to the assets API. This is because I need to make some changes to the online storefront or the merchant's website.This is my code:
Here is what I get (I should not be getting this)
What I should be getting based on the REST Docs:
I would appreciate the help. I don't know if it's my error instead of the boilerplates and I would be really glad if you could just spare me a few minutes and help me with this problem. I have been at this for 2 days and I am not progressing forward.
Thanks