codeJavaScript SDK

Overview

The Fredhopper SDK is designed as a lean API client for interacting with the Fredhopper Query API, through the Shopify App Proxy, focused on making it easy to query Fredhopper endpoints while letting developers build their own UI and rendering logic. It handles authentication, request formatting, and provides utilities for common Fredhopper operations.

To enable the SDK Theme Embed provided with the Shopify App, see the documentation

For more details on using the Fredhopper Query Languagearrow-up-right, used by both the SDK and the Query API, see the documentation.

Quick Start

Basic Setup

The SDK is automatically initialized when the script loads. Credentials are managed through the app and exposed via window.fredhopper.credentials.

// SDK is available globally
const collectionResults = await fredhopper.queryCatalog({category: "12345"});
const searchResults= await fredhopper.queryCatalog({term: "shoes", view: "search"});

Development Mode

Add ?fhr_debug=true to your URL to enable detailed console logging in the browser console.

API Reference

Core Methods

queryCatalog(options)

Stateless method to query a catalog with advanced options in Fredhopper.

Parameters:

  • options (object, optional): Configuration object

Options Properties:

  • page (number, optional): Page number. Default: 1

  • limit (number, optional): Results per page. Default: 24

  • sort (string, optional): Sort option. Default: "relevance"

    • Available values: "relevance", "price-asc", "price-desc", or custom sort field

  • filters (object, optional): Filter object. Default: {}

    • Single value: { brand: "nike" } or { brand: ["nike"] }

    • Multiple values: { brand: ["nike", "adidas"] }

    • Range values: { price: "10<price<100" }

  • catalog (string, optional): Catalog identifier. Default: "catalog01"

  • locale (string, optional): Locale in Fredhopper format (e.g., "en_GB"). Default: auto-detected with utils.detectLocale()

  • category (string, optional): Category identifier. Default: ''

    • The category ID originates from Shopify's collection object using the Liquid variable {{ collection.id }} and is automatically available.

  • view (string, optional): Fredhopper view type. Default: "lister"

    • It is recommended to use "search" for search queries.

  • term (string, optional): Search term. Default: ''

  • suppress (string, optional): Default: ''

    • Fredhopper suppress parameter to suppress parts of the response (e.g., 'facets')

Returns: Promise<any> - Query results from Fredhopper API

Important Note:

The Fredhopper API requires two key parameters:

  • fh_view- Specifies Fredhopper view type. It defaults to 'lister' and can be set via the view option.

  • fh_location - Automatically built from your catalog, locale, search term, category, and filters using the SDK's location builder.

Following parameters are added automatically to the request payload based on the options provided:

  • fh_location - Built from catalog, locale, term, category, and filters

  • fh_view - Set from view

  • fh_view_size - Set from limit

  • fh_start_index - Calculated from page and limit

  • fh_sort_by - Omitted for sort=relevance which is the default

  • fh_suppress - Set from suppress

  • fh_session - Default: "shopify-app"

  • market - Default: auto-detected from locale

  • credentials - Hash containing API credentials

  • fh_session_id - Unique session identifier for tracking. Omitted if it does not exist.

queryProduct(options)

Stateless method to query a product with advanced options in Fredhopper.

Parameters:

  • options (object, optional): Configuration object

Options Properties:

  • id (string): product id

  • id2 (string): variant id

  • filters (object, optional): Filter object. Default: {}

    • Single value: { brand: "nike" } or { brand: ["nike"] }

    • Multiple values: { brand: ["nike", "adidas"] }

    • Range values: { price: "10<price<100" }

  • catalog (string, optional): Catalog identifier. Default: "catalog01"

  • locale (string, optional): Locale in Fredhopper format (e.g., "en_GB"). Default: auto-detected with utils.detectLocale()

  • category (string, optional): Category identifier. Default: ''

    • The category ID originates from Shopify's collection object using the Liquid variable {{ collection.id }} and is automatically available.

  • view (string, optional): Fredhopper view type. Default: "detail"

  • term (string, optional): Search term. Default: ''

  • suppress (string, optional): Default: ''

    • Fredhopper suppress parameter to suppress parts of the response (e.g., 'facets')

Returns: Promise<any> - Query results from Fredhopper API

Important Note:

The Fredhopper API requires two key parameters:

  • fh_view- Specifies Fredhopper view type. It defaults to 'detail' and can be set via the view option.

  • fh_location - Automatically built from your catalog, locale, search term, category, and filters using the SDK's location builder.

