Focuses on API authentication, access scopes, and permission management.
I already have built a custom app using the latest Node template (https://github.com/Shopify/shopify-app-template-node). I installed it in the development store and distributed it in another store. Now I am in need of an offline access token to use Shopify admin API.
Please guide me in obtaining an offline access token for my custom app; I will securely store it for usage.
Thanks in advance 🙂
Hey @rousnay,
To perform an offline task (like processing a webhook), you can do something like:
async function someOfflineProcess() {
const sessionId = await shopify.api.session.getOfflineId(shop)
const session = await shopify.config.sessionStorage.loadSession(sessionId);
const client = new shopify.api.clients.Rest({session});
// Use the client, e.g. update a product:
const products = await client.put({
path: `products/${product.id}.json`,
data,
});
}
Let me know if you get stuck!
Scott | Developer Advocate @ Shopify
@SBD_ Thank you so much for your reply, yes I am trying to process a webhook (fulfillment notification) from another server.
I tried with the code above, but giving an error in 'shop' as an argument, I tried adding the shop URL directly ("iplaysafe-consultancy-app-store.myshopify.com") as an argument, but it's not working, (InvalidShopError: Received invalid shop argument).
🤔 That should do it. Can I please see your package.json?
Scott | Developer Advocate @ Shopify
const sessionId = await shopify.api.session.getOfflineId(shop) how to get shop here?
Hey @FaizaBashir
`shop` is the myshopify url, for example:
const sessionId = await shopify.api.session.getOfflineId('example.myshopify.com')
Scott | Developer Advocate @ Shopify
thank you for your response. How to get my shopify url. I am struggling with getting shop url and access token I tried many solutions but nothing seems to work. I am working on Shopify app with node and react following documentation. How to get access token and shop url in index.js or middleware?
Thanks a lot. Following the solution you provided, I have successfully got access token. But I hardcoded the shop url. How to get shop url?
Given a webhook is triggering this, you could pull the shop from the webhook headers (`X-Shopify-Shop-Domain`). Be sure to verify the webhook before trusting the value.
Scott | Developer Advocate @ Shopify
Thanks a million. I am new to shopify app develpment and I am wrking on a test app for learning. No, its noot being triegered by webhook. Can you please share example of getting shop url both with and without webhook?
Hey @FaizaBashir
Our wires might be getting crosses - can you please provide a description of the app you're building / what you need the token for?
Also be sure to check out the tutorial to familiarize yourself with the Shopify App concepts: https://shopify.dev/docs/apps/getting-started/build-app-example
Scott | Developer Advocate @ Shopify
@SBD_ I am working on a test wishlist app with node and react for learning. I am using script tag api. I am able to get access token but how to get shop url here?
The shop can be obtained from the session:
app.post("/api/install", async (_req, res) => {
console.log(res.locals.shopify.session.shop);
});
The app template comes with REST and GraphQL clients, so you don't need to manually construct the requests with Axios. Here's an example of creating a script tag with the REST client:
app.post("/api/install", async (_req, res) => {
const client = new shopify.api.clients.Rest({session: res.locals.shopify.session});
await client.post({
path: "script_tags",
data: {"script_tag":{"event":"onload","src":"https://some-example.com/script.js"}},
});
...
});
Scott | Developer Advocate @ Shopify
@SBD_ thanks a million. I tried the code you provided but its not being called. I am using express. I tried both in index.js and middleware but this is not executed.
Hey @FaizaBashir
That's just an example route. You'll need something on the app's frontend (like a 'Install tags" button) to hit the route with a fetch.
Scott | Developer Advocate @ Shopify
hey @SBD_ is there no way we could call a route in backend? if so how can we call a route in backend pleae share eample.
To do it automatically, you could run the logic after authentication, something like:
app.get(
shopify.config.auth.callbackPath,
shopify.auth.callback(),
installScriptTags(),
shopify.redirectToShopifyOrAppRoot()
);
Scott | Developer Advocate @ Shopify
here is my index.js code
I see this endpoint in index.js inside createServer function. How to call this in index.js or middleware? When i call this endpoint with axios i get eeror invlid url.
Could you please help me. I searched alot but couldnt find solution. How i get shop url? I am stuck please help
Could you please help me? How to get shop url in backend? in index.js or middleware. I want to call script tag api how to get shop here