Upload product images

The following guide shows you how to write a script using Bootic’s official Ruby API client.

The script will:

1. Prepare images for upload

Directory structure

We’ll create a directory and put the images you want to upload inside.

Each image should be named after the product it belongs to, using the “slug” property.

For example, an JPG image file for a product named “Apple iPhone 6 Plus” should be called apple-iphone-6-plus.jpg.

TIP: you can download your products in CSV format from your shop's dashboard, and use it as a guide. The CSV file includes the "slug" field for all products.

We’ll write all our Ruby code in a file called upload_script.rb.

2. Configure the client.

In upload_script.rb, start by requiring the client library:

require 'bootic_client'

Then make sure to configure the client with your credentials. Read how to configure the Ruby client library to get the library ready.

3. Instantiate the client

The following creates a Bootic client instance for server-to-server scripts.

client = BooticClient.client(:client_credentials, scope: 'admin')

4. Load the root resource and your shop.

root = client.root

The root resource includes information on your current scope and the shops you own. From there you can navigate to other resources and take actions such as creating, updating or deleting products and their variants.

Now find your default shop:

shop = root.shops.first

5. Download and list all your products.

# all products in your shop
products = shop.products(status: 'all').full_set

Here, the client is using the products endpoint of the api, documented here, to fetch products of all statuses in your shop.

We get a filtered list of products, and then call #full_set on it, to turn it from a paginated list into the full list of available products.

We’ll start by just printing the product titles and exiting the script.

products.each do |product|
  puts product.title
end

You can run the script with:


ruby upload_script.rb

# Example output
Apple iPhone 6 Plus
Apple iPhone 6
Samsung Galaxy S7
Samsung Galaxy S6
Nokia Lumia 960

6. Find available images.

Now, we’ll use the “slug” attribute of each product to find matching image files.

products.each do |product|
  # ex. "apple-iphone-6-plus.jpg"
  file_name = "#{product.slug}.jpg"
  file_path = File.expand_path(file_name)

  if File.exists?(file_path)
    puts "#{file_name} exists! Uploading file..."
  else
    puts "No images for product #{product.slug}"
  end
end

Output:


apple-iphone-6-plus.jpg exists! Uploading file...
apple-iphone-6.jpg exists! Uploading file...
samsung-galaxy-S7.jpg exists! Uploading file...
samsung-galaxy-S6.jpg exists! Uploading file...
No images for product nokia-lumia-960

7. Upload images

Great, not we just need to actually upload the images!

products.each do |product|

  # ex. "apple-iphone-6-plus.jpg"
  file_name = "#{product.slug}.jpg"
  file_path = File.expand_path(File.join(".", "images", file_name)

  if File.exists?(file_path) && product.has?(:create_product_asset)
    puts "#{file_name} exists! Uploading file..."
    image_file = File.new(file_path)
    image = product.create_product_asset(
      file_name: file_name,
      data: image_file,
      title: product.title
    )

    if image.has?(:errors)
      puts "Error uploading image for #{product.slug}: #{image.errors.first.messages.first}"
    else
      puts "Uploaded image for #{product.slug}"
    end
  else
    puts "No images for product #{product.slug}"
  end
end

First, we use the file path in your file system to instantiate File Ruby object.
The we use the file object to call #create_product_asset on the product and upload the file data.

# An instance of File
# http://ruby-doc.org/core-2.2.0/File.html
image_file = File.new(file_path)

# Pass the file to #create_product_asset
# https://developers.bootic.net/rels/create_product_asset/
image = product.create_product_asset(
  file_name: file_name,
  data: image_file,
  title: product.title
)

file_name and data are mandatory. title is optional. Here, we use the product’s title as the image title.

You can now go and check your Bootic shop to see if the images were uploaded.

The Ruby client library supports passing a File object since version 0.0.14. Make sure you're using the latest version!

The end

That’s it. The full script is here.

What about deleting product images? You can find an example script here