how to get the current session object for Admin API calls outside routes?

Kryp
Shopify Partner
1 0 3

Hello, 

I am new to Shopify, and I really can't find an answer to my dilemma.
I am trying to make a series of calls to the admin API (rest, GraphQL).

From what I understand from the documentation, every query abstraction needs a session object to get the accessToken and shop from like so.

 

const product = new shopify.rest.Product({session: session});

 

 

Since my app's scaffold was generated by the command:

npm init @shopify/app@latest

And after reading the generated code and trying to understand how everything works, the conclusion I got to is that the session object is constructed through the  middleware

 

shopify.validateAuthenticatedSession()

 

and added to the res.locals, so on every request from the frontend with app-bridge using authenticatedFetch() to make sure it is authenticated.

What I am trying to do is to query the store products every set interval of times using cron-node to do some job outside of any route something like:

 

cron.schedule('* * * 1 * *', function () {
  await titlesUpdator(session);
});

 

while titleUpdator() is a normal function that runs an admin API call,  and no matter how long I searched there seems to be no straightforward way to construct a session object from the sessionStorage (for now it's an SQLite database).

 

Any help on how to tackle that would be greatly appreciated.

Thank you so very much.

Replies 3 (3)

StevieW
Shopify Partner
12 1 6

I'm stuck on exactly the same thing.

 

Kryp, did you ever get a solution, or can anyone else help, please?

ChristianJ
Shopify Partner
2 0 3

I'm also new to Shopify, and so far it's been absolutely impossible for me to find an answer to this simple question, although I'm happy to see that I'm not alone. The "shopify-api-js" docs (on Github) is not particularly clear on the subject. I found however that I can do what I want with only an access token, using the "shopify-api-node" library.

ChristianJ
Shopify Partner
2 0 3

With this conversation I was able to make an offline / private app work: https://github.com/Shopify/shopify-api-js/issues/700#issuecomment-1410780918

 

To be clear, here are the values that I used: `apiKey`, `apiSecretKey` (this one confusingly corresponds to my access token in my case), and my shop name.