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
  }
}

本文标签: