8

There are API's available to fetch and delete address of a customer but not create a new one.

GET    /V1/customers/addresses/:addressId
GET    /V1/customers/me/billingAddress
GET    /V1/customers/:customerId/billingAddress
GET    /V1/customers/me/shippingAddress
GET    /V1/customers/:customerId/shippingAddress
DELETE /V1/addresses/:addressId

POST Api's are not available publicly?

0

5 Answers 5

9

I was having a similar problem and was digging into the AccountManagement class to find the proper payload. I noticed that the createAccount function checks if a customer ID exists and loads the stored data.

By performing a POST /V1/customers/:id, I was able to add another address with the following payload:

{ "customer":
    {
       "email":"[email protected]",
       "firstname":"test",
       "lastname":"test",
       "websiteId": 0,
       "addresses": [
            {
                "id": 9,
                "firstname": "test",
                "lastname": "test",
                "company": "test technology",
                "street": [
                "Test Street 9",
                "Tiny House 9"
                ],
                "city": "City Nine",
                "region_id": 12,
                "region": "California",
                "postcode": "91790",
                "country_id": "US",
                "telephone": "1234567890"
            },
            {
                "firstname": "test",
                "lastname": "test",
                "company": "test technology",
                "street": [
                "Test Street 10",
                "Tiny House 10"
                ],
                "city": "City Ten",
                "region_id": 12,
                "region": "California",
                "postcode": "91790",
                "country_id": "US",
                "telephone": "1234567890"
            }
        ]
    }

By post method how did you do this actually when i am trying it is showing me error of missing route did you write webapi.xml code for this route.

Another Way:

add endp point app/code/YourCompany/Customer/etc/webapi.xml

<route url="/V1/addresses" method="POST">
        <service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/>
        <resources>
            <resource ref="Magento_Customer::manage"/>
        </resources>
    </route>

Body:

{
  "address": { 
    "customer_id": "363",
    "defaultShipping": true,
    "defaultBilling": true,
     "region": "Adabor - Dhaka",
    "country_id": "BD",
    "street": [  "304, Tejgaon IA" ],
    "postcode": "1208",
    "city": "Dhaka",
    "firstname": "Matin",
    "lastname": "Api Adrss",   
    "telephone": "01717676441",
    "countryId": "BD"
  }
}

If Update Existing address add addressID on body preload:

"id": 323,
"customer_id": "363",

Post: http://yourstore.com/rest/V1/addresses [admin integration token]

[get CustomerID http://yourstore.com/rest/V1/customers/me getMethod customer token]

2
  • In Magento 2.3, I tried PUT and it works like a charm! Mar 7, 2019 at 14:13
  • How do we know the address id while updating customer.
    – Anees
    Dec 3, 2020 at 9:15
6

It should be possible to add/update customer addresses using customer repository API:

PUT /V1/customers/:id (for admin)
PUT /V1/customers/me (for customer)
0

I'm using Magento 2.2, PUT and POST both do not work for adding a new address to an existing account. I get the following error:

"A customer with the same email already exists in an associated website"

Is adding an address to an existing Magento customer only supported in version 2.3 and later?

1
  • UPDATE: I was able to get the endpoint "PUT /V1/customers/:id" to work but an additional field was needed in the "customer" object. After adding an "id" field (Magento customer ID), I am able to add addresses to an already existing customer account.
    – y2alex007
    May 2, 2019 at 22:25
0

/V1/customers/me

This endpoint is available only for customer and with ajax.

1
  • When I said customer, I mean with customer bearer token (/integration/customer/token)
    – Plim
    Jul 6, 2020 at 3:59
0

There is an AddressRespository API available in the core. We can use this API to save the address without using the Customer Entity.

File: vendor/magento/module-customer/Api/AddressRepositoryInterface.php Method: save()

As this is not linked with any endpoint, we may have to create a custom REST endpoint and pass the Address object as POST request.

POST Body would look like this:

{
    "address": {
        "customer_id": 24,
        "firstname": "Harish",
        "lastname": "ST",
        "telephone": "",
        "region_id": 550,
        "country_id": "IN",
        "street": [
            "My Street Address"
        ],
        "postcode": "000000",
        "city": "City",
        "default_shipping": true,
        "default_billing": true
    }
}

I used this to create new addressed for my Mobile application.

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.