How to create Magento2 Rest api

How to create Magento2 Rest api

Magento2 Rest api, So folks In today’s topic I am going to cover how you can create Magento2 Rest api.

Magento 2 rest api is used to send or receive any data from magento database, Magento Rest api can be used to develop mobile app and integrate with any 3rd party applicaiton or develop headless ecommerce projects.

You can use magento rest api over http protocol.

There are various action for magento rest api which are GET,POST,PUT,DELETE , you can use any of this method as per your requirements.

Magento2 rest api example

First of all you need to create a module,

Now you need to create a webapi.xml file inside etc folder and add below code

<?xml version="1.0" ?>
<routes xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="GET" url="/V1/posts">
        <service class="Mageacademy\RestApiExample\Api\PostRepositoryInterface" method="getList"/>
            <resource ref="anonymous"/>

Here you can see route node inside routes node and we have define method as “GET” , you can define GET , POST , PUT or DELETE as per your requirement,

url : It will be your api endpoints , It should be unique path. you can define as per your requirements

then you can see service node and below are attributes for service node

class : you need to define service interface path which will be responsible to receive request and send response.

method : This will be your function name of interface.

We will cover about resources in our next blog , for now you can use anonymous as shown in above code.

Now you need to create PostRepositoryInterface interface inside NameSpace/ModuleName/Api Folder and add code as below


namespace Mageacademy\RestApiExample\Api;

interface PostRepositoryInterface
     * Returns Featured Brands to user
     * @api
     * @param No params.
     * @return string

    public function getList();


Here you can see we have define PostRepositoryInterface and add 1 function getList(). Please note Docblocks is required for this interface then only your magento rest api will work.

Now you need to create Model class file PostRepository under Model folder and add code as below

namespace Mageacademy\RestApiExample\Model;

use Mageacademy\RestApiExample\Api\PostRepositoryInterface;

class PostRepository implements PostRepositoryInterface

     * Return Response for Rest api
     * @api
     * @param No params.
     * @return string[]
    public function getList()
        return "First Rest Api Example";

}//end class

Here you can see we have implemented PostRepositoryInterface in PostRepository Model class file and it’s function getList. we have passed a simple data as a response.

Now we need to create di.xml file and add code as below

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Mageacademy\RestApiExample\Api\PostRepositoryInterface" type="Mageacademy\RestApiExample\Model\PostRepository"/>


Now you need to enable your module and run setup upgrade command.

Now in Postman call this api as shown in screenshot below

Hope you like our this blog, Stay connected with us for more relative information.

In our next blog we will cover more detailed information in Magento2 rest api.

Subscribe to our newsletter

Leave a Reply
Your email address will not be published. *