How to add new Customer Address By REST API Magento 2?

Add Update Customer Address Using REST Web API in Magento 2 with URL rest/{store_code}/V1/customers/me and PUT Action method.

API is useful when you have to import customer addresses from a third-party platform. Just call REST API and you can import the address to the Magento.

There are two ways to Add or Update Customer Addresses using REST,

1Using Admin Panel Token as Header Parameter.

Using this way, It’s a simple one to add or update customer addresses.

Endpoint URL:

<HOST_URL>/rest/{store_code}/V1/customers/:customerId

Where store_code is your store code. Default store_code is the default.

Action: PUT

Headers:

Content-Type: application/json
Authorization Bearer {ADMIN_ACCESS_TOKEN}

Payload Request Body:

{
  "customer": {
    "addresses": [
      {
        "region": {
            "region_code": "TX",
            "region": "Texas",
            "region_id": 57
        },
        "country_id": "US",
        "street": [
            "Wonder Valley"
        ],
        "telephone": "1111122222",
        "postcode": "78701",
        "city": "Austin",
        "firstname": "Rakesh",
        "lastname": "Jesadiya",
        "default_shipping": true,
        "default_billing": true
      },
      {
        "region": {
            "region_code": "CA",
            "region": "California",
            "region_id": 12
        },
        "country_id": "US",
        "street": [
            "Horse Bouldvard"
        ],
        "firstname": "Joe",
        "lastname": "Broad",
        "telephone": "1234123412",
        "postcode": "45405",
        "city": "Los Angelos",
        "default_shipping": false,
        "default_billing": false
      }
    ]
  }
}

Using Admin Token as Header, you have to just pass addresses array as Payload Body and you don’t require email id, website id, or first name and last name.


2.  Using Customer Token as Header Parameter

You must require a customer token before adding a new address using REST API. This customer access token value will be passed in the Request Header.

Check the link,  Generate Customer Token by REST API.

Step by step guide to creating a new address using POSTMAN,

Endpoint URL:

<HOST_URL>/rest/{store_code}/V1/customers/me

Where store_code is your store code. By Default Magento comes with, store_code as default. You can get its value from the `store` database table.

Action:
PUT

Headers:

Content-Type: application/json
Authorization Bearer {CUSTOMER_ACCESS_TOKEN}

Payload Request Body:

{
  "customer": {
    "email": "myemail@fake1.com",
    "firstname":"Rakesh",
    "lastname":"Patel",
    "website_id": 1,
    "addresses": [
      {
        "region": {
            "region_code": "TX",
            "region": "Texas",
            "region_id": 57
        },
        "country_id": "US",
        "street": [
            "Wonder Valley"
        ],
        "telephone": "1111122222",
        "postcode": "78701",
        "city": "Austin",
        "firstname": "Rakesh",
        "lastname": "Jesadiya",
        "default_shipping": true,
        "default_billing": true
      },
      {
        "region": {
            "region_code": "CA",
            "region": "California",
            "region_id": 12
        },
        "country_id": "US",
        "street": [
            "Horse Bouldvard"
        ],
        "firstname": "Joe",
        "lastname": "Broad",
        "telephone": "1234123412",
        "postcode": "45405",
        "city": "Los Angelos",
        "default_shipping": false,
        "default_billing": false
      }
    ]
  }
}

In the Payload body,

first name, Last name, email, website_id are required fields.

Under the addresses object, all the fields are required which I mentioned in the above example.

You can assign default_shipping and default_billing as per your requirement.

When you run the given payload, Output will be display based on your data, and your addresses will be inserted into the customer address.