App reviews, troubleshooting, and recommendations
Hi, I am new to Shopify. Created an app and I have obtained the accessToken after initiating oAuth in my application. I am storing the accessToken in the database.
A background Job need to access the products from my store and update some details.
I am using Node 'shopify/shopify-api' package.
As per the documentation I need to call `new shopify.clients.Rest({session});`. I am not sure how can I get session.
Since I am in the Worker Job, there is no request context or cookies to get the session. Can you pls help?
//Example code from shopify Readme
const sessionId = await shopify.session.getCurrentId({ rawRequest: req, rawResponse: res, }); // use sessionId to retrieve session from app's session storage // getSessionFromStorage() must be provided by application const session = await getSessionFromStorage(sessionId); const client = new shopify.clients.Rest({session});
Hi @rmohan,
I hope you are doing well.
you are developing a worker job using access token, you should call GraphQL or REST API by Node's Fetch API instead of Shopify API package
here an example
try {
const query = `query {
products(first: 10) {
edges {
node {
id
title
}
}
}
}`
let response = await fetch(`https://${domain}/admin/api/2024-07/graphql.json`,{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': `${accessToken}`
}
});
let data = await response.json()
} catch (e) {
console.error(e.message);
}
If your worker job is running on the same server with your app, then first, you should know which type of session storage your app is using. It can be memory (no way to get) or a db like redis, SQLite (easy to connect). After you retrieve session from storage, parse it like this
import { Session } from "@shopify/shopify-api";
async function retrieveProducts(domain) {
...
// const sessionId = ... (it usually be `offline_${domain}`)
// const sessionObj = queryFromDB(sessionId);
const session = new Session(sessionObj);
...
}
I hope my information is useful to you. 🤗
B2B Wholesale Solution: Streamline your B2B operation with advanced features like wholesale registration forms, custom pricing.
B2B Portal, Quote, Net 30: Speed up purchasing and streamline your quotation process with advanced features like quick order, request for quote.
B2B Lock Password Protect: Easily control access to pages, products, and pricing with robust features.
BSS Commerce - Full-service eCommerce Agency I Use Shopify for 1$ in the first month now
The REST Admin API is legacy (reference). You should use GraphQL instead. You can use the unauthenticated admin (reference) for your background job. You don't need to obtain a session since the shopify.sessionStorage has the session access if you set your shopifyApp properly (reference) in the shopify.server file.
const { admin } = await shopify.unauthenticated.admin(shop);
const response = await admin.graphql(
`#graphql
query getProduct($id: ID!) {
product(id: $id) {
title
descriptionHtml
}
}`,
{
variables: { id: 'gid://shopify/Product/1234567890' },
},
);
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025