How to Create Custom API and Customer in Magento 2?

| |
Comments: 0
How to Create Custom API and Customer in Magento 2?

In this blog we learn about How to Create Custom API in Magento 2.Using API you can speed up getting,sending and processing data of any products, any customer and any orders. Using API we can transfer data automatically so it’s easier than manual export and import.

What is API Magento2?

API stands for Application Programming Interface. Magento API is a framework that provides developers with web services which communicate well with the Magento system and it also provides the ability to manage ecommerce stores effectively.

There are mainly two types of web API used in Magento 2:

1) REST(Representational State Transfer) API

2)SOAP (Simple Object Access Protocol) API

How does Magento 2 API work?

Generally We use the REST API in Magento 2. Using REST API the first step is defining route path.The routes are defined in 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/test/” method=”POST”>
<service class=”Mageants\Hello\Api\TestInterface” method=”setData”/>
<resources>
<resource ref=”anonymous”/>
</resources>
</route>
</routes>

In above example the route tag have url attribute that defines the route /V1 /hello/test/ and method is Post.In service tag the class attribute defines service contract Mageants\Hello\Api\TestInterface with the route.

Magento 2 Create Custom API: Step by Step

To Create Custom API in Magento 2, follow below steps:

Step 1: Create webapi.xml file in the app/code/Mageants/Blog/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/test/” method=”POST”>
<service class=”Mageants\Blog\Api\TestInterface” method=”setData”/>
<resources>
<resource ref=”anonymous”/>
</resources>
</route>
</routes>

Here we use post method and route /V1 /hello/test/.

Step 2: Now Create a di.xml file in the app/code/Mageants/Blog/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=”Mageants\Blog\Api\TestInterface” type=”Mageants\Blog\Model\Test”/>
</config>

Step 3: Create a TestInterface.php at app/code/Mageants/Blog/Api/TestInterface.php.

<?php
namespace Mageants\Blog\Api;
interface TestInterface
{
/**
* POST for test api
* @param string[] $data
* @return string
*/
public function setData($data);
}

Step 4: Create Test.php file at app/code/Mageants/Blog/Model/Test.php.

<?php
namespace Mageants\Blog\Model;
use Mageants\Blog\Api\TestInterface;
use Mageants\Blog\Model\PostFactory;
use Mageants\Blog\Model\ResourceModel\Post\CollectionFactory;
class Test implements TestInterface
{
private $PostFactory;
private $CollectionFactory;
public function __construct(PostFactory $PostFactory,CollectionFactory $CollectionFactory)
{
$this->PostFactory = $PostFactory;
$this->CollectionFactory = $CollectionFactory;
}
/**
* {@inheritdoc}
*/
public function setData($data)
{
$name =$data[‘name’];
$number =$data[‘number’];
$city =$data[‘city’];
$insertData = $this->PostFactory->create();
$insertData->setName($name)->save();
$insertData->setNumber($number)->save();
$insertData->setCity($city)->save();
return ‘successfully saved’;
}
}

After follow above steps run setup upgrade and deploy command and verify the custom rest api using [Website/domain]/swagger.

Using above example we will set the custom data in database using API.

Create Customer using Rest API in Magento 2: Steps

Step 1: Create a customer account

Using Post request enter store url along with endpoint like :

POST /rest/V1/integration/admin/token

In our case

POST http://127.0.0.1/magento_237custom/rest/V1/integration/admin/token

Now enter your Magento admin user name and password.

For Create customer account create a new request using End Point

POST /rest//V1/customers

To Create simple customer account, write below code in body section in the postman:

{
“customer” : {
“email” : “test@example.com”,
“firstname” : “test-firstname”,
“lastname” : “test-lastname”,
“addresses” : [
{
“defaultBilling” : true,
“defaultShipping” : true,
“firstname” : “test-firstname”,
“lastname” : “test-lastname”,
“region” : {
“regionCode” : “NY”,
“regionId” : 43,
“region” : “New York”
},
“countryId” : “US”,
“postcode” : “11501”,
“city” : “Mineola”,
“street” : [
“160 1st St.”
],
“telephone” : “516-555-1111”
}
]
},
“password” : “Password123”
}
{
“id”: 13,
“group_id”: 1,
“default_billing”: “6”,
“default_shipping”: “6”,
“created_at”: “2022-02-02 12:34:02”,
“updated_at”: “2022-02-02 12:34:02”,
“created_in”: “Default Store View”,
“email”: “test@example.com”,
“firstname”: “test-firstname”,
“lastname”: “test-lastname”,
“store_id”: 1,
“website_id”: 1,
“addresses”: [
{
“id”: 6,
“customer_id”: 13,
“region”: {
“region_code”: “NY”,
“region”: “New York”,
“region_id”: 43
},
“region_id”: 43,
“country_id”: “US”,
“street”: [
“160 1st St.”
],
“telephone”: “516-555-1111”,
“postcode”: “11501”,
“city”: “Mineola”,
“firstname”: “test-firstname”,
“lastname”: “test-lastname”,
“default_shipping”: true,
“default_billing”: true
}
],
“disable_auto_group_change”: 0,
“extension_attributes”: {
“is_subscribed”: false
}
}

Step 2: Verify the above email and password.

– Using test@example.com email and Password123 password login into luma site.

– Click in My Account and check first name,last name and other details.

Conclusion:

With above steps and examples we have created a custom rest api and created a customer using rest api in Magento 2.In case issue regarding above example, you can contact us.

Recommended Tutorial For You!

How to Create Custom Page in Magento 2?

How to Apply Schema Patch in Magento 2?

Leave a Reply

Your email address will not be published. Required fields are marked *