Quantcast
Channel: Systems Integration – Demac Media
Viewing all articles
Browse latest Browse all 16

Mini Tutorial: Uploading Products in Shopify with REST and C#

$
0
0

Demac Media prides itself in providing quality wherever it sees potential for growth in the eCommerce sector. Recently, Demac partnered with Shopify to help small businesses create a niche for their products using Shopify’s convenient and easy-to-use eCommerce platform. eCommerce is about your products and their online presence, and if you already have a successful business you naturally want your products on your Shopify store, as you look forward to scale.
Typically, these products are uploaded through a CSV file into your Shopify store. But, for businesses who maintain their products and inventory in different file formats, we have designed a template that works around business logic to upload those products.. For example, a company might have their product records in an XML format. Such a format would be hard to parse and input manually into a CSV format that is acceptable by Shopify, especially if you have thousands of records. Demac’s integration team can parse such formats and upload the products based on the requirements of the client’s needs. Moreover, the system can also be integrating with various ERP systems as well.

Shopify uses a REST API that allows developers to communicate with a specific Shopify store. In this tutorial, I would like to share some of the tools I used to develop the integration system for Shopify. Also, I would like to share a small function that makes a REST call to add a product to Shopify using REST Sharp. Sounds exciting, doesn’t it. But before we continue, I would like to explain what a typical Shopify product consists of. This is how a particular Shopify product will look like in JSON format:

{
  "product": {
    "title": "Burton Custom Freestlye 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "variants": [
      {
        "option1": "First",
        "price": "10.00",
        "sku": "123"
      },
      {
        "option1": "Second",
        "price": "20.00",
        "sku": "123"
      }
    ]
  }
}

Variant products can be thought of as simple products in Magento and they have various fields that describe the product.

In the above JSON example the option1 field helps the store owner define the variant product. These options can contain values specifying colour and sizes etc. For example, if the two variant products above vary by colour, then the values would be colour values such as Blue or Pink. Similarly, the option value can be Large or Small if the product varies by size.

A given product can have various options (option1, option2 and option3 ) but each option values must be unique otherwise your variant product would not be created. Also, you can only have a maximum of three options for a given variant product.

There are other properties that can be used to make a Shopify product. But for the purposes of this tutorial, I will keep it simple and use the above example throughout. While working with REST services I found that an easy-to-use REST client can be really useful. I used “Advance REST Client,” a Google Chrome extension, to help me work with Shopify’s REST calls. For example, to add a product to Shopify, you simply need to fill in the following fields:

From the select boxes, select the POST option as you will be posting a product into your store. The second text box you should add the url of your site and the resource you are attempting to access. In the next text box add the Shopify store key that is provided to you by Shopify. Don’t forget to add the ‘X-Shopify-Access-Token’ header on all your requests. The third text box is where you enter the JSON string or the Shopify product you are trying to create.

When you have filled all these fields, click send and you will get a response string that will confirm if the product is created. Shopify returns a “Created” status if the product is successfully created. You can see the the return status in green in the image above.

Another convenient tool to use is Fiddler. Fiddler is a free web debugging tool that helps you analyze your HTTP(S) traffic. You can use it to check if your REST request returned an expected response. The tool is similar to WireShark but I find Fiddler easy to use. For example, for the given POST request above, Fiddler captures the following.

The box above is the product that you want to create in JSON format. The box below contains the response content returned by Shopify. This is a Shopify product that Shopify created based on the data in the JSON string. As can be seen, Shopify created a product_id automatically. This is a unique ID assigned to a Shopify product. This ID can be used to get a particular product from Shopify method by invoking a GET request.

Although the above tools are great at analyzing various REST and SOAP responses, there are libraries available that can help you programme your REST calls. REST Sharp is a handy library that can help developers make these REST calls. The following example will help explain how various methods of the library works. You can check out the rest of their documentation here.

private string AddProduct(string shopifyProduct)
{
     shopifyProduct = "{" + "\"product\":" + shopifyProduct + "}";
     var client = new RestClient(“https://yourStoreURL”);
     var request = new RestRequest("/admin/products.json", Method.POST);
     request.AddHeader("X-Shopify-Access-Token", “Your stores API Key”);
     request.AddParameter("application/json; charset=utf-8", shopifyProduct,   ParameterType.RequestBody);
     request.RequestFormat = DataFormat.Json;
     var restResponse = client.Execute(request);

     return restResponse.StatusCode.ToString();
}

The AddProduct method takes in a JSON string that has the properties of a Shopify product as described above. The function returns the status of the response returned from Shopify. If the product is successfully created, then Shopify returns “Created” as the status code for the request.

Initially, you need to instantiate the RestClient class and pass the base URL of your Shopify store as a parameter. To give reference of the resource that needs to be used, you need to pass the address of the resource. Since, in our case we need to add a product, we will pass “/admin/products.json” as the address of the resource with the request method, which in this case is POST.

As mentioned above it is important that your request is authenticated, so make sure that you have your API key assigned in the X-Shopify-Access-Token header. The Execute method takes a RestRequest object as input and returns an IRestResponse object, which contains the status code returned by Shopify. To analyze your REST Sharp request you can use Fiddler and see if your request has the correct parameters and if the response is the one that you expected.

These tools are very useful when working with WebServices and helped me a lot in understanding and working with Shopify’s REST API. I hope it helps you too.

Until next time, Happy Coding!

The post Mini Tutorial: Uploading Products in Shopify with REST and C# appeared first on Demac Media.


Viewing all articles
Browse latest Browse all 16

Trending Articles