Cert++
Close

OCAPI before, after and modifyResponse hooks and their return behavior

Key Takeaway

A Status of ERROR from a hook stops processing, rolls back the transaction and returns HTTP 400 Bad Request. For a state-changing request, the before hook, the after hook and the system logic share one database transaction, so an ERROR from the after hook means nothing is committed.

OCAPI hooks are named extension points where your own script code runs at fixed moments while B2C Commerce handles a Shop API or SCAPI Shopper API request. A before hook runs before the server changes anything. An after hook runs after the change, inside the same database transaction. A modifyResponse hook edits the response document just before the server sends it. What a hook returns (a Status object or nothing) decides whether the request continues, fails, or skips the default logic.

Hook names and methods

Each hook name ends with the hook type and the HTTP method. For example:

  • dw.ocapi.shop.basket.billing_address.beforePUT
  • dw.ocapi.shop.basket.items.afterPOST
  • dw.ocapi.shop.category.modifyGETResponse

GET requests support before and modifyResponse hooks. State-changing methods (POST, PATCH, PUT and DELETE) also support after hooks, because only they write to the database.

The same hooks run for both SCAPI and OCAPI requests. Their names keep the historical dw.ocapi.shop.* prefix, even when a SCAPI request calls them.

Hook scripts load as CommonJS modules. Each function must be exported under the hook name without its package qualification. For example, the function for dw.ocapi.shop.basket.billing_address.beforePUT is exported as exports.beforePUT.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a use case, extend functionality or capture an event using hook extension points

0/6

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.