Showing results for 
Search instead for 
Did you mean: 

REST api - dispatch events?

REST api - dispatch events?

Digging through source code it looks like the REST api does not dispatch events.  How are observers run when interacting via the REST api?   


We want to add to cart via REST but have an observer that we want to run.  We need to change the price of the item being added based on properties of it.  Any way to do this?  



Re: REST api - dispatch events?



What about using plugins instead observers (which I understand are not possible to be used in this case).

You'll find here the oficial documentation about plugins.

Re: REST api - dispatch events?

If you want to observe events which are dispatched in API calls, you can use the etc/webapi_rest/events.xml file. It is also possible with the etc/events.xml file which works for all areas.


I got that from Vinai Kopp: including ans answer for the soap api.

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?

This would be great if it worked.  2 things.


1) Where do I find the etc/webapi_rest/events.xml file.

2) I already created my own observer and it has an events.xml at the etc/ level.  If it should work at that level then the event should be picked up it is not.


Digging into the actual code the webapi code never fires the event when an item is added.  With out the event being fired by the native Magento code the event listeners will never know about it.


I am new to Magento (and PHP) but come from a Java world where everything is very structured.  The Magento code base appears to be disorganized chaos to me with code and config files thrown all over the place with zero standards followed and documentation that doesn't match the code base.



Re: REST api - dispatch events?

1) Just create the directory webapi_rest below etc/ in your module and create the events.xml file there.

2) It worked for me with etc/events.xml and etc/webapi_rest/events.xml (modifying the address format in the cart). Are you sure you got the right event?

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?

1) Ok.  That makes sense.  however #2

2) the event is never fired.  Here is the event that I am listening for:


Here is the endpoint being called (snippet taken from webapi.xml):

<route url="/V1/carts/:quoteId/items" method="POST">
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
<resource ref="Magento_Cart::manage" />

 This is the save method:

public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
/** @var \Magento\Quote\Model\Quote $quote */
$cartId = $cartItem->getQuoteId();
$quote = $this->quoteRepository->getActive($cartId);

$quoteItems = $quote->getItems();
$quoteItems[] = $cartItem;
return $quote->getLastAddedItem();

This is the quoteRepository->save method.  No where is an event fired:

public function save(\Magento\Quote\Api\Data\CartInterface $quote)
if ($quote->getId()) {
$currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);

foreach ($currentQuote->getData() as $key => $value) {
if (!$quote->hasData($key)) {
$quote->setData($key, $value);



The only place in the entire code base where the event is fired is via the addProduct method in the vendor/magento/module-checkout/Model/Cart.php which only appears to be called from the front-end.



Re: REST api - dispatch events?

Does the API call in fact add a product? It looks as if it just saves the quote without adding anything...
An event you might observe there would be sales_quote_save_after or sales_quote_save_before. Haven't tested it though.

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?



Does any one got solution for observer not firing for Rest Api.




Re: REST api - dispatch events?

I have custom module and my code structure is as per service contact.


Now i have a dispatch event in my model file

$this->_eventManager->dispatch('vendor_register_success', $eventParams);

As per REST API standard and as per the this solution, i added event in specific module where it comes from.



<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="vendor_register_success">
<observer name="vendor_register_success" instance="[Vendor]\[Module]\Observer\VendorBrandRegister" />

and My observer looks like this

public function execute(\Magento\Framework\Event\Observer $observer)
echo "In execute"; exit;

but this event is not dispatching.
What am i missing ?

Because as per the solution, i need to add events in `webapi_rest` folder in `etc` folder.


Re: REST api - dispatch events?

I have tried in add to cart rest API but event is not working.