I modified the seed script from Square’s connect-api-examples repo. My version is here: square-express-shopping-cart-public/seed-catalog.js at main · manda-farmer/square-express-shopping-cart-public · GitHub
Make sure that you backup your catalog before running this script.
I just tested it with version 9.1 of the SDK and was able to push data to the catalog. It uses ES6-style imports, so make sure you run
~$ npm install esm --save
and append
-r esm
to your script like so:
{
"name": "square-seed-catalog",
"version": "0.0.1",
"private": true,
"scripts": {
"seed": "NODE_ENV=sandbox node -r esm ./seed-catalog.js generate",
"help": "NODE_ENV=sandbox node -r esm ./seed-catalog.js --help"
},
The script pulls data from file sample-seed-data.json. For your use case:
"#Crush It T-Shirt": {
"image": {
"url": ".Assets/crush-it.png",
"id": "#crushit",
"caption": "Crush It T-Shirt"
},
Here is the relevant section of the seed script:
import { Client, Environment, FileWrapper } from 'square';
import sampleData from './sample-seed-data.json';
import fs from 'fs';
import readline from 'readline';
import 'dotenv/config';
// We don't recommend to run this script in production environment
// Configure OAuth2 access token for authorization: oauth2
const config = {
environment: Environment.Sandbox,
accessToken: process.env.SQUARE_SANDBOX_ACCESS_TOKEN
};
// Configure catalog API instance
const { catalogApi } = new Client(config);
/*
* Given an object with image data and a corresponding catalogObjectId,
* calls the createCatalogImage API and uploads the image to the corresponding catalogObjectId.
* For more information on the createCatalogImage API, visit:
* https://developer.squareup.com/reference/square/catalog-api/create-catalog-image
* @param Object with Image information
* @param String catalogObjectId
*/
const addImages = async (image, catalogObjectId, success) => {
// Create JSON request with required image information requirements.
const request = {
idempotencyKey: require("crypto").randomBytes(64).toString("hex"),
objectId: catalogObjectId,
image: {
id: image.id,
type: "IMAGE",
imageData: {
name: image.name,
caption: image.caption,
},
},
};
const fileReadStream = fs.createReadStream(image.url);
const imageFile = new FileWrapper(fileReadStream, {
contentType: 'image/jpeg',
});
try {
const { result: { image } } = await catalogApi.createCatalogImage(request,imageFile);
success();
} catch (error) {
console.error("Image upload failed with error: ", error);
}
}
*
* Main driver for the script.
*/
const args = process.argv.slice(2);
if (args[0] == "generate") {
addImages();
} else if (args[0] == "-h" || args[0] == "--help") {
console.log(
"Please check the README.md for more information on how to run our catalog script.\nAvailable commands include:\n npm run seed - Generates catalog items for your sandbox catalog.\n npm run clear - Clears your sandbox catalog of all items.\n\n More information can also be found on our Quick Start guide at https://developer.squareup.com/docs/orders-api/quick-start/start."
);
} else {
console.log("Command not recognized. Please try again.");
}
Obviously, it will need some tweaking, but hopefully this points you in the right direction and will allow you to scale out your code a little more to add batches of images.