Get products using Offline accessToken

Get products using Offline accessToken

rmohan
Shopify Partner
1 0 0

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});

 

Replies 3 (3)

BSSCommerce-B2B
Shopify Partner
1972 564 568

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

BSSCommerceB2B_0-1724285049911.png

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

suphero
Shopify Partner
15 2 3

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

 

 

ranrub
Shopify Partner
3 0 0

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