How to Create Custom API in Magento 2

Nov 24, 2016896Andrey Matveev
How to Create Custom API in Magento 2

Today I’d like to guide you through the process of creating Custom API in Magento 2.

At first, we must create the primary files of a module.

composer.json:

registration.php:

etc/module.xml:

The second step is – create an interface for custom API. Let’s create Api/TestInterface.php file and save it with the following content:

That’s how we created an interface with the following functions:

test() function, it doesn’t receive any parameters;

test1($param), it does receive 1 parameter in order of the call to our API.

Now we must create a model, that would work through our interface. Let’s create Model/Test.php file and write the following code into it:

We have just created a model, which is inherited by our interface and we described functions that are called in it. After that, we must connect our interface with the model. Please do the following to perform it:

Create etc/di.xml file with the following content:

Then we create a path to call our API. For that we write the following in etc/webapi.xml file:

Here we specified 2 paths to our functions:

/V1/test/ , which calls test method in TestInterface
/V1/test1/:param, which calls test1 method in TestInterface, receives parameter and transfers it into the function.

API is called through the GET method, but you can change it to POST, DELETE or some other methods if you want.

Also you are able to specify users group, that have permission to call your API or you can specify your own rule through the following structure, which is described in <route></route>:

And it’s possible to indicate Magento_Customer::group, self, Magento_Customer::admin and other rules, instead of anonymous.

It is possible to indicate additional parameters as well. For example we need to transfer customer_id parameter into the function through the interface.

For that purpose we will write the following between <route></route>:

Thus we should add a parameter in our interface and model, which will receive customer_id value.

Api/TestInterface.php:

Model/Test.php:

That’s all. We’ve just created custom API and now it’s ready to use.



5 Comments

  1. calling api rest/V1/test1 throwing “Request does not match any route.” error.

    first one is working.

  2. Hi Jafar,

    Thank you for your comment. There was a misprint in the article. Try to replace the following lines in the TestInterface.php file:
    * @param $param string
    * @param int $customerId
    * @return string

    by
    * @param int $customerId
    * @param string $param
    * @return string

    The Test.php file will contain the following function:
    /**
    * Test1 function
    *
    * @api
    * @param int $customerId
    * @param string $param
    * @return string
    */
    public function test1($customerId, $param) {
    return $customerId.$param;
    }

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *