How to create a custom API in magento 2 which can be accessed by all the customers only? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How can I create a Custom SOAP/XML-RPC v1 & V2 Api in magento?Get All customers from Magento using rest APIHow to create custom API for custom shipping methodsHow to create custom SOAP api for magento 2.0?Magento 2 - How to create a new custom module APIlogging into magento 2 customers area using 3rd party APICan i get all customers, along with their address information, using the REST api?calling custom api in another custom api in Magento 2custom api to create customer in Magento2How to create a custom Api in magento 2 to add multiple products in cart using single APi call?

Statistical analysis applied to methods coming out of Machine Learning

Does the main washing effect of soap come from foam?

How do I say "this must not happen"?

Pointing to problems without suggesting solutions

NIntegrate on a solution of a matrix ODE

Can two people see the same photon?

Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

Flight departed from the gate 5 min before scheduled departure time. Refund options

What criticisms of Wittgenstein's philosophy of language have been offered?

An isoperimetric-type inequality inside a cube

Should man-made satellites feature an intelligent inverted "cow catcher"?

Can the Haste spell grant both a Beast Master ranger and their animal companion extra attacks?

How to ask rejected full-time candidates to apply to teach individual courses?

Why does BitLocker not use RSA?

By what mechanism was the 2017 UK General Election called?

How to resize main filesystem

.bashrc alias for a command with fixed second parameter

Centre cell vertically in tabularx

Fit odd number of triplets in a measure?

Marquee sign letters

Why are current probes so expensive?

What are some likely causes to domain member PC losing contact to domain controller?

Derived column in a data extension



How to create a custom API in magento 2 which can be accessed by all the customers only?



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How can I create a Custom SOAP/XML-RPC v1 & V2 Api in magento?Get All customers from Magento using rest APIHow to create custom API for custom shipping methodsHow to create custom SOAP api for magento 2.0?Magento 2 - How to create a new custom module APIlogging into magento 2 customers area using 3rd party APICan i get all customers, along with their address information, using the REST api?calling custom api in another custom api in Magento 2custom api to create customer in Magento2How to create a custom Api in magento 2 to add multiple products in cart using single APi call?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I need to create a custom API which can be accessed by all the customers only in magento 2 . Please help.










share|improve this question
























  • all customer means? login or guest or both?

    – Keyur Shah
    Apr 17 at 11:56

















1















I need to create a custom API which can be accessed by all the customers only in magento 2 . Please help.










share|improve this question
























  • all customer means? login or guest or both?

    – Keyur Shah
    Apr 17 at 11:56













1












1








1








I need to create a custom API which can be accessed by all the customers only in magento 2 . Please help.










share|improve this question
















I need to create a custom API which can be accessed by all the customers only in magento 2 . Please help.







magento2 magento2.2 customer api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 17 at 11:49









Muhammad Hasham

2,9612931




2,9612931










asked Apr 17 at 11:12









Meetali GuptaMeetali Gupta

587




587












  • all customer means? login or guest or both?

    – Keyur Shah
    Apr 17 at 11:56

















  • all customer means? login or guest or both?

    – Keyur Shah
    Apr 17 at 11:56
















all customer means? login or guest or both?

– Keyur Shah
Apr 17 at 11:56





all customer means? login or guest or both?

– Keyur Shah
Apr 17 at 11:56










1 Answer
1






active

oldest

votes


















0














First create app/code/Vendor/Module/etc/module.xml





Add registration.php in your module



<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);


There are two more additional configurations we need to add API capability to module, webapi.xml and di.xml. In webapi.xml we are configuring access rights and API Interface that specified method will use.



Web API configuration – etc/webapi.xml



<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/hello/name/:name" method="GET">
<service class="VendorModuleApiHelloInterface" method="name"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>


Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group. We will for now use anonymous so we can access it as a guest.



Define Interface – etc/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="VendorModuleApiHelloInterface"
type="VendorModuleModelHello" />
</config>


In di.xml we define what model will interface call to. We also need to add Interface and Model, please note that you need to take care about comments also.



Interface – Api/HelloInterface.php



<?php
namespace VendorModuleApi;

interface HelloInterface

/**
* Returns greeting message to user
*
* @api
* @param string $name Users name.
* @return string Greeting message with users name.
*/
public function name($name);



Model – Model/Hello.php



<?php
namespace VendorModuleModel;
use VendorModuleApiHelloInterface;

class Hello implements HelloInterface

/**
* Returns greeting message to user
*
* @api
* @param string $name Users name.
* @return string Greeting message with users name.
*/
public function name($name)
return "Hello, " . $name;





Communicating with new API call




Testing as guest



To test REST you can go to http://domain_name/rest/V1/method/attribute/value.



Example: http://magento2.loc/rest/V1/hello/name/Jim.



This is how response should look like for this example:



<response>Hello, Jim</response>


Adding ACL



If we don’t set anonymous in resource of webapi.xml, we need to set existing Magento resource or create our own. We can do that by adding acl.xml to etc.



ACL – etc/acl.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Vendor_Module::hello" title="Hello" translate="title" sortOrder="110" />
</resource>
</resources>
</acl>
</config>


Reference: https://inchoo.net/magento-2/magento-2-custom-api/



I hope this will help






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270474%2fhow-to-create-a-custom-api-in-magento-2-which-can-be-accessed-by-all-the-custome%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First create app/code/Vendor/Module/etc/module.xml





    Add registration.php in your module



    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
    );


    There are two more additional configurations we need to add API capability to module, webapi.xml and di.xml. In webapi.xml we are configuring access rights and API Interface that specified method will use.



    Web API configuration – etc/webapi.xml



    <?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/hello/name/:name" method="GET">
    <service class="VendorModuleApiHelloInterface" method="name"/>
    <resources>
    <resource ref="anonymous"/>
    </resources>
    </route>
    </routes>


    Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group. We will for now use anonymous so we can access it as a guest.



    Define Interface – etc/di.xml



    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="VendorModuleApiHelloInterface"
    type="VendorModuleModelHello" />
    </config>


    In di.xml we define what model will interface call to. We also need to add Interface and Model, please note that you need to take care about comments also.



    Interface – Api/HelloInterface.php



    <?php
    namespace VendorModuleApi;

    interface HelloInterface

    /**
    * Returns greeting message to user
    *
    * @api
    * @param string $name Users name.
    * @return string Greeting message with users name.
    */
    public function name($name);



    Model – Model/Hello.php



    <?php
    namespace VendorModuleModel;
    use VendorModuleApiHelloInterface;

    class Hello implements HelloInterface

    /**
    * Returns greeting message to user
    *
    * @api
    * @param string $name Users name.
    * @return string Greeting message with users name.
    */
    public function name($name)
    return "Hello, " . $name;





    Communicating with new API call




    Testing as guest



    To test REST you can go to http://domain_name/rest/V1/method/attribute/value.



    Example: http://magento2.loc/rest/V1/hello/name/Jim.



    This is how response should look like for this example:



    <response>Hello, Jim</response>


    Adding ACL



    If we don’t set anonymous in resource of webapi.xml, we need to set existing Magento resource or create our own. We can do that by adding acl.xml to etc.



    ACL – etc/acl.xml



    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
    <resources>
    <resource id="Magento_Backend::admin">
    <resource id="Vendor_Module::hello" title="Hello" translate="title" sortOrder="110" />
    </resource>
    </resources>
    </acl>
    </config>


    Reference: https://inchoo.net/magento-2/magento-2-custom-api/



    I hope this will help






    share|improve this answer



























      0














      First create app/code/Vendor/Module/etc/module.xml





      Add registration.php in your module



      <?php
      MagentoFrameworkComponentComponentRegistrar::register(
      MagentoFrameworkComponentComponentRegistrar::MODULE,
      'Vendor_Module',
      __DIR__
      );


      There are two more additional configurations we need to add API capability to module, webapi.xml and di.xml. In webapi.xml we are configuring access rights and API Interface that specified method will use.



      Web API configuration – etc/webapi.xml



      <?xml version="1.0"?>
      <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
      <route url="/V1/hello/name/:name" method="GET">
      <service class="VendorModuleApiHelloInterface" method="name"/>
      <resources>
      <resource ref="anonymous"/>
      </resources>
      </route>
      </routes>


      Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group. We will for now use anonymous so we can access it as a guest.



      Define Interface – etc/di.xml



      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="VendorModuleApiHelloInterface"
      type="VendorModuleModelHello" />
      </config>


      In di.xml we define what model will interface call to. We also need to add Interface and Model, please note that you need to take care about comments also.



      Interface – Api/HelloInterface.php



      <?php
      namespace VendorModuleApi;

      interface HelloInterface

      /**
      * Returns greeting message to user
      *
      * @api
      * @param string $name Users name.
      * @return string Greeting message with users name.
      */
      public function name($name);



      Model – Model/Hello.php



      <?php
      namespace VendorModuleModel;
      use VendorModuleApiHelloInterface;

      class Hello implements HelloInterface

      /**
      * Returns greeting message to user
      *
      * @api
      * @param string $name Users name.
      * @return string Greeting message with users name.
      */
      public function name($name)
      return "Hello, " . $name;





      Communicating with new API call




      Testing as guest



      To test REST you can go to http://domain_name/rest/V1/method/attribute/value.



      Example: http://magento2.loc/rest/V1/hello/name/Jim.



      This is how response should look like for this example:



      <response>Hello, Jim</response>


      Adding ACL



      If we don’t set anonymous in resource of webapi.xml, we need to set existing Magento resource or create our own. We can do that by adding acl.xml to etc.



      ACL – etc/acl.xml



      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
      <acl>
      <resources>
      <resource id="Magento_Backend::admin">
      <resource id="Vendor_Module::hello" title="Hello" translate="title" sortOrder="110" />
      </resource>
      </resources>
      </acl>
      </config>


      Reference: https://inchoo.net/magento-2/magento-2-custom-api/



      I hope this will help






      share|improve this answer

























        0












        0








        0







        First create app/code/Vendor/Module/etc/module.xml





        Add registration.php in your module



        <?php
        MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
        );


        There are two more additional configurations we need to add API capability to module, webapi.xml and di.xml. In webapi.xml we are configuring access rights and API Interface that specified method will use.



        Web API configuration – etc/webapi.xml



        <?xml version="1.0"?>
        <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/hello/name/:name" method="GET">
        <service class="VendorModuleApiHelloInterface" method="name"/>
        <resources>
        <resource ref="anonymous"/>
        </resources>
        </route>
        </routes>


        Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group. We will for now use anonymous so we can access it as a guest.



        Define Interface – etc/di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="VendorModuleApiHelloInterface"
        type="VendorModuleModelHello" />
        </config>


        In di.xml we define what model will interface call to. We also need to add Interface and Model, please note that you need to take care about comments also.



        Interface – Api/HelloInterface.php



        <?php
        namespace VendorModuleApi;

        interface HelloInterface

        /**
        * Returns greeting message to user
        *
        * @api
        * @param string $name Users name.
        * @return string Greeting message with users name.
        */
        public function name($name);



        Model – Model/Hello.php



        <?php
        namespace VendorModuleModel;
        use VendorModuleApiHelloInterface;

        class Hello implements HelloInterface

        /**
        * Returns greeting message to user
        *
        * @api
        * @param string $name Users name.
        * @return string Greeting message with users name.
        */
        public function name($name)
        return "Hello, " . $name;





        Communicating with new API call




        Testing as guest



        To test REST you can go to http://domain_name/rest/V1/method/attribute/value.



        Example: http://magento2.loc/rest/V1/hello/name/Jim.



        This is how response should look like for this example:



        <response>Hello, Jim</response>


        Adding ACL



        If we don’t set anonymous in resource of webapi.xml, we need to set existing Magento resource or create our own. We can do that by adding acl.xml to etc.



        ACL – etc/acl.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
        <acl>
        <resources>
        <resource id="Magento_Backend::admin">
        <resource id="Vendor_Module::hello" title="Hello" translate="title" sortOrder="110" />
        </resource>
        </resources>
        </acl>
        </config>


        Reference: https://inchoo.net/magento-2/magento-2-custom-api/



        I hope this will help






        share|improve this answer













        First create app/code/Vendor/Module/etc/module.xml





        Add registration.php in your module



        <?php
        MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
        );


        There are two more additional configurations we need to add API capability to module, webapi.xml and di.xml. In webapi.xml we are configuring access rights and API Interface that specified method will use.



        Web API configuration – etc/webapi.xml



        <?xml version="1.0"?>
        <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/hello/name/:name" method="GET">
        <service class="VendorModuleApiHelloInterface" method="name"/>
        <resources>
        <resource ref="anonymous"/>
        </resources>
        </route>
        </routes>


        Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group. We will for now use anonymous so we can access it as a guest.



        Define Interface – etc/di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="VendorModuleApiHelloInterface"
        type="VendorModuleModelHello" />
        </config>


        In di.xml we define what model will interface call to. We also need to add Interface and Model, please note that you need to take care about comments also.



        Interface – Api/HelloInterface.php



        <?php
        namespace VendorModuleApi;

        interface HelloInterface

        /**
        * Returns greeting message to user
        *
        * @api
        * @param string $name Users name.
        * @return string Greeting message with users name.
        */
        public function name($name);



        Model – Model/Hello.php



        <?php
        namespace VendorModuleModel;
        use VendorModuleApiHelloInterface;

        class Hello implements HelloInterface

        /**
        * Returns greeting message to user
        *
        * @api
        * @param string $name Users name.
        * @return string Greeting message with users name.
        */
        public function name($name)
        return "Hello, " . $name;





        Communicating with new API call




        Testing as guest



        To test REST you can go to http://domain_name/rest/V1/method/attribute/value.



        Example: http://magento2.loc/rest/V1/hello/name/Jim.



        This is how response should look like for this example:



        <response>Hello, Jim</response>


        Adding ACL



        If we don’t set anonymous in resource of webapi.xml, we need to set existing Magento resource or create our own. We can do that by adding acl.xml to etc.



        ACL – etc/acl.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
        <acl>
        <resources>
        <resource id="Magento_Backend::admin">
        <resource id="Vendor_Module::hello" title="Hello" translate="title" sortOrder="110" />
        </resource>
        </resources>
        </acl>
        </config>


        Reference: https://inchoo.net/magento-2/magento-2-custom-api/



        I hope this will help







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 17 at 11:42









        Muhammad HashamMuhammad Hasham

        2,9612931




        2,9612931



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270474%2fhow-to-create-a-custom-api-in-magento-2-which-can-be-accessed-by-all-the-custome%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sum ergo cogito? 1 nng

            419 nièngy_Soadمي 19bal1.5o_g

            Queiggey Chernihivv 9NnOo i Zw X QqKk LpB