JavaScript 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 Language, 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)
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:1limit(number, optional): Results per page. Default:24sort(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 withutils.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 theviewoption.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 fromcatalog,locale,term,category, andfiltersfh_view- Set fromviewfh_view_size- Set fromlimitfh_start_index- Calculated frompageandlimitfh_sort_by- Omitted forsort=relevancewhich is the defaultfh_suppress- Set fromsuppressfh_session- Default:"shopify-app"market- Default: auto-detected from localecredentials- Hash containing API credentialsfh_session_id- Unique session identifier for tracking. Omitted if it does not exist.
queryProduct(options)
queryProduct(options)Stateless method to query a product with advanced options in Fredhopper.
Parameters:
options(object, optional): Configuration object
Options Properties:
id(string): product idid2(string): variant idfilters(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 withutils.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 theviewoption.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 fromcatalog,locale,term,category, andfiltersfh_secondid- Set fromidfh_secondid2- Set fromid2fh_view- Set fromviewfh_suppress- Set fromsuppressfh_session- Default:"shopify-app"market- Default: auto-detected from localecredentials- Hash containing API credentialsfh_session_id- Unique session identifier for tracking. Omitted if it does not exist.
Raw API Access
rawRequest(params)
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 stringfh_view(string): Fredhopper view type
Optional Parameters:
fh_view_size(number): Number of resultsfh_start_index(number): Starting index for paginationfh_sort_by(string): Sort fieldfh_session(string): Session identifierfh_session_id(string): Unique session IDmarket(string): Market code
Returns: Promise<any> - Raw API response
Important Note: Credentials are automatically added to all requests.
Utilities
Location Building
utils.buildLocation(options)
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 usingutils.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-selectview(string, optional): Fredhopper view type. Default:"lister"
Returns: string - Formatted Fredhopper location string
Encoding Utilities
utils.encodeFHUnicodeValue(value)
utils.encodeFHUnicodeValue(value)Encode filter values for Fredhopper Unicode format.
Parameters:
value(string, required): Value to encode
Returns: string - Encoded value
utils.formatSearchTerm(term)
utils.formatSearchTerm(term)Format search terms for Fredhopper by converting special characters to Unicode escape sequences.
Parameters:
term(string, required): Search term to formatview(string, optional): Fredhopper view type. Default:"lister"
Returns: string - Formatted search term
Other Utilities
utils.debounce(func, delay)
utils.debounce(func, delay)Debounce function for optimizing search inputs and preventing excessive API calls.
Parameters:
func(function, required): Function to debouncewait(number, required): Delay in milliseconds
Returns: function - Debounced function
utils.detectLocale()
utils.detectLocale()Auto-detect Shopify locale and convert it to Fredhopper format.
Parameters: None
Returns: string - Fredhopper locale string
utils.formatPrice(price, currency)
utils.formatPrice(price, currency)Format prices using Intl.NumberFormat.
Parameters:
price(number, optional): Price to format. Default:0currency(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 loggingdevelopment(boolean, optional): Enable development mode
Returns: Promise<void>
Event Handling
onReady(callback)
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 namecallback(function, required): Event handler function
Returns: void
Examples
Basic Product Search
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
Credentials Missing: Ensure the app is properly installed and credentials are set
CORS Errors: The SDK uses Shopify proxies to avoid CORS issues
Invalid Location Strings: Use the location builder utility to ensure proper formatting
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:
Console logs with
?fhr_debug=trueNetwork tab for API request/response details
Verify credentials are properly set in the app
For Fredhopper API documentation and support, consult the official Fredhopper documentation.
Last updated

