Create a product with Ruby

Highlighted
Shopify Partner
12 0 2

Hi, I'm having trouble adding a product with a Ruby script. I'm building an uploaded plugin for Dropzone.

I'm new to Ruby so I'm not quite sure how to format the post request with the api credentials. In my code I have the correct credentials instead of "apikey", "password", and "myshopname".

 

require 'net/http'
require "rexml/document"
include REXML  # so that we don't have to prefix everything with REXML::...

doc = Document.new '<?xml version="1.0" encoding="UTF-8"?><product/>'
root = doc.root
type = root.add_element "product-type"
body = root.add_element "body"
title = root.add_element "title"
tags = root.add_element "tags"
vendor = root.add_element "vendor"
sku = root.add_element "sku"

type.text = "Art Print"
body.text = "Body text"
title.text = "Title text"
tags.text = "animal,water"
vendor.text = "My name"
sku.text = "123456"

shopify_url = 'https://apikey:password@myshopname.myshopify.com/admin/products.xml'

url = URI.parse(shopify_url)
request = Net::HTTP::Post.new(url.path)
request.content_type = 'text/xml'
request.body = doc
response = Net::HTTP.start(url.host) {|http| http.request(request)}

puts response

 

Response I'm getting is a "500 - Temporary Problem".

Thanks!

 

 

0 Likes
Tourist
69 0 3

In general you want to use ActiveResource for these things. Here's an example of creating a product:

 

require 'rubygems'
require 'active_resource'
require 'shopify_api'

ShopifyAPI::Session.setup(:api_key => '...', :secret => '...')
shop = 'christiansen-and-sons8103.myshopify.com'
token = '...'

ActiveResource::Base.site = ShopifyAPI::Session.new(shop, token).site

ShopifyAPI::Product.create(:handle => 'foo', :title => 'Foo')

I don't have a read/write API key handy, so you may have to add more attributes than handle and title, but that's the basic format you want to use.

 

Directed Edge recommendations app for Shopify: http://bit.ly/de-app
0 Likes
Shopify Partner
12 0 2

I'm actually not building an app in Rails though. This is a single ruby script running on it's own. Does that matter?

0 Likes
Shopify Staff
Shopify Staff
49 0 1

Hey there,

you can use just core Ruby to use the Shopify API (with e.g. REXML), but I recommend using ActiveResource (or directly the shopify_api) gem instead. You don't have to use all of Rails, just requiring 'active_resource' (and maybe 'active_support') should be enough

That said, let me show you how to make a successful call using just Ruby and Net/HTTP plus REXML. You were pretty much on the right path, just the part where the REXML::Document gets converted to a string was incorrect. I'm also not sure if Net/HTTP supports using the basic authentication directly inside the URL. Try this, that should work:

 

require 'net/http'
require "rexml/document"
include REXML # so that we don't have to prefix everything with REXML::...

API_KEY, PASSWORD = "your_api_key", "your_password"

doc = Document.new
doc << XMLDecl.new('1.0', 'UTF-8')

root = doc.add_element 'product'
type = root.add_element "product-type"
body = root.add_element "body"
title = root.add_element "title"
tags = root.add_element "tags"
vendor = root.add_element "vendor"
sku = root.add_element "sku"

type.text = "Art Print"
body.text = "Body text"
title.text = "Title text"
tags.text = "animal,water"
vendor.text = "My name"
sku.text = "123456"

shopify_url = 'https://your-shop-domain.myshopify.com/admin/products.xml'

# prepare a buffer that the document will be written to
xml_string = ''
# pretty print with 2 spaces as indentation
doc.write(xml_string, 2)
url = URI.parse(shopify_url)
request = Net::HTTP::Post.new(url.path)
# use basic authentication
request.basic_auth(API_KEY, PASSWORD)
request.body = xml_string
request.content_type = 'text/xml'
response = Net::HTTP.start(url.host) {|http| http.request(request)}

puts xml_string
puts response

 

 

Cheers, Dennis

Shopify Support Developer

Developer
0 Likes
Shopify Partner
12 0 2

Thank you so much. That works perfectly.

0 Likes