Backend-for-frontend Apex controllers using Commerce ConnectApi
Key Takeaway
A BFF Apex controller is your third choice for storefront data. When you build one, cache its result when appropriate and use ConnectApi.CommerceSearch.searchProducts for product search that respects buyer entitlements.
A backend-for-frontend (BFF) Apex controller is a custom Apex class, built for a specific storefront page, that a Lightning web component calls to get exactly the data that page needs. Inside Apex, Commerce ConnectApi classes such as ConnectApi.CommerceSearch provide store-level operations like product search. The choices you make in the controller affect how often the page goes back to the server and which products a buyer can get.
Where a BFF Apex controller fits
Components in an LWR storefront can get data three ways. Use them in this order of preference, because following it avoids unnecessary server requests and keeps data consistent across the page.
- Data providers retrieve data in an optimized way. They are essential for accessing server-side data during server-side rendering (SSR).
- Client-side storefront APIs come from the Commerce Runtime. Data providers are built on top of them.
- BFF Apex controllers are also called custom Experience APIs. Build one only when data providers and the client-side storefront APIs aren't available or sufficient.