salesforce integration with shopify

Salesforce REST API Integration With Shopify

Here I come again with another blog of mine which will help you out in the integration of Salesforce with Shopify platform.

Shopify is a platform with the help of which one can create its online store to sell their products. One can set up any kind of store to start its own business online.

In order to achieve Salesforce integration with Shopify, we need to have a Shopify account/store which we integrate to show output in the Salesforce.

As you must be aware of the fact that in order to fetch data from any platform into any other platform, we need to have an Access token and password. In Shopify, Access token and password can be easily generated by creating a private app.

dont miss out iconDon't forget to check out: Integrate .NET console application with Salesforce Rest API

Create Private app in Shopify:

  1. Login in to your Shopify account/store.
  2. From the options available on the left side, click apps.
  3. Click “Manage private apps”
  4. Click on “Create new private app” and fill out the details of the private app. You can also provide the scope of access to different options available for this application.
  5. After creating the app you will get the API key and password.

After doing all the above-mentioned things, it would be better if you create a few products, orders, and customers in your Shopify store.

Things to do in Salesforce org before proceeding further are:

1. First, we need to place the Shopify store URL(Example URL in the private app created in the Shopify store) in Remote Site Settings.

Path: Setup > Security > Remote Site settings > New Remote Site and paste the example URL and save it with any name of your choice. 

2. Shopify connects with the trusted sites and in order to create Salesforce a trusted site for Shopify, we need to create a certificate in Salesforce which will make Salesforce a trusted site and Shopify accepts the certified request and send the data. 

Path: Setup > Security > Certificate and Key Management > Create Self-Signed certificate > Fill the details and save it.

Till now what we have done is created a private app for token generation and added the URL into the remote site settings in Salesforce and made Salesforce as a trusted site for Shopify.

Now we have to create a REST class that will fetch the data from Shopify.

REST Class:

public class ShopifySalesforce {
    /*
    description : Method for Callout to Shopify to get all Products records
    */
    public void retrieveShopifyData() {
        //Local Variables
        string ResponseBody;
        //Making Callouts using HTTP Requests
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        //Provide URL of the Shopify store to HTTP requests
        System.debug('Endpoint start');
        String url = 'https://xxxxxx.myshopify.com';
        string endPointValue = url + '/admin/api/2020-04/products.json';
        req.setEndpoint(endPointValue);
        System.debug('Endpoint finish');
        //Providing Username and Password
        String username = 'Enter Shopify API key here';
        String password = 'Enter Password here';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' +
        EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        req.setHeader('content-type', 'application/json');
        //Use get Method as we are retrieving data from shopify
        req.setMethod('GET');
        try{
            HttpResponse res = h.send(req);
            System.debug('res'+res);
            ResponseBody = res.getBody();
            System.debug('Working fine till here');
        }
        catch (exception e) {
            System.debug('exception' + e);
        }
    }
}

Create a VF page and call the REST class from VF page:

<apex:page controller="ShopifySalesforce" >
    <apex:form >
        <apex:commandButton value="getShopifyData" action="{!retrieveShopifyData}"/>
    </apex:form>
</apex:page>

To check the output you can set up the debug logs and check the output there and you can also use other endpoints as mentioned in Shopify API documentation.

dont miss out iconCheck out another amazing blog by Udit here: Inserting Data into Salesforce Data Extensions Using Cloud Pages and If-Else Conditional Statement in AMPscript

You can use Postman to test the API as well. Follow the below steps:

GET Request:

  1. You can use the Postman application or chrome extensions.
  2. Use the authentication type as Basic Auth and provide the Username and Password. Username is an API key generated from the private app in Shopify and password is the password generated from the private app in Shopify.
  3. Provide “https://xxxxx.myshopify.com/admin/api/2020-04/products.json” in the URL section and use GET as a request and hit send. This will return all the existing products in your Shopify store.

POST Request:

  1. In order to create/insert the products in Shopify via Postman, follow the same steps as above just use POST request instead of GET.
  2. Navigate to the body section and select the raw option and from the drop-down list select JSON(application/json) and provide details of products in JSON format.
{
    "product":{
        "title":"Burton Custom Freestyle 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"
        }]
    }
}

Make sure to try this on your own, Stay Safe Stay Home.

Popular Salesforce Blogs