How to use Walmart API to find store locations

In this post, I will share code snippet from our HomeRP® portal where we integrate Walmart API for locating stores. You will have to send following GET request to Walmart services to get one or more stores based on the search query parameters.

https://developer.api.walmart.com/api-proxy/service/affil/product/v2/stores

You can append following query parameters to this URL to refine your search.

  • city: Name of nearest city
  • zip: Postal code of nearest city
  • lon: Longitude of nearest city location
  • lat: Latitude of nearest city location

Although the documentation says that all query parameters are optional, it is not true. You have to specify at least one of the parameters. Walmart does not allow an open search of store locations by not specifying any parameter. Omitting query parameters will complete the request but no results will be returned. Following code snippet shows .Net code that send a request to Walmart service. I use Store class that is used to deserialize JSON response into typed object.

public IList FindStores(string city = "", string state = "", 
         string postalCode = "", int? longitude = null, int? latitude = null) 
{
            try
            {
                var httpClient = new HttpClient();
                AddRequestHeaders(httpClient);
                var requestUri = $"{BaseUrl}stores";
                if (!string.IsNullOrEmpty(city)) { requestUri += $"?city={city.Trim()}"; }
                if (!string.IsNullOrEmpty(postalCode)) { requestUri += $"?zip={postalCode.Trim()}"; }
                if (longitude.HasValue) { requestUri += $"?lon={longitude}"; }
                if (latitude.HasValue) { requestUri += $"?lat={latitude}"; }

                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<IList<Store>>(content.Result);
                    default:
                        _logger.Error("Walmart Api", 
                              $"Error finding store: {response.StatusCode}: {content.Result}");
                        break;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Walmart Api", $"Error finding store", exception: ex);
                throw;
            }

            return new List<Store>();
}

public class Store
    {
        [JsonProperty("no")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("country")]
        public string Country { get; set; }
        [JsonProperty("coordinates")]
        public string[] Coordinates { get; set; }
        [JsonProperty("streetAdress")]
        public string Address { get; set; }
        [JsonProperty("city")]
        public string City { get; set; }
        [JsonProperty("stateProvCode")]
        public string State { get; set; }
        [JsonProperty("zip")]
        public string PostalCode { get; set; }
        [JsonProperty("phoneNumber")]
        public string Phone { get; set; }
    }

Search

Social

Weather

3.1 °C / 37.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags