{"id":12791,"date":"2020-10-06T10:13:55","date_gmt":"2020-10-06T10:13:55","guid":{"rendered":"https:\/\/www.mageworx.com\/blog\/?p=12791"},"modified":"2022-04-19T15:22:47","modified_gmt":"2022-04-19T15:22:47","slug":"getting-started-with-magento-web-apis-rest-soap","status":"publish","type":"post","link":"https:\/\/www.mageworx.com\/blog\/getting-started-with-magento-web-apis-rest-soap","title":{"rendered":"Getting Started with Magento Web APIs (REST &#038; SOAP)"},"content":{"rendered":"\n<!-- SEO Ultimate (http:\/\/www.seodesignsolutions.com\/wordpress-seo\/) - Code Inserter module -->\n<!-- Google Tag Manager (noscript) -->\r\n<noscript><iframe src=\"https:\/\/www.googletagmanager.com\/ns.html?id=GTM-5DTCW7B8\"\r\nheight=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"><\/iframe><\/noscript>\r\n<!-- End Google Tag Manager (noscript) -->\n<!-- \/SEO Ultimate -->\n\n<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p>The Magento 2 Web API framework offers users an opportunity to create new services that can communicate with third-party modules. They are Magento 2 supports REST and SOAP web services based on CRUD operations (create, read, update, and delete).<\/p>\n\n\n\n<p>In this article, we will explain the initial steps of using Magento 2 API. These APIs speed up the processing power and facilitate the transmission of data, such as products, customers, or orders, as well as transferring it to a third-party system. It also helps you manage the inventory.&nbsp; <\/p>\n\n\n\n<p>By automating the process, the usage of API helps you write less code. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Magento Web APIs?<\/h2>\n\n\n\n<p>The Magento web API is very easy to understand for developers looking to use web services that help communicate with the Magento system. These features are key to the API:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Magento 2 supports both <a href=\"https:\/\/www.mageworx.com\/blog\/magento-2-gift-cards-available-rest-api-requests\/\">REST (Representational State Transfer) <\/a>and SOAP (Simple Object Access Protocol).&nbsp;<\/li><li>It offers three types of authentication: 1) OAuth 1.0a for third-party application authentication, 2) the tokenization method for mobile application authentication, and 3) login credential for administration and customer verification. <\/li><li>You can configure any Magento or third-party web API by writing a few lines of XML.<\/li><li>It supports field filtering of web API that conserve the mobile response.&nbsp;<\/li><li>The current framework is based on the CRUD (create, read, update, delete) and search model. <\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What Can You Do With the Magento Web APIs?<\/h2>\n\n\n\n<p>APIs help various modules communicate with each other. They can be used to perform a wide array of tasks. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You can create online stores using Magento, and connect them with a physical system like POS (Point of Sale) to control the inventory globally&nbsp;<\/li><li>Easily integrate with CRM (Customer Relationship Management) or ERP (Enterprise Resource Planning) backend systems such as Salesforce, Microsoft Dynamics, or other sound software<\/li><li>It helps to connect with the CMS (Content Management System)<\/li><li>You can also create JavaScript widgets on the Magento backend (Admin panel) or Magento storefront<br><br><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Started: With Magento 2 Rest API<\/h2>\n\n\n\n<p>To create a web service role in Magento 2, follow these easy steps:<\/p>\n\n\n\n<p>1.Log in to the Magento 2 Admin Panel.<\/p>\n\n\n\n<p>2.Go to <strong>System<\/strong> &gt;&gt; <strong>User Roles<\/strong> and click the <strong>Add New Role<\/strong>button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/3C6KGBmMHszLauL6K0YnZ6clDPouXEcgcM_Ok79oQ2Du9Qcqfarbv0ATXAxDnCc2bVWB5ipbsG1VnqRoPz5OnXzqRS_Xl6n3dVYeUMkuRdbyGzzN4V9NV4cZS3xFScJCopXR_mYV\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>3. Enter the <strong>Role <\/strong>name.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/uq6p_eZiDaa37Mj25b8Jn053mTu3V34yXRz_VU_ddVLzAkJpVPXKMPymrVyGV5pwzo5aH_M61R0xXyue9G0ikc_gZJFAHtCJDEzjdex7ooK9sCy4i9ztXRJU4RZzJwb1JPk_7DfS\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>4. In the <strong>Your Password <\/strong>field, enter the current password of your Magento 2 Admin.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Bj0dKW_HaXnFzJj7icujLS5tLaECBJrQSit08kUBURV9g6DyisY5QGxQUaFoV6zlJ3vE1YooRpJQhbicu_Lz5cf37b0ZLQfQbzlihHh39-hRjMRqE-I6xJyCwN_b_e_FCPZxIh7A\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>5.On the left-hand side, click Role Resources. Under Resource Access, select only what is required for your web service.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/t1dHKDk0Ge7kIC3zpbqHtq4flVXwAQJrXTuQTROF0GaM-zGjEqc3c9jRL5-Kv6z-kaj7iUrfgBXs3y1MdsPagJlr11LYEozyXUwWogbGBR_GukxuWnIfkC184BL8ZosssweDGnxs\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>6.Once done, hit the Save Role.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Web Service User in Magento 2<\/h2>\n\n\n\n<p>Create a new user for the newly created role by following these steps:<\/p>\n\n\n\n<p>1.Go to <strong>System <\/strong>&gt;&gt; <strong>All Users,<\/strong> and then click <strong>Add New User<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/zPNULo3ycgYhkG4Nu-tUh72uyCfAJx9YcGHG-q1N1KTcAG5CHvvIKK6spuBc956dHnuMiFFyzunsLthMkG_htwTz6rviZwQTYX-l-7KOzPBoNDuffI3zzeUgZNSCdEB0tKPdEHEH\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>2.Enter the required information, including <strong>User Name<\/strong>,<strong> First<\/strong> and <strong>Last Name<\/strong>, <strong>Email<\/strong>, and <strong>Password<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/KYoGJtSY5v_3RT8x0TDKXhCNWuV2ILxLkltHUKpl9S-uFW0gy1D625d1t-aJK_jMukwkvQ9Rwdl-w3QbKQOl7aHjH6uKyklU1pVEHB9--yTLE9a2Ij0G6fQuSIGDShyP0NJ__Wud\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<p>3. On the left-hand side, click <strong>User Role<\/strong>, and then select the newly created role. Once done, click the <strong>Save User<\/strong>button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/uDQn95KzvLZsp-_ZgN1CQNl7dWdx4nFlnflyr2gAQSsyRE865LozMdI6YQBURn9tk1FrL8WLkVBOGL3SgrLMtv4AHuW4AoftJfUFm2CoepYU_pDjH1mbEYnWvbSpumnK9nHefoi8\" alt=\"Getting Started with Magento Web APIs | Mageworx Magento Blog\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Magento 2 REST API Authentication<\/h2>\n\n\n\n<p>Here, I will authenticate REST API through the token authentication method. This involves passing a username and password in the initial connection and receiving a token that will be saved in a variable for further calls.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/API URL for authentication\n$apiURL=\"http:\/\/magento-91647-257956.cloudwaysapps.com\/index.php\/rest\/V1\/integration\/admin\/token\";\n\/\/parameters passing with URL\n$data = array(\"username\" =&gt; \"username\", \"password\" =&gt; \"********\");\n$data_string = json_encode($data);\n$ch = curl_init($apiURL);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array(\"Content-Type: application\/json\",\"Content-Length: \".strlen($data_string)));\n$token = curl_exec($ch);\n\/\/decoding generated token and saving it in a variable\n$token=  json_decode($token); \n?&gt;<\/code><\/pre>\n\n\n\n<p><strong>Please note:<\/strong> The username &amp; password are used as an example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get Modules Using REST API in Magento 2<\/h2>\n\n\n\n<p>You can fetch data using the Magento 2 REST API. <a href=\"https:\/\/devdocs.magento.com\/guides\/v2.4\/rest\/bk-rest.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Here\u2019s a complete list of REST APIs for Magento<\/a> EE and CE.&nbsp;<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/Using above token into header\n$headers = array(\"Authorization: Bearer \".$token);\n\/\/API URL to get all Magento 2 modules\n$requestUrl='YourgeneratedUrl';\n$ch = curl_init($requestUrl);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n$result = curl_exec($ch);\n\/\/decoding result\n$result=  json_decode($result);\n\/\/printing result\nprint_r($result);\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>In the above code, I already passed the token (which was fetched earlier) with the API URL to get all the modules installed on the Magento 2 store.<\/p>\n\n\n\n<p>Here\u2019s the complete code.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/API URL for authentication\n$apiURL=\"URL\";\n\/\/parameters passing with URL\n$data = array(\"username\" =&gt; \"username\", \"password\" =&gt; \"*********\");\n$data_string = json_encode($data);\n$ch = curl_init($apiURL);\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array(\"Content-Type: application\/json\",\"Content-Length: \".strlen($data_string)));\n$token = curl_exec($ch);\n\/\/decoding generated token and saving it in a variable\n$token=  json_decode($token);\n\/\/******************************************\/\/\n\/\/Using above token into header\n$headers = array(\"Authorization: Bearer \".$token);\n\/\/API URL to get all Magento 2 modules\n$requestUrl='YourURL';\n$ch = curl_init($requestUrl);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n$result = curl_exec($ch);\n\/\/decoding result\n$result=  json_decode($result);\n\/\/printing result\nprint_r($result);\n?&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Use SOAP Services<\/h2>\n\n\n\n<p>Before we get into the SOAP services, it\u2019s important to learn about Magento\u2019s naming conventions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You have to use the CamelCase for naming services. For example, MyFirstModule<\/li><li>The string \u201cService\u201d is omitted, meaning you can\u2019t use it.<\/li><li>The \u201cMagento\u201d prefix and the \u201cInterface\u201d suffix are also escalated.<\/li><li>If your \u2018service\u2019 name is the same as the \u2018module\u2019 name, the module name will be omitted.<br><br><\/li><\/ul>\n\n\n<p><a href=\"https:\/\/www.mageworx.com\/magento-2-store-locator-and-pickup.html?utm_source=MW_blog&amp;utm_medium=banner&amp;utm_campaign=WEB_APIs_article\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12184 size-full\" src=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1.png\" alt=\"MageWorx Updates Roundup | MageWorx Blog\" width=\"1130\" height=\"190\" srcset=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1.png 1130w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1-600x101.png 600w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1-768x129.png 768w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1-250x42.png 250w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1-696x117.png 696w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2020\/04\/1130-\u0445-190-banner_1-1068x180.png 1068w\" sizes=\"auto, (max-width: 1130px) 100vw, 1130px\" \/><\/a><\/p>\n\n\n<p>So, make sure to keep these points in mind.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SOAP Services\u2015Authentication Using Magento 2<\/h2>\n\n\n\n<p>SOAP resources can be accessed using OAuth access tokens over HTTP. OAuth authentication is similar to the token method but supplies a more complex arrangement. Access tokens are strings that represent an access <a href=\"https:\/\/glossary.magento.com\/authorization\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">authorization<\/a> issued to the client.&nbsp;<br><\/p>\n\n\n\n<p>Here is the PHP script that explains how to get an access token:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$opts = &#91;\n            'http'=&gt; &#91;\n                'header' =&gt; 'Authorization: Bearer 36849300bca4fbff758d93a3379f1b8e'\n            ]\n        ];\n$wsdlUrl = 'http:\/\/magento.ll\/soap\/default?wsdl=1&amp;services=testModule1AllSoapAndRestV1';\n$serviceArgs = &#91;\"id\" =&gt; 1];\n\n$context = stream_context_create($opts);\n$soapClient = new SoapClient($wsdlUrl, &#91;'version' =&gt; SOAP_1_2, 'stream_context' =&gt; $context]);\n\n$soapResponse = $soapClient-&gt;testModule1AllSoapAndRestV1Item($serviceArgs); ?&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Final Words<\/h2>\n\n\n\n<p>This article outlined the basics of Magento web APIs, but there\u2019s a lot more you could learn. If you want a <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/devdocs.magento.com\/guides\/v2.4\/get-started\/create-integration.html\" target=\"_blank\">firmer grasp on the Magento 2 API usage<\/a>, then you should explore how methods are built-in Magento 2 and <a href=\"https:\/\/www.mageworx.com\/magento2-extensions.html?utm_source=MW_blog&amp;utm_medium=banner&amp;utm_campaign=WEB_APIs_article\">third-party extensions<\/a>. You should also understand how to pass the information to the needed system.<\/p>\n\n\n\n<p>So, that\u2019s it!<\/p>\n\n\n\n<p>To experience faster <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.cloudways.com\/en\/magento-hosting.php\" target=\"_blank\">Magento hosting<\/a> with peace of mind, check out this <a href=\"https:\/\/platform.cloudways.com\/signup\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">3-day free trial<\/a> on DigitalOcean, Vultr, and Linode.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>The Magento 2 Web API framework offers users an opportunity to create new services that can communicate with third-party modules. They are Magento 2 supports REST and SOAP web services based on CRUD operations (create, read, update, and delete). In this article, we will explain the initial steps of using Magento 2 API. These APIs [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":12792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[],"class_list":{"0":"post-12791","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-magento-2"},"_links":{"self":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/12791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/comments?post=12791"}],"version-history":[{"count":5,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/12791\/revisions"}],"predecessor-version":[{"id":15748,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/12791\/revisions\/15748"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/media\/12792"}],"wp:attachment":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/media?parent=12791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/categories?post=12791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/tags?post=12791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}