question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Share one Shopify instance across Express app

See original GitHub issue

I’m developing a Shopify app which uses Express and this module for API calls and I’ve run into trouble sharing one Shopify instance across my routes. Here’s a stripped down version of what I’m trying to achieve:

var express = require("express");
var Shopify = require("shopify-api-node");
var app = express();

// Declare at highest scope so all routes can reference it
var shopify;

app.get("/", (req, res) => {
  var { shopName, apiKey, password } = req.query;

  // Re-declare to a Shopify instance using data from query string parameter
  shopify = new Shopify({ shopName, apiKey, password });

  shopify.shop.get()
    .then((shop) => {
      return res.send(JSON.stringify(shop));
    })
    .catch(() => {
      return res.status(500).send("Something broke.");
    });
});

app.get("/other-route", (req, res) => {
  shopify.product.list()
    .then((products) => {
      return res.send(JSON.stringify(products));
    })
    .catch(() => {
      return res.status(500).send("Something broke.");
    });
});

app.listen(3000, () => {
  console.log("Example app listening on port 3000!")
});

That actually works like how I want it to:

What I’m struggling with is eventually all routes will be in separate files, how could I share that one Shopify instance between all files?

In my actual non-stripped-down app I’m using express-session, I tried storing the Shopify instance in that but that only takes JSON-serializable data so that doesn’t work. Is there an Express-best practice I’ve missed when it comes to storing instances across routes/session?

Right now I’m creating a new Shopify instance for each route which isn’t ideal, it works but eventually I’ll run into problems with the API call limit, I’d prefer to use one Shopify instance across all of my routes with autoLimit: true and not have to worry about it, any help would be greatly appreciated.

PS: Thanks once again for this module, it’s great.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
tomblanchardcommented, Jun 11, 2018

FYI: In case anyone is interested in reading about this more in-depth, I blogged about this here:

https://tomblanchard.co.uk/storing-instances-in-express-sessions/

1reaction
lpincacommented, Jun 7, 2018

Yes, but I don’t see the problem. Can’t you check the session and use/set the shopify object accordingly?

If you need multiple Shopify objects based on a session value, you can keep a map of them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building Your First Shopify App with Node.js and Express ...
In this free one -hour webinar hosted by Developer API Support Lead Andrew McCauley, you'll learn how to create a basic and functional ......
Read more >
node.js - Share third-party API rate limit throughout session
I'm using the Shopify-api-node module to make my API calls, it supports an autoLimit option out of the box which automatically limits the...
Read more >
Getting started with Shopify App Bridge
By default, all new apps are embedded in the Shopify admin. You can embed an existing app with the following steps: From your...
Read more >
How to Authenticate Your Embedded App in the Shopify Admin
In the Shopify ecosystem, embedded apps run in an iframe in a production environment. In order to test the functionality of your app...
Read more >
Shopify Node API: How to Integrate It in Your App - Fusebit
Setting Up a Shopify Node API Project · mkdir Shopify-node-API cd Shopify-node-API npm init -y · npm install express dotenv typescript @types/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found