Technology

Integrating Magento with a Third-Party API

Posted by Pixafy Team

Today marks the first post in a multi-part series of articles describing the best practices for integrating Magento with a Test third-party API.  In this example, we will be integrating with Magento’s own web service API, but this techniques can be broadly applied to other API integrations.

Step 1: Set up the API Account & Review the API Documentation

Normally, this step will be done through the website which we are going to integrate with Magento.  For simplicity’s sake, let’s integrate with Magento’s web service API. It will be a great way to learn what is really going on under the hood on both sides.  The integration we’ll perform will be very simple: the user will input an item SKU, and the page will display information related to the product by querying the web service.

First, let’s take a look at Magento’s web service WSDL.  You can find this by going to the base URL for your Magento install and adding /api/v2_soap?wsdl=1 to the end.  Here you’ll see a full description of the Magento v2 API, but we will only be dealing with a small subset of this document, “catalogProductInfo”.  Each operation deals with a request message, and its corresponding response message.

Next, we’ll need to set up an API user in order to use the Magento API from our test integration script.  To do this, log in to the Magento admin, then go to System -> Web Services -> SOAP/XML-RPC – Users.  Click “Add New User” and fill out the form.  Keep in mind the values you set for “User Name” and “New API Key”.  After saving the user, go to System -> Web Services -> SOAP/XML-RPC – Roles and click “Add New Role”.  Give the role a name and then click the “Role Resources” tab.  Click the checkbox under Catalog -> Product -> Retrieve products data.  Now click “Save Role”, and a new tab “Role Users” should appear.  Go to this tab and find the user you had just created.  Add this user to the role and then click “Save Role” again.  Now you’re all set with the API permissions you need!

Step 2: Set up the Admin Configuration Settings

In order to provide some reusability for our module, we’ll need to provide the admin user with a settings configuration page, so he or she may change things like the web service URL, or the API credentials.  A great reference to follow for this part of the demo can be found here: http://alanstorm.com/magento_admin_hello_world_revisited

Let’s create the module we’ll need to create a module for the demo.  On a fresh install of Magento, add the following files:

In app/etc/modules/Example_Wsdlintegration.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Example_Wsdlintegration>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends>
        </Example_Wsdlintegration>
    </modules>
</config>

In app/code/local/Example/Wsdlintegration/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Example_Wsdlintegration>
            <version>0.0.1</version>
        </Example_Wsdlintegration>
    </modules>
    <global>
        <models>
            <wsdlintegration>
                <class>Example_Wsdlintegration_Model</class>
            </wsdlintegration>
        </models>
        <helpers>
            <wsdlintegration>
                <class>Example_Wsdlintegration_Helper</class>
            </wsdlintegration>
        </helpers>
    </global>
    <default>
        <wsdlintegration>
            <general>
                <status>1</status>
                <debug_log>example_debug.log</debug_log>
                <error_log>example_error.log</error_log>
            </general>
        </wsdlintegration>
    </default>
</config>

In app/code/local/Example/Wsdlintegration/etc/adminhtml.xml:

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <wsdlintegration>
                                        <title>Example WSDL Integration</title>
                                    </wsdlintegration>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</config>

In app/code/local/Example/Wsdlintegration/etc/system.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <tabs>
        <example translate="label" module="wsdlintegration">
            <label>Example</label>
            <sort_order>1000</sort_order>
        </example>
    </tabs>
    <sections>
        <wsdlintegration translate="label" module="wsdlintegration">
            <label>WSDL Integration</label>
            <tab>example</tab>
            <sort_order>0</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <general translate="label" module="wsdlintegration">
                    <label>General Settings</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>0</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <status translate="label" module="wsdlintegration">
                            <label>Enabled: </label>
                            <frontend_type>select</frontend_type>
                            <sort_order>0</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                        </status>
                        <service_url translate="label" module="wsdlintegration">
                            <label>Service URL: </label>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <status>1</status>
                            </depends>
                        </service_url>
                        <api_user translate="label" module="wsdlintegration">
                            <label>API Username: </label>
                            <frontend_type>text</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <status>1</status>
                            </depends>
                        </api_user>
                        <api_key translate="label" module="wsdlintegration">
                            <label>API Key: </label>
                            <frontend_type>password</frontend_type>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <status>1</status>
                            </depends>
                        </api_key>
                        <debug_log translate="label" module="wsdlintegration">
                            <label>Debug Log: </label>
                            <frontend_type>text</frontend_type>
                            <sort_order>50</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Leave blank to disable debug logging</comment>
                            <depends>
                                <status>1</status>
                            </depends>
                        </debug_log>
                        <error_log translate="label" module="wsdlintegration">
                            <label>Error Log: </label>
                            <frontend_type>text</frontend_type>
                            <sort_order>60</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Leave blank to disable error logging</comment>
                            <depends>
                                <status>1</status>
                            </depends>
                        </error_log>
                    </fields>
                </general>
            </groups>
        </wsdlintegration>
    </sections>
</config>

In app/code/local/Example/Wsdlintegration/Helper/Data.php:

<?php

class Example_Wsdlintegration_Helper_Data extends Mage_Core_Helper_Abstract {

}

With these five files, we’ve laid some groundwork for our project and created our admin settings page.  To see the results, first make sure you are logged out of the Magento admin, then log back in and go to System -> Configuration and go to the tab Example -> WSDL Integration.  Here, you should enter the API settings that we configured in part 1:

Service URL: http://your_magento_host/api/v2_soap?wsdl=1
API Username: “User Name” you set in part 1.
API Key: “New API Key” you set in part 1.

Click “Save Config” and we’re done with this part of the setup.  Join us next time when we’ll get into extending the SOAP client class and interacting with the server!

About Pixafy

Pixafy develops eCommerce technology products for clients and end users which leverage open source solutions such as Magento. Established in 2010, the award-winning company is responsible for building a portfolio of eCommerce products that generate over $125 million in annual revenue. Pixafy’s mission is to help champion the open source movement by introducing user inspired products that replace time and cost intensive development with automated technologies.