Posted on 23/04/2025 10:46:53
Hi Mateusz,
The request /dwapi/ecommerce/products/search is just an end point that fetches products from DW as JSON. It's not BC specific at all. However, as part of fetching products, the prices need to be calculated. This is done by one or more so-called PriceProviders that can provide prices for products. If you use Live Integration, it has a PriceProvider that connects to an ERP like BC. When it connects, it can send the customer number (and user ID) as part of the request. It pulls this customer number from the current user. If there's no user, no customer number is sent and you get anonymous prices (or Live Integration doesn't run at all if it's not set up to get anonymous prices). When the request is made from a normal, pageview-based request, there's a DW auth cookie (and session state) that is used to get the current user. When you make an API request however, there's no cookie and session, and hence no user. By getting a JWT token from the auth end point, and sending that along with the API call, DW can extract user info from the JWT token and create a user that is then used inside live integration. So, in summary, you must send (as you found out) a JWT token in the API request to get user-specific pricesn or otherwise the end points don't know anything about the user that is making the request.
For impersonation, the same principles apply. In summary, you need to do this:
1. Log in as the main user. This gives you a JWT token. You already have this.
2. Hit /dwapi/users/impersonate?userId=UserId where UserId is the DW ID of the user you want to impersonate (you can get the list of users from /dwapi/users/impersonatees)
3. The impersonate end point gives you another JWT which represents the impersonated user.
When you then hit one of the product end points, you should send that second JWT that represents the impersonated user. That way, the request to the ERP is made on behalf of that user (its customer number) resulting in the correct prices.
More details on the impersonation end points here: https://doc.dynamicweb.dev/documentation/headless/delivery-api/access.html#impersonation
Hope this helps,
Imar