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

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:)

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

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

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

Hi ,

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

Thanks

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

Hey @SaulSimonett

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

thanks.

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.