Magento 2 Add a New Customer Address via API

addressapimagento2restsoap

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":"customer@example.com",
    "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"
            ]
        }
    ]
}
}

Best Answer

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":"cusaaastomesr@example.com",
    "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

Related Topic