Hello,
I’m trying to do a product Batch Read to retrieve the product ID through the SKU but when I get back the request the result array is empty.
I’ve tried the code generated on the product endpoint page with our account and that seems to work so the problem might be in the formatting?
Anyhow, here’s a gist of it in C#
JObject requestBody = new JObject
{
{"properties", "*"},
{"propertiesWithHistory", "*"},
{"idProperty", "hs_sku" },
{"inputs", new JArray
{
new JObject {{"id", "NBEK2_C"}},
new JObject {{"id", "NBEK2_S"}},
}
}
};
RestClient.DefaultRequestHeaders["authorization"] = "Bearer " + accessToken;
RestClient.Request(new RequestHelper
{ Uri="https://api.hubapi.com/crm/v3/objects/products/batch/readarchived=false",
Method = "POST",
Headers = new Dictionary<string, string> {
{ "accept", "application/json" },
{ "content-type", "application/json" },
},
Body = requestBody,
})
Any ideas?
Thanks!
Hello @JHFlink
Based on the code you provided, it seems like you’re missing the HubSpot API key in the authorization header. You’ll need to replace accessToken with your actual HubSpot API key.
Additionally, you have a typo in your Uri parameter. The readarchived parameter should be appended as a query string, not as part of the path. So your Uri parameter should look like this:
Uri = "https://api.hubapi.com/crm/v3/objects/products/batch/read?archived=false"
Finally, you might want to add error handling to your code to catch any exceptions that might be thrown during the API call. Here’s an updated version of your code with these changes
JObject requestBody = new JObject
{
{"properties", "*"},
{"propertiesWithHistory", "*"},
{"idProperty", "hs_sku" },
{"inputs", new JArray
{
new JObject {{"id", "NBEK2_C"}},
new JObject {{"id", "NBEK2_S"}},
}
}
};
string apiKey = "your_api_key_here";
RestClient.DefaultRequestHeaders["Authorization"] = "Bearer " + apiKey;
var response = RestClient.Request(new RequestHelper
{
Uri = "https://api.hubapi.com/crm/v3/objects/products/batch/read?archived=false",
Method = "POST",
Headers = new Dictionary<string, string>
{
{ "accept", "application/json" },
{ "content-type", "application/json" },
},
Body = requestBody,
});
if (response.StatusCode == HttpStatusCode.OK)
{
var responseBody = JObject.Parse(response.Content);
// handle the response here
}
else
{
// handle the error here
}
Thank you for the reply!
Sorry if I was not clear enough, I do have both the token and the error/success handling, just left it out here since it was pretty trivial.
And the query string ? somehow got left out when I copied the code into here.
I kind of feel that it has something to do with the requestBody Json data, the rest api I’m using is slightly different from the one in the product endpoint examples which goes like this (and works):
var client = new RestClient("https://api.hubapi.com/crm/v3/objects/products/batch/read?archived=false");
var request = new RestRequest(Method.POST);
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "Bearer YOUR_ACCESS_TOKEN");
request.AddParameter("application/json", "{\"properties\":[\"seats\"],\"propertiesWithHistory\":[\"seats\"],\"idProperty\":\"hs_sku\",\"inputs\":[{\"id\":\"NBEK2_C\"},{\"id\":\"NBEK2_S\"}]}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Use this modified version of your request body
JObject requestBody = new JObject
{
{"inputs", new JArray
{
new JObject
{
{"id", "NBEK2_C"},
{"properties", new JArray{"hs_sku"}}
},
new JObject
{
{"id", "NBEK2_S"},
{"properties", new JArray{"hs_sku"}}
},
}
}
};
Thank you!
With the addition of the {“idProperty”, “hs_sku” }, to your version of the request body it seems to work.
JObject requestBody = new JObject
{
{"idProperty", "hs_sku" },
{"inputs", new JArray
{
new JObject
{
{"id", "NBEK2_C"},
{"properties", new JArray{"hs_sku"}}
},
new JObject
{
{"id", "NBEK2_S"},
{"properties", new JArray{"hs_sku"}}
},
}
}
};