Skip to main content

The task was to set e-mail addresses export from the service with the help of Webhook to the client’s CRM via API.

Webhook allows transporting some data while the determined event takes place. From the technical point of view, Webhook is a post-request to the certain URL that is set for a proper data acceptance. If it is needed to get some changes in the internal service’s objects, API can be used for this purpose. In this case, the service has to get periodical requests via API. If Webhook is used, the service does post-request to the URL. Thus, the data is received in real-time.

I’d got the data format description and set URL name in the service and saved test request with the help of (it is a useful resource for debugging actions like this). I used the saved request in postman ( This program is useful for debugging applications that work with http-requests. It simplifies local debugging, there is no need of outside port forwarding of the server and DNS setup.

Specify a new route in config.xml in the extension for Magento:




Create a controller that will get a post with data.


 class YouCompany_YorModuleName_IndexController extends Mage_Core_Controller_Front_Action


public function indexAction()
 if ($this->getRequest()->isPost()) {
 $postdata = file_get_contents("php://input");
 $postdata = json_decode($postdata, true);
 $email = $postdata['email'];
 if (!Zend_Validate::is($email, 'EmailAddress')) {
 Mage::log('Webhook Invalid email in post data');
 return null;


$ownerId = Mage::getModel('customer/customer')

if ($ownerId !== null) {
 Mage::log('This email address is already assigned to another user.');
 return null;
 $status = Mage::getModel('newsletter/subscriber')->subscribe($email);

//Send data to crm
 $request = new Varien_Http_Client();
 $request->setUri(self::URL_TO_SECONDPARTY_API );
 $request->setHeaders('Secondary-Party-Tracker-Id', Mage::getStoreConfig('some_module/config/tracking_id'));
 $data = array(
 'type' => 'account',
 'data' => array(
 'action' => 'signup',
 'email' => $email,
 $json = json_encode($data);
 $response = $request->setRawData($json, 'application/json')->request('POST');
 $responseMessage =$response->getMessage();
 if ($responseMessage != 'OK') {
 Mage::log('webhook - ' . $responseMessage);
 if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
 Mage::log('webhook - disable confirmation request.');
 // return null;
 } else {
 Mage::log('webhook - success');
Vladimir Repalo

Vladimir Repalo

Magento Developer at Mobecls, 8+ years of experience. Feel free to ask me anything about this post in the comments below.