6

Does the Magento 2 REST API support updating a single customer address?

This endpoint is incomplete: customerAddressRepositoryV1

GET /V1/customers/addresses/{addressId}
DELETE /V1/addresses/{addressId}

This endpoint allows adding/updating of addresses: customerCustomerRepositoryV1:

PUT /V1/customers/{id}

To update an existing address specify the address id property (table customer_address_entity column entity_id). For example, to update address id = 16:

{
    "customer": {
        "email": "[email protected]",
        "firstname": "Test",
        "lastname": "Test",
        "store_id": 1,
        "website_id": 1,
        "addresses": [{
            "id": 16,
            "customerId": 1,
            "region": {
                "region_code": "CA",
                "region": "California"
            },
            "region_id": 12,
            "countryId": "US",
            "street": [
                "123 Test St."
            ],
            "telephone": "555 555 5555",
            "postcode": "90210",
            "city": "Beverly Hills",
            "defaultShipping": false,
            "defaultBilling": true
        }]
    }
}

However, if a customer has more than one address then all other addresses are deleted from the customer. We want to update the address (but NOT delete other addresses). It also seems redundant to have to pass customer information e.g. email. If this feature doesn't exist we'll need to create our own endpoint.

3 Answers 3

3

Matthew

No, M2.2 even does not support Customer Address save/update. What you can do with current implementation is:

  1. Get full customer information:

    curl -X GET http://your.magento2.com/rest/default/V1/customers/ID \ -H 'authorization: Bearer TOKEN'

  2. Put full customer information updating the desired address/es:

    curl -X PUT http://your.magento2.com/rest/default/V1/customers/ID \ -H 'authorization: Bearer TOKEN'

Or declare in your own module two new endpoints into webapi.xml, since AddressRepository already have getList and UpdateAddressCollection (M2.2 I can see them) functions you can create a new interface and your own AddressRepository model which extends original Interface/Model and then use this functions for update and search.

0
2

I just created a new module for that. But without own api interfaces and models. Instead I use the default ones of magnento-customer.

MyPackage/MyModule/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">
    <route url="/V1/addresses" method="POST">
        <service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/>
        <resources>
            <resource ref="Magento_Customer::manage"/>
        </resources>
    </route>
</routes>

Path is then: /rest/V1/addresses Method: POST To update an address add "id" (address id) and "customer_id" To create an address just add "customer_id"

Sample address update JSON request body

{
    address: {
        id: '248913',
        customer_id: '13736',
        firstname: 'Otto',
        lastname: 'Fox',
        street: [
            "Street 1"
        ],
        telephone: "01234 56789",
        postcode: "12345",
        city: "Leipzig",
        defaultBilling: true,
        country_id: "DE",
}
0

Thanks to @ahe-borriglione. Let me make a small addition. If you want to use this endpoint on storefront securely then insert into your 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/mine/addresses" method="POST">
        <service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="address.customer_id" force="true">%customer_id%</parameter>
        </data>
    </route>
</routes>

so address.customer_id will be replaced by id of currently logged customer so you do not have to fill it to body. And each customer will work only with his account.

Your Answer

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

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