Home > widget-integration > RecommendationAPI > getRecommendation
RecommendationAPI.getRecommendation() method
Fetches product recommendations from the Boost recommendation API.
This is the main entry point for making recommendation requests. It orchestrates the request construction process by delegating to extensible protected methods for parameter and payload building, then executes the HTTP POST request to the recommendation endpoint.
The method follows a clear pipeline: 1. Enhances query parameters through 2. Enhances request body through 3. Serializes parameters to query string format 4. Executes POST request with enhanced parameters and body
Signature:
getRecommendation(params: RecommendationQueryParams, bodyPayload: RecommendationPayload): Promise<HTTPResponse<RecommendationResponse>>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
params |
Query parameters for the recommendation request, including pagination settings (limit, page), widget configuration (widgetId), and any base filtering criteria. These parameters will be enhanced with currency, B2B settings, and custom shop-specific parameters through the transformation pipeline. | |
|
bodyPayload |
Request body payload containing the recommendation context such as product IDs, collection IDs, cart contents, widget type, and filtering rules. This payload will be enhanced with any custom shop-specific data through the transformation pipeline. |
Returns:
Promise<HTTPResponse<RecommendationResponse>>
Promise that resolves to a RecommendationResponse containing an array of recommended products with their metadata, pagination information, and tracking data for analytics. The promise rejects if the API request fails.
Exceptions
{Error} Network connectivity issues, timeout errors, or DNS resolution failures
{HTTPError} HTTP 4xx client errors (invalid parameters, authentication issues)
{HTTPError} HTTP 5xx server errors (API unavailable, internal server errors)
Remarks
Request flow: - **Step 1**: Query parameters are transformed by , which adds currency info, B2B parameters, and any custom query parameters - **Step 2**: Body payload is transformed by , allowing injection of custom filters, context, and business rules - **Step 3**: Parameters are serialized to URL query string format - **Step 4**: HTTP POST request is made to /?{queryString} with enhanced body
The method leverages the HTTP client from parent class, which provides connection pooling, retry logic, and error handling.
Performance considerations: - Uses POST instead of GET to support large request bodies - Parameter transformation is lightweight (no deep cloning) - HTTP client uses connection pooling for efficiency - Response is streamed and parsed efficiently
Error handling: - Network errors are propagated as rejected promises - HTTP error responses (4xx, 5xx) are rejected with error details - Inherits error handling behavior from
Example 1
Fetch similar products for a specific product:
const response = await api.getRecommendation(
{ limit: 10, page: 1 },
{
productId: '12345',
type: 'similar-products',
widgetId: 'pdp-similar'
}
);
console.log(`Found ${response.products.length} recommendations`);
response.products.forEach(product => {
console.log(`${product.title} - $${product.price}`);
});
Example 2
Fetch frequently bought together recommendations:
const response = await api.getRecommendation(
{ limit: 5 },
{
productId: '12345',
type: 'frequently-bought-together',
widgetId: 'pdp-fbt',
filters: {
inStock: true,
published: true
}
}
);
Example 3
Fetch cart-based recommendations with pagination:
const response = await api.getRecommendation(
{ limit: 20, page: 1 },
{
type: 'cart-recommendations',
widgetId: 'cart-upsell',
cartProductIds: [123, 456, 789],
filters: {
excludeCartProducts: true,
minPrice: 10,
maxPrice: 100
}
}
);