admin管理员组文章数量:1287866
I'm developing a Node.js application that interacts with the HubSpot API using the @hubspot/api-client package. I am trying to retrieve detailed information about a product, including its "hs_sku" property. However, when I fetch a product using the basic API, the "hs_sku" property does not seem to appear in the response.
lib -> hubspot.ts
import { HubspotDeal, HubspotCompany, HubspotProduct } from "@shared/schema";
import { apiRequest } from "./queryClient";
export async function getProducts() {
const res = await apiRequest("GET", "/api/products");
return await res.json() as HubspotProduct[];
}
server -> hubspot.ts
import axios, { AxiosInstance } from "axios";
import { HubspotCompany, HubspotDeal, HubspotContact, HubspotProduct, OIL_TYPES, PACKAGING_TYPES } from "@shared/schema";
const HUBSPOT_API_BASE_URL = ";;
export class HubspotService {
public apiKey: string;
public axios: AxiosInstance;
constructor() {
if (!process.env.HUBSPOT_API_KEY) {
console.error("HUBSPOT_API_KEY is not set");
throw new Error("HUBSPOT_API_KEY environment variable is required");
}
this.apiKey = process.env.HUBSPOT_API_KEY;
...
async getProducts(): Promise<HubspotProduct[]> {
try {
// First get list of all products with minimal properties
console.log("Fetching initial product list...");
const response = await this.axios.get("/objects/products", {
params: {
limit: 100,
properties: ['name'] // Just get names to minimize response size
},
});
console.log(`Found ${response.data.results.length} products, fetching details...`);
// Then fetch each product individually using the same endpoint as Postman
const productDetails = await Promise.all(
response.data.results.map(async (product) => {
console.log(`Fetching details for product ${product.id}`);
const detailResponse = await this.axios.get(`/objects/products/${product.id}`, {
params: {
properties: ['hs_sku', 'name', 'price', 'description', 'hs_product_type',
'price_standard', 'createdate', 'hs_lastmodifieddate', 'hs_object_id']
},
});
console.log(`Product ${product.id} details:`, detailResponse.data);
return detailResponse.data;
})
);
return productDetails;
} catch (error: any) {
console.error("HubSpot API Error:", error.response?.data || error.message);
throw new Error("Failed to fetch products from HubSpot");
}
}
async getProductBySKU(sku: string): Promise<HubspotProduct | null> {
try {
console.log("Searching for product with SKU:", sku);
const response = await this.axios.post("/objects/products/search", {
filterGroups: [{
filters: [{
propertyName: "hs_sku",
operator: "EQ",
value: sku
}]
}],
properties: ["hs_sku", "name", "price", "description", "hs_product_type",
"price_standard", "createdate", "hs_lastmodifieddate", "hs_object_id"]
});
console.log("HubSpot search response:", response.data);
if (response.data.total === 0) {
console.log("No product found with SKU:", sku);
return null;
}
const product = response.data.results[0];
console.log("Found product:", product);
return product;
} catch (error: any) {
console.error("HubSpot API Error:", error.response?.data || error.message);
throw new Error("Failed to fetch product from HubSpot");
}
}
}
export const hubspotService = new HubspotService();
Here's the API response I get regardless if I specified hs_sku:
HubSpot API Response: {status: 200,
statusText: 'OK',
data: {
id: '30911513317',
properties: {
createdate: '2025-02-21T18:42:14.309Z',
description: 'description',
hs_lastmodifieddate: '2025-02-22T18:26:43.587Z',
hs_object_id: '30911513317',
name: 'name',
price: '5000'
},
createdAt: '2025-02-21T18:42:14.309Z',
updatedAt: '2025-02-22T18:26:43.587Z',
archived: false
}
}
HubSpot API Response: {
status: 200,
statusText: 'OK',
data: {
id: '30911513317',
properties: {
createdate: '2025-02-21T18:42:14.309Z',
description: 'description',
hs_lastmodifieddate: '2025-02-22T18:26:43.587Z',
hs_object_id: '30911513317',
name: 'name',
price: '5000'
},
createdAt: '2025-02-21T18:42:14.309Z',
updatedAt: '2025-02-22T18:26:43.587Z',
archived: false
}
}
本文标签:
版权声明:本文标题:How can I retrieve the "hs_sku" property from a HubSpot Product in my Node.js application? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322500a2372286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论