In the previous part of this Custom API development series, we created our custom module to implement custom API and we created files for plugin as well, which are required by the custom APIs provided by our module.
In this second and last part will look at the backend section of Magento to show how to expand the custom APIs.
Setting up API User and API Role From the Magento BackEnd
Now we are setting up API User and API Role in Magento’s Backend. For the setup of API Role go to System > Web Services > (SOAP/XMLRPC) Roles and Click on “Add New Role” button for creating a new role.
Under the “Role Info” tab it will ask for “Role Name” and “Current Admin Password”. I used “Cloudways SOAP Role” in the Role Name field and clicked on the “Save Role” button. After saving your role go to “Role Resources” tab. Under Role Resources, you’ll see different resources with check boxes. Click on the Core > Store > List of stores check box. It means that “Cloudways SOAP Role” can only access “List of stores” resources APIs.
Note: I selected Stores > List of Stores Under “Resources” for demonstration purpose. You can choose a module of your own choice.
Click on the Save Role button for save the role information.
Now set up API User for assigning your API role. Setup of API User “Cloudways SOAP Role” to the new user which we are going to create. For Setup of API User go to System > Web Services > (SOAP/XMLRPC) Users. Click on the “Add New User” button for creating a new user. Under the “User Info” tab fill the information those required for creating a new user.
Click on the “Save User” button. Under “User Role” tab select the “Cloudways SOAP Role” radio button.
Click on the Save User and that’s it. We set up the Role and User for accessing the Magento API.
Consume the API in Magento
In this part, I show you how to call Magento APIs. Under your Document root directory, create a consume.php file and use the code below:
<?php // v2 call $client = new SoapClient('http://www.magentostore.com/api/v2_soap/?wsdl=1'); $session = $client->login('soapuser', 'soapuser'); $result = $client->cwcustomapimodule($session); $client->endSession($session); echo '<pre>'; print_r($result);
Change http://www.magentostore.com to your Magento store URL. Magento supports two versions of the APIs. v1 and v2. In this article, I show Magento v2 API method.
In this API mode first, I create new instance “SoapClient” class. Next, I call “Login” method for login to Magento store (We will need to provide “Username” and “Password” of the API User) which we created in the earlier section. Here we are all done.
This sums up the custom API development series. I hope after following this series you feel comfortable with the Magento APIs and you learned how to create your own Magento custom APIs as per your requirements. I would love to hear your comments about the tutorials and how you implemented your custom API.
If you’d like to practice what we’ve learned in this custom API series, you can sign up for your free trial of the Cloudways Managed Cloud Platform. Thanks to the Cloudways hosting stack, you get optimized and ultra fast Managed Magento Hosting for your web store, increasing your sales and improving your search engine rankings.
Boost Your Magento Store Performance by 5x Times & Maximize Your Sales
Our fastest Magento hosting can help you in growing your business revenue by 500%