Share one Shopify instance across Express app
See original GitHub issueI’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:
- Visit a URL like: http://localhost:3000/?shopName=your-shop-name&apiKey=your-api-key&password=your-app-password
- API call works
- Visit the URL: http://localhost:3000/other-route
- API calls works
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:
- Created 5 years ago
- Comments:14

Top Related StackOverflow Question
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/
Yes, but I don’t see the problem. Can’t you check the session and use/set the
shopifyobject accordingly?If you need multiple
Shopifyobjects based on a session value, you can keep a map of them.