How to use Walmart search API using .Net

I have integrated Walmart API into our HomeRP® application. If you are familiar with Amazon's product search API, then this will come as no surprise.

Walmart has following API that you can use for search.

https://developer.api.walmart.com/api-proxy/service/affil/product/v2/search?query=mysearchterm

Above request is the use of the API in its simplest form. You will provide your search phrase in query query parameter. This API has additional query parameters to refine the results.

  • start:Starting point of the results within the matching set of items - upto 10 items will be returned starting from this item
  • numItems :Number of matching items to be returned, max value 25. Default is 10.
  • sort:Sorting criteria, allowed sort types are [relevance, price, title, bestseller, customerRating, new]. Default sort is by relevance.
  • order:Sort ordering criteria, allowed values are [ascending, descending]. This parameter is needed only for the sort types [price, title, customerRating].

There are some additional query string parameter including above list. You can read about them on Walmart developer site. All query string parameters other then query are optional.

public SearchResults Search(string term, int startIndex = 1, int maxResults = 10)
{
            try
            {
                var httpClient = new HttpClient();
                AddRequestHeaders(httpClient);
                var requestUri = new Uri($"{BaseUrl}search?query={term}&start={startIndex}&numItems={maxResults}");

                var getTask = Task.Run(async () => await httpClient.GetAsync(requestUri));
                var response = getTask.Result;
                var content = Task.Run(async () => await response.Content.ReadAsStringAsync());
                switch (response.StatusCode)
                {
                    case HttpStatusCode.OK:
                        return JsonConvert.DeserializeObject<SearchResults>(content.Result);
                    default:
                        _logger.Error("Walmart Api", $"Error searching by query: {response.StatusCode}: {content.Result}");
                        break;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Walmart Api", $"Error searching by query {term}", exception: ex);
                throw;
            }

            return null;
        }

The API will return the results as JSON response. The details of this response can be found on following page.

Search Response Details

Search

Social

Weather

8.2 °C / 46.7 °F

weather conditions Clouds

Monthly Posts

Blog Tags