{"id":193,"date":"2024-02-01T20:07:13","date_gmt":"2024-02-01T20:07:13","guid":{"rendered":"https:\/\/davagordon.co.uk\/blog\/?p=193"},"modified":"2024-05-12T02:31:18","modified_gmt":"2024-05-12T01:31:18","slug":"beginners-guide-to-building-your-first-magento-2-api","status":"publish","type":"post","link":"https:\/\/davagordon.co.uk\/blog\/beginners-guide-to-building-your-first-magento-2-api\/","title":{"rendered":"Beginner&#8217;s Guide to Building your first Magento 2 API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2, the king of e-commerce systems, provides an excellent chance for newcomers to dip their toes into the realm of bespoke APIs. In this step-by-step guide, we&#8217;ll break down the process into manageable phases, allowing even individuals with no coding expertise to establish their own bespoke Magento 2 API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Make Your Workspace Ready<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before anything else, ensure that your development environment is set up. If you&#8217;re new to this, try using technologies like <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener\" title=\"Docker\">Docker<\/a> or <a href=\"https:\/\/www.vagrantup.com\/\" target=\"_blank\" rel=\"noopener\" title=\"Vagrant\">Vagrant<\/a> to make the process easier. Make sure you have a web server, <a href=\"https:\/\/www.php.net\/\" target=\"_blank\" rel=\"noopener\" title=\"PHP\">PHP<\/a>, <a href=\"https:\/\/getcomposer.org\/\" target=\"_blank\" rel=\"noopener\" title=\"Composer\">Composer<\/a>, and <a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\" title=\"MySQL\">MySQL<\/a> database ready to go.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Construct your first module.<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"wp-block-paragraph\">In Magento 2, functionalities are grouped into modules. Creating a new module is the first step in developing a custom API. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Begin by establishing the module structure using files such as registration.php and module.xml. Don&#8217;t worry, you may duplicate these structures from other modules!<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\"><div class=\"wp-block-image is-style-rounded\">\n<figure class=\"aligncenter size-medium is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/davagordon.co.uk\/blog\/wp-content\/uploads\/2024\/02\/magento2-modules-300x300.jpeg\" alt=\"\" class=\"wp-image-201\" style=\"aspect-ratio:1;object-fit:cover;width:238px;height:auto\" srcset=\"https:\/\/davagordon.co.uk\/blog\/wp-content\/uploads\/2024\/02\/magento2-modules-300x300.jpeg 300w, https:\/\/davagordon.co.uk\/blog\/wp-content\/uploads\/2024\/02\/magento2-modules-150x150.jpeg 150w, https:\/\/davagordon.co.uk\/blog\/wp-content\/uploads\/2024\/02\/magento2-modules-768x768.jpeg 768w, https:\/\/davagordon.co.uk\/blog\/wp-content\/uploads\/2024\/02\/magento2-modules.jpeg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n<\/div><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 Navigate to the &#8216;app\/code&#8217; Directory<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Magento modules are typically located under the <code>app\/code<\/code> directory. Navigate to it using the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd path\/to\/your\/magento2\/app\/code\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 Create Your Module Directory<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s make a separate directory for your module. Replace Vendor and Module with names appropriate for your project:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmkdir -p Vendor\/Module\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 Create Module Files<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Enter your module directory and create the required files. The essential structure of your module is defined by these files, which include <code>registration.php<\/code> and <code>module.xml<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd Vendor\/Module\ntouch registration.php\nmkdir -p etc\ntouch etc\/module.xml\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 Customize Registration.php<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open <code>registration.php<\/code> in your preferred text editor and add the following content:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nuse \\Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    &#039;Vendor_Module&#039;,\n    __DIR__\n);\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5 Configure Module.xml<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, move into the <code>etc<\/code> directory and edit <code>module.xml<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd etc\ntouch module.xml\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Open <code>module.xml<\/code> and insert:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Module\/etc\/module.xsd&quot;&gt;\n    &lt;module name=&quot;Vendor_Module&quot; setup_version=&quot;1.0.0&quot;\/&gt;\n&lt;\/config&gt;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.6 Activate Your Module<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s time to tell Magento about your module. Execute the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nphp bin\/magento module:enable Vendor_Module\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2.7 Run Setup Upgrade<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, update the Magento database to incorporate your new module:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nphp bin\/magento setup:upgrade\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">With these steps, you have successfully constructed and activated your first Magento 2 module, laying the groundwork for the construction of your custom API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Design Your Custom API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a folder named <code>api<\/code> inside your module. Within this, create two important files: <code>api.xml<\/code> and your API interface file. Think of <code>api.xml<\/code> as your API&#8217;s blueprint, and the interface file as a list of the tasks your API will do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a simple example for api.xml:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;routes xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:module:Magento_Webapi:etc\/webapi.xsd&quot;&gt;\n    &lt;route url=&quot;\/V1\/custom-api-endpoint&quot; method=&quot;GET&quot;&gt;\n        &lt;service class=&quot;Vendor\\Module\\Api\\CustomApiInterface&quot; method=&quot;customMethod&quot;\/&gt;\n        &lt;resources&gt;\n            &lt;resource ref=&quot;anonymous&quot;\/&gt;\n        &lt;\/resources&gt;\n    &lt;\/route&gt;\n&lt;\/routes&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And the interface file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/\/ Vendor\\Module\\Api\\CustomApiInterface.php\n\nnamespace Vendor\\Module\\Api;\n\ninterface CustomApiInterface\n{\n    \/**\n     * Custom API Method\n     *\n     * @return string\n     *\/\n    public function customMethod();\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 4: Add Magic to the API.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, in your API interface file, include some basic logic for your function. Let&#8217;s keep things pleasant and straightforward:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/\/ Vendor\\Module\\Model\\Api\n\nnamespace Vendor\\Module\\Model\\Api;\n\nuse Vendor\\Module\\Api\\CustomApiInterface;\n\nclass CustomApi implements CustomApiInterface\n{\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function customMethod()\n    {\n        return &quot;Hello from your custom API!&quot;;\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 5: Secure Your API Playground.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid problems, setup access control using ACLs. This is equivalent to putting a password to your API, ensuring that only authorised users may play.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Playtime! Test your very own API.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before launching your new idea, test it in a safe environment. Magento provides testing tools to ensure that everything operates well. Try multiple scenarios to ensure that your API works as anticipated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Publish your Magic Manual.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Provide clear, easy instructions for your unique API. This helps others (and you in the future!) understand how to utilise it. Outline what your API can do, what it requires, and what it returns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Showtime! Deploy and Monitor.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When everything seems okay, deploy your new custom API on the big stage. Keep an eye on its performance in case there are any unforeseen surprises.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations on taking your first steps in the world of Magento 2 custom APIs! You&#8217;ve created a module that lays the groundwork for a one-of-a-kind e-commerce experience. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re looking for more information and want to explore other lessons and insights, we&#8217;ve got you covered. Explore our comprehensive selection of <a href=\"https:\/\/davagordon.co.uk\/blog\/category\/magento-tips-insights-best-practices\/\" title=\"Magento\">Magento tutorials<\/a> and uncover a plethora of materials to help you improve your e-commerce skills.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy coding, and may your Magento journey be full of discoveries and success!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2, the king of e-commerce systems, provides an excellent chance for newcomers to dip their toes into the realm of bespoke APIs. In this step-by-step guide, we&#8217;ll break down the process into manageable phases, allowing even individuals with no coding expertise to establish their own bespoke Magento 2 API. Step 1: Make Your Workspace [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":207,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[34,14,10,11],"class_list":["post-193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-tips-insights-best-practices","tag-api","tag-ecommerce","tag-how-to","tag-magento"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":9,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions\/204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/media\/207"}],"wp:attachment":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}