In this post, I am explaining to you to add/update many customer addresses via Magento 2 customer address rest api.
When we have to add multiple addresses at once through REST API, we cannot add the customer address through the default API of Magento 2.
For how to add a new customer address using api in Magento 2 we have to use the endpoint URL {store_url}/rest/V1/address and POST Action method.
Magento 2 REST API Add/Update Many Customer Address
Magento 2 Add a New Customer Address API help you to add as many addresses for a single customer without updating the old address.
I'm explaining to you by web API module to add a new customer address using Magento 2 REST API.
Endpoint:
https://wishusucess.com/rest/V1/addresses/
Method: POST
Header:
- Authorization: Bearer Admin_Token
- Content-Type: application/json
Step 1: Registration of Customer API
In the first step of adding/updating the customer address via Magento 2 rest API, we have to register the module via the registration.php file.
Wishusucess/AddAddress/registration.php
<?php /* * Developer: Magento 2x Developer Hemant Kumar Singh * Category: Customer REST API */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_AddAddress', __DIR__ );
Step 2: Create Module File
In this step, we have to give the basic information of our edit information of customer using api.
Wishusucess/AddAddress/etc/module.xml
<?xml version="1.0"?> <!-- /* * Developer: Magento 2x Developer Hemant Kumar Singh * Category: Customer REST API */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Wishusucess_AddAddress" setup_version="1.0.0" schema_version="1.0.0"> </module> </config>
Step 3: Routing of Add/Update Customer API
Now in this step, we have to define the route id via a web API XML file. This file has all the details of routing and the method that we have to select while hitting the endpoint.
Wishusucess/AddAddress/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"> <!-- Routing define --> <route url="/V1/addresses" method="POST"> <service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/> <resources> <resource ref="Magento_Customer::manage"/> </resources> </route> </routes>
Check-in body and response detail about Add Multiple Customer Address via Magento 2 REST API
Edit Customer Address REST API in Magento 2
Now in order to update the customer address, you have to create a rest API.
Below are the details of editing customer addresses using rest API for a magento 2 store.
You can get the body details
Endpoint:
https://wishusucess.com/rest/V1/addresses/
Method: POST
Header:
- Authorization: Bearer Admin_Token
- Content-Type: application/json
Step 1: Register Update Address API
Wishusucess/UpdateAddress/registration.php
<?php /* * Developer: Magento 2x Developer Hemant Kumar Singh * Category: Customer REST API */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_UpdateAddress', __DIR__ );
Step 2: Create Module File for API
Wishusucess/UpdateAddress/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Wishusucess_UpdateAddress" setup_version="1.0.0" schema_version="1.0.0"> </module> </config>
Step 3: Create Web API XML File
Wishusucess/UpdateAddress/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/addresses" method="POST"> <service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/> <resources> <resource ref="Magento_Customer::manage"/> </resources> </route> </routes>
Read more: What should be the body details to Update Customer address with REST API in Magento 2.
Conclusion:
To add or update the customer address of Magento 2 you need to create a custom web api as mentioned above.
Using this module you can add any number of addresses for the customer or you can use the rest api to edit it.