You can use Clerk.js to make API calls, by using the built in function Clerk.call(), this function takes 3 arguments.

Example Call

Define the function, and call it in the tracking-script.

HTML

<script type="text/javascript">
  window.clerkAsyncInit = function() {
    Clerk.config({
      key: 'indfgh9upf9hpg4h'
    });
    popularProducts();
  };
  (function(){
    var e = document.createElement('script');
    e.type='text/javascript'; e.async = true;
    e.src = document.location.protocol +
    '//api.clerk.io/static/clerk.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(e, s);
  })();
</script>

<script>
  function popularProducts(){
    Clerk.call(
      "recommendations/popular",
      {
        key: "indfgh9upf9hpg4h",
        limit: 10,
      },
      function(response){ console.log(response); }
    );
  }
</script>

Response

The response a JavaScript Object, with the status of the API call, and the result.

JavaScript

jQuery110205659561061587193_1499933500270(
  {
    "status":"ok",
   "count":72,
   "facets":null,
   "result":[399,410,551,338,403,439,425,402,406,456]
  }
);

Working with the response

Use a Callback function to handle the response

HTML

<script>
  Clerk.call
    (
      "search/categories",
        {
          'key':"indfgh9upf9hpg4h",
          'query': "men",
          'limit': "10"
        },
        function(x){
          var cat = x.categories;
          if(cat.length > 0)
          {
            $('#your-target').append("<h3>Related Categories</h3>")
          }
          for(var index in cat) {
            var clerkName = cat[index].name;
            var clerkUrl = cat[index].url;
            $('#your-target').append("<a
            href="+clerkUrl+">"+clerkName+"</a> <br>")
         }
      }
   )
</script>

This example will return the categories matching the query, and present them as text.

This way you can make calls to our API quick and easy.

Did this answer your question?