Add Multiple Customer Address via Magento 2 REST API

You have to implement a module to add multiple customer address for customers in Magento 2 store through rest api

This feature is not available in Magento 2 default REST API which can be used to add multiple customer addresses at once.

For this, I will explain to you to step by step method how you can save more than one customer address through API by implementing an extension.

 

Add Multiple Customer Address via REST API

As you can see on the screen below, this is the customer dashboard of the Magento 2.4 store.

Here the customer can save many addresses for himself, but the API for this is not available by default.

For this you will have to add some code, then by using it, you can save the data through API or

Add Multiple Customer Address

 

 

Step 1: Get Admin Token

First of all, you have to get an admin token then add that token as a header in Next API and send it along with.

Endpoint:

https://wishusucess.com/rest/V1/integration/admin/token

Method: POST

Body:

{
"username":"admin45",
"password":"Ainy@#13"
}

Response:

eiv9xouqxyxrasuz344k2azf2w6jtjsw

 

Step 2: Get Customer ID Using Customer Email

In the second step, we have to get the customer id detail by using email id via rest api.

In order to get the customer id via email id, you have to create a webapi route so you have to create a module also.

Endpoint:

https://wishusucess.com/rest/V1/getcustomerloginidusingemail

Method: POST

Header:

  • Content-Type: application/json
  • Authorization = Bearer admin_token

Body:

{"email":"cs.hemantsinghtech9@gmail.com"}

Response:

111

 

Step 3: Add Multiple Customer Address Using REST API

Now you can save the multiple customer addresses by using the below customer add address rest api in Magento 2.

So you have to send the address detail in the body along with the customer id.

You can get this below rest api extension via following the link of add/update many customer addresses via REST API in Magento 2.

Endpoint:

https://wishusucess.com/rest/V1/addresses

Method: POST

Header:

  • Authorization: Bearer <Admin Token>
  • Content-Type: Application/json

Body:

{
"address": { 
"customer_id": "111",
"defaultShipping": true,
"defaultBilling": true,
"region": "India",
"country_id": "IN",
"street": [ "201302, Noida" ],
"postcode": "203302",
"city": "Noida",
"firstname": "Hemant23",
"lastname": "Singh34", 
"telephone": "77777777777",
"countryId": "IN"
}
}

Save Customer Address via API
Save Customer Address via API

 

Response:

{
"id": 230,
"customer_id": 111,
"region": {
"region_code": null,
"region": null,
"region_id": 0
},
"region_id": 0,
"country_id": "IN",
"street": [
"201302, Noida"
],
"telephone": "77777777777",
"postcode": "203302",
"city": "Noida",
"firstname": "Hemant23",
"lastname": "Singh34"
}

 

Read more: Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

 

Conclusion:

As you can see in the details, we can do whatever address is to be saved through Rest API.

For that, we will need to write some custom code which we have told you in this post.

Using the above-mentioned details, you can save any number of addresses for the customer through Test API.