Skip to content

Instantly share code, notes, and snippets.

@BigWillie
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigWillie/74388fbc4c866cb1129b to your computer and use it in GitHub Desktop.
Save BigWillie/74388fbc4c866cb1129b to your computer and use it in GitHub Desktop.
var fs = require('fs');
var parse = require('csv-parse');
var _ = require('lodash');
var needle = require('needle');
// First you need to create a private app in shopify.
// From this - you'll get a load of secret stuff, an API key and a password
var shopname = 'foo'; // eg: foo.myshopify.com
var secretStuff = 'API-KEY-GOES-HERE:PASSWORD-GOES-IN-HERE';
var parser = parse({
delimiter: ','
}, function(err, data) {
var i = 0;
_.forEach(data, function(n) {
i++;
// Timeout to delay our api calls to Shopify.
// because if we fire everything at once,
// shopify will tell us to sod off.
setTimeout(function() {
// In this example, we build a product object, and
// populate it with data from our csv array.
// Where bits are missing, we fill in
// with placeholders... 'Please Edit' etc...
var product = {
"product": {
"title": !!n[3] ? n[3] : 'Please add',
"body_html": !!n[4] ? n[4] : 'Please Edit',
"vendor": !!n[0] ? n[0] : 'Unknown',
"product_type": !!n[2] ? n[2] : 'Unknown',
"published": true
}
}
// Then use needle to send our product up to Shopify
needle.post('https://' + secretStuff + '@' + shopname + '.myshopify.com/admin/products.json', product, function(err, resp) {
// you can pass params as a string or as an object.
if (err) {
console.log('Error', err)
}
// If you want, you can do something with the respose (returned by Shopify) here
});
}, 1500 * i)
});
});
// Read data from CSV file... parse data
fs.createReadStream(__dirname + '/file.csv').pipe(parser);
@BigWillie
Copy link
Author

A node.js script to import products into Shopify (could also be adapted for other interactions with Shopify's api). You'll need to npm install csv-parse, lodash and needle. The comments explain it all. Run from the terminal: node shopify_product_import.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment