How to create session for using Cron job for fetching data from Shopify

Girish_Rajwani
Shopify Partner
79 2 8

Hi guys,

 

I want to use cron job for some purpose but my query is how can I create the session for fetching data from Shopify?

 

Any help would be really appreciated.

 

Thank in advance:)

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
Replies 7 (7)

Not applicable

Hi @Girish_Rajwani,

 

After you install a Shopify app you will have an access_token. You can use that in your API calls when running your cron job.

 

So install a custom app or create a private app, then grab the access_token and start making calls to the Admin API.

 

Best,

Sam - Owner / Lead Developer

Achieve Applabs

Girish_Rajwani
Shopify Partner
79 2 8

Hey Sam,

 

Thanks for the reply. The app is public as i am not making for any specific client.

 

So will it work in public app?

 

Also, what would you suggest , cron job every 24 hours or Webhook when everytime order is placed?

 

Thanks 

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
Not applicable

Hi @Girish_Rajwani,

 

Yes should work the same for public apps.

 

chron job would fetch the access token from db then make API calls using a client.

 

Best,

Sam - Owner / Lead Developer

Achieve Applabs

Girish_Rajwani
Shopify Partner
79 2 8

Hi ,

 

which library to use to make session from access token ? can you please share link or sample code ?

 

Thanks

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani

SaulSimonett
Shopify Partner
2 0 0

Hi @Girish_Rajwani I'm currently facing the same problem. Have you come up with a solution?

Girish_Rajwani
Shopify Partner
79 2 8

Hey @SaulSimonett 

 

yes i found the solution and cron is working. kindly share proper error message or screenshot  so that i can properly .

 

thanks.

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
SaulSimonett
Shopify Partner
2 0 0

Yes. I usually fetch data from the frontend after is authenticated. Then in the backend I have the following code that creates a GraphQL client (it can be also REST) from the Frontend session.

 

 

function getApiClient(res) {
  return new shopify.api.clients.Graphql({
    session: res.locals.shopify.session,
  });
}

 

 

How did you create the session from the CronJob to fetch data from the API?

Thanks.