Following parameters are added automatically to the request payload based on the options provided:

  • fh_location - Built from catalog, locale, term, category, and filters

  • fh_secondid - Set from id

  • fh_secondid2 - Set from id2

  • fh_view - Set from view

  • fh_suppress - Set from suppress

  • fh_session - Default: "shopify-app"

  • market - Default: auto-detected from locale

  • credentials - Hash containing API credentials

  • fh_session_id - Unique session identifier for tracking. Omitted if it does not exist.

Raw API Access

rawRequest(params)

Direct API request with full control over all Fredhopper parameters:

Parameters:

  • params (object, required): Direct Fredhopper API parameters

Required Parameters:

  • fh_location (string): Fredhopper location string

  • fh_view (string): Fredhopper view type

Optional Parameters:

  • fh_view_size (number): Number of results

  • fh_start_index (number): Starting index for pagination

  • fh_sort_by (string): Sort field

  • fh_session (string): Session identifier

  • fh_session_id (string): Unique session ID

  • market (string): Market code

Returns: Promise<any> - Raw API response

Important Note: Credentials are automatically added to all requests.

Utilities

Location Building

utils.buildLocation(options)

Build Fredhopper location strings programmatically.

Parameters:

options (object, optional): Location building configuration

Options Properties:

  • catalog (string, optional): Catalog identifier. Default: "catalog01"

  • locale (string, optional): Locale. Default: auto-detected using utils.detectLocale()

  • searchTerm (string, optional): Search term. Default: ''

  • category (string, optional): Category identifier. Default: ''

  • filters (object, optional): Filter object. Default: {}

  • facetMultiMap (object, optional): Defines which facets allow multiple selections. Default: {}

  • Format: { facetName: true/false }

  • true = multi-select, false = single-select

  • view (string, optional): Fredhopper view type. Default: "lister"

Returns: string - Formatted Fredhopper location string

Encoding Utilities

utils.encodeFHUnicodeValue(value)

Encode filter values for Fredhopper Unicode format.

Parameters:

  • value (string, required): Value to encode

Returns: string - Encoded value

utils.formatSearchTerm(term)

Format search terms for Fredhopper by converting special characters to Unicode escape sequences.

Parameters:

  • term (string, required): Search term to format

  • view (string, optional): Fredhopper view type. Default: "lister"

Returns: string - Formatted search term

Other Utilities

utils.debounce(func, delay)

Debounce function for optimizing search inputs and preventing excessive API calls.

Parameters:

  • func (function, required): Function to debounce

  • wait (number, required): Delay in milliseconds

Returns: function - Debounced function

utils.detectLocale()

Auto-detect Shopify locale and convert it to Fredhopper format.

Parameters: None

Returns: string - Fredhopper locale string

utils.formatPrice(price, currency)

Format prices using Intl.NumberFormat.

Parameters:

  • price (number, optional): Price to format. Default: 0

  • currency (string, optional): Currency code. Default: "USD"

Returns: `string - Formatted price string

Configuration & Initialization

Basic Initialization

Initialize or reconfigure the SDK. The SDK auto-initializes on load, but you can reconfigure it.

Parameters:

  • options (object, optional): Configuration object

Options Properties:

  • debug (boolean, optional): Enable debug logging

  • development (boolean, optional): Enable development mode

Returns: Promise<void>

Event Handling

onReady(callback)

Execute callback when SDK is fully initialized.

Parameters:

  • callback (function, required): Function to execute when ready

Returns: void

Event System

Subscribe to SDK events.

Parameters:

  • event (string, required): Event name

  • callback (function, required): Event handler function

Returns: void

Examples

Advanced Collection with Filters

Debounced Search Input

Error Handling

The SDK throws errors for failed requests. Always wrap API calls in try-catch blocks:

Best Practices

1. Handle Localization

Use utils.detectLocale() to automatically handle different markets:

3. Debounce User Input

Always debounce search inputs to avoid excessive API calls:

4. Use Location Builder for Complex Queries

For complex filtering scenarios, use the location builder utility:

5. Handle Loading States

Track loading states in your UI:

Troubleshooting

Debug Mode

Enable debug mode to see detailed request/response logging:

Common Issues

  1. Credentials Missing: Ensure the app is properly installed and credentials are set

  2. CORS Errors: The SDK uses Shopify proxies to avoid CORS issues

  3. Invalid Location Strings: Use the location builder utility to ensure proper formatting

  4. Rate Limiting: Implement debouncing for user input

TypeScript Support

While the SDK is written in JavaScript, it includes JSDoc type annotations for better IDE support. For full TypeScript support, you can create type definitions:

Support

For issues specific to this SDK implementation, check:

  1. Console logs with ?fhr_debug=true

  2. Network tab for API request/response details

  3. Verify credentials are properly set in the app

For Fredhopper API documentation and support, consult the official Fredhopper documentation.

Last updated