4

I'm trying to add a new customer address using Magento 2 REST API in C#. I'm using POST /V1/customers/:id. I keep getting the below error. Where do I find the regionId? Is there another API I can use to get it because I can't easily query the Magento database everytime.

{  
   "message":"%fieldName is a required field.",
   "parameters":{  
      "fieldName":"regionId"
   }
}

Here is the JSON request:

{
"customer":{
    "email":"[email protected]",
    "firstname":"John",
    "lastname":"Doe",
    "websiteId":"1",
    "addresses":[
        {
            "customer_id":"1",
            "firstname":"John",
            "lastname":"Doe",
            "company":"ABC Manufacturing",
            "telephone":"555-555-5555",
            "city":"Boston",
            "region":"Massachusetts",
            "postcode":"02115",
            "country_id":"US",
            "street":[
                "123 Main Street",
                "PO Box 321"
            ]
        }
    ]
}
}

1 Answer 1

6

You will get all the customer address related attributes by calling below url :

http://yourwebsite.com/index.php/rest//V1/attributeMetadata/customerAddress/

Method Name : GET

Same way for Authorization Bearer youraccesstoken

You will get the all the address related attributes name and its values like state , country etc.

Their you will find RegionId for your region ( Example : Massachusetts = 32) Where 32 is region Id.

At last pass below parameters to add customer address with same url.

{
"customer":{
    "email":"[email protected]",
    "firstname":"John",
    "lastname":"Doe",
    "websiteId":"1",
    "addresses":[
        {
            "customer_id":"3",
            "region_id": 32, // RegionId must needs to pass
            "country_id":"US",
            "street":[
                "123 Main Street",
                "PO Box 321"
            ],
            "firstname":"John",
            "lastname":"Doe",
            "company":"ABC Manufacturing",
            "telephone":"555-555-5555",
            "city":"Boston",
            "postcode":"02115"
        }
    ]
}
}

It will update address of customer and your error will resolved.

Refer this link for all api details - http://devdocs.magento.com/guides/v2.0/rest/list.html or https://r-martins.github.io/m1docs/guides/v2.4/rest/list.html

4
  • This is not working for me. Getting blank list in response Oct 22, 2017 at 7:01
  • @GauravAgrawal - check your parameters all are there or not ? also check your method and access token as well , if still getting blank then post your issue as an new question and comment link here then i will check it and let you know for the same Oct 23, 2017 at 5:16
  • @ManthanDave I tried this code working. one up vote. it is creating new address in customer but all other addresses deleted. can you guide me to add new address with rest api Apr 17, 2020 at 12:54
  • @TirthPatel You need to add an array of addresses, so when you get customers from API you get addresses but also add those addresses as well as new addresses in this request body. Apr 10, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.