Developing Shopify Apps, Part 4: Change is Good
Share
So far, in the Developing Shopify Apps series, we've covered:
- The setup: joining Shopify's Partner Program, creating a new test shop, launching it, adding a private app to it and playing with a couple of quick API calls.
- Exploring the API: a quick explanation of the API and RESTafarianism, retrieving general information about a shop and dipping a toe into finding out about things like your shop's products, and so on.
- Even more explaration: REST consoles, getting a complete list of all the products, articles, blogs, customers and so on, retrieving specific items given their ID and creating new items.
Now these are modifications!
In this article, we're going to look at another important types of operation: modifying existing items.
Modifying Customers
To modify an object, we're going to need an existing one first. I'm going to start with "Peter Griffin", a customer that I created in the previous article in this series. His ID is 51827492, so we can retrieve his record thusly:
-
GET api-key:password@shop-url/admin/customers/51827492.xml
for the XML version -
GET api-key:password@shop-url/admin/customers/51827492.json
for the JSON version
Here's the response in XML:
<?xml version="1.0" encoding="UTF-8"?> <customer> <accepts-marketing type="boolean" nil="true" /> <orders-count type="integer">0</orders-count> <id type="integer">51827492</id> <note nil="true" /> <last-name>Griffin</last-name> <total-spent type="decimal">0.0</total-spent> <first-name>Peter</first-name> <email>peter.lowenbrau.griffin@giggity.com</email> <tags /> <addresses type="array"> <address> <city>Quahog</city> <company nil="true" /> <address1>31 Spooner Street</address1> <zip>02134</zip> <address2 nil="true" /> <country>United States</country> <phone>555-555-1212</phone> <last-name>Griffin</last-name> <province>Rhode Island</province> <first-name>Peter</first-name> <name>Peter Griffin</name> <province-code>RI</province-code> <country-code>US</country-code> </address> </addresses> </customer>
Let's suppose that Peter has decided to move to California. We'll need to update his address, and to do it programatically, we'll need the following:
- His customer ID (we've got that).
- The new information. For this example, it's
- address1: 800 Schwarzenegger Lane
- city: Los Angeles
- state: California
- zip: 90210
- phone: 555-888-9898
- And finally, the method for calling the Shopify API to modify existing items.
First, there's the format of the URL for modifying Peter's entry. The URL will specify what operation we want to perform (modify) and on which item (a customer whose ID is 51827492).
-
PUT api-key:password@shop-url/admin/customers/51827492.xml
for the XML version -
PUT api-key:password@shop-url/admin/customers/51827492.json
for the JSON version
For this example, we'll use the XML version. If you're using Chrome's REST console, put the XML URL into the Request field (located in the Target section), as shown below:
Then there's the message body, which will specify which fields we want to update. Here's the message body to update Peter's address to the new Los Angeles-based one shown above, in XML form:
<?xml version="1.0" encoding="UTF-8"?> <customer> <addresses type="array"> <address> <address1>800 Schwarzenegger Lane</address1> <city>Los Angeles</city> <province>CA</province> <country>US</country> <zip>90210</zip> <phone>555-888-9898</phone> </address> </addresses> </customer>
If you're using Chrome's REST console, put the message body in the RAW Body field (located in the Body section) and make sure Content-Type is set to application/xml:
Send the request. If you're using Chrome's REST Console, the simplest way to do this is to press the PUT button located at the bottom of the page. You should get a "200 OK" response and the following response body:
<?xml version="1.0" encoding="UTF-8"?> <customer> <accepts-marketing type="boolean" nil="true" /> <orders-count type="integer">0</orders-count> <id type="integer">51827492</id> <note nil="true" /> <last-name>Griffin</last-name> <total-spent type="decimal">0.0</total-spent> <first-name>Peter</first-name> <email>peter.lowenbrau.griffin@giggity.com</email> <tags /> <addresses type="array"> <address> <city>Los Angeles</city> <company nil="true" /> <address1>800 Schwarzenegger Lane</address1> <zip>90210</zip> <address2 nil="true" /> <country>United States</country> <phone>555-888-9898</phone> <last-name>Griffin</last-name> <province>California</province> <first-name>Peter</first-name> <name>Peter Griffin</name> <province-code>CA</province-code> <country-code>US</country-code> </address> </addresses> </customer>
As you can see, Peter's address has been updated.
Modifying Products
Let's try modifying an existing product in our store. Once again, we'll modify an item we created in the previous article: the Stumpy Pepys Toy Drum.
When we created it, we never specified any tags. We now want to add some tags to this product -- "Spinal Tap" and "rock" -- to make it easier to find. In order to do this, we need:
- The product ID. It's.
- The tags, "Spinal Tap" and "rock".
- And finally, the method for calling the Shopify API to modify existing items.
PUT api-key:password@shop-url/admin/products/48339792.xml for the XML version
PUT api-key:password@shop-url/admin/products/48339792.json for the JSON version
For this example, we'll use the JSON version. If you're using Chrome's REST console, put the XML URL into the Request field (located in the Target section), as shown below:
Then there's the message body, which will specify which fields we want to update. Here's the message body to add the tags to the drum's entry, in JSON form:
{ "product": { "tags": "Spinal Tap, rock", "id": 48339792 } }
If you're using Chrome's REST console, put the message body in the RAW Body field (located in the Body section) and make sure Content-Type is set to application/json:
Send the request. If you're using Chrome's REST Console, the simplest way to do this is to press the PUT button located at the bottom of the page. You should get a "200 OK" response and the following response body:
{ "product": { "body_html": "This drum is so good...\u003Cstrong\u003Eyou can't beat it!!\u003C/strong\u003E", "created_at": "2011-08-03T18:20:17-04:00", "handle": "stumpy-pepys-toy-drum-sp-1", "product_type": "drum", "template_suffix": null, "title": "Stumpy Pepys Toy Drum SP-1", "updated_at": "2011-08-08T17:57:55-04:00", "id": 48339792, "tags": "rock, Spinal Tap", "images": [], "variants": [{ "price": "0.00", "position": 1, "created_at": "2011-08-03T18:20:17-04:00", "title": "Default", "requires_shipping": true, "updated_at": "2011-08-03T18:20:17-04:00", "inventory_policy": "deny", "compare_at_price": null, "inventory_quantity": 1, "inventory_management": null, "taxable": true, "id": 113348882, "grams": 0, "sku": "", "option1": "Default", "option2": null, "fulfillment_service": "manual", "option3": null }], "published_at": "2011-08-03T18:20:17-04:00", "vendor": "Spinal Tap", "options": [{ "name": "Title" }] } }
Modifying Things with the Shopify API: The General Formula
As you've seen, whether you prefer to talk to the API with XML or JSON, modifying things requires:
- An HTTP PUT request to the right URL, which includes the ID of the item you want to modify
- The information that you want to add or update, which you format and put into the request body
...and that's it!
Next...
We've seen getting, adding, and modifying, which leaves...deleting.