Get products using Offline accessToken

Topic summary

A developer is building a Shopify app with a background worker job that needs to access and update product data using an offline access token stored in a database. The challenge is obtaining a session object for the Shopify API client without request context or cookies.

Proposed Solutions:

  • Direct API calls: Use Node’s Fetch API to call GraphQL or REST endpoints directly with the access token in headers, bypassing the Shopify package’s session requirement
  • Session reconstruction: If the worker runs on the same server, retrieve the session from storage (Redis, SQLite) using the session ID (typically offline_${domain}) and reconstruct it using Shopify’s Session class
  • Unauthenticated admin API (recommended): Use shopify.unauthenticated.admin(shop) which accesses sessions through shopify.sessionStorage if properly configured, eliminating manual session handling

Key Technical Notes:

  • REST Admin API is legacy; GraphQL is preferred
  • The unauthenticated admin approach requires proper shopifyApp configuration with session storage setup
  • Code examples provided for both direct GraphQL queries and the unauthenticated admin method
Summarized with AI on October 31. AI used: claude-sonnet-4-5-20250929.

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. :hugs:

1 Like

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' },
  },
);
1 Like

thanks @suphero , you’re indeed a hero with this reply.