I want to update Customer Metafield using Admin Rest Api.

SIN_88
Shopify Partner
3 0 0

As the title says, I want to update customer metafield using admin rest api.

The response is coming back with 200, but the metafields were not included in the response object either.
Also, I cannot get the metafiled coming to the following endpoint.
https://{{ shop name }}/admin/api/2023-01/metafields.json
I tried to update the following endpoint, but was unable to do so.
Is there some configuration that needs to be done?
Thank you in advance.

 

endpointhttps://{{ shop name }}/admin/api/2023-01/customers/{{ customer.id }}.json
Webhook version2023-01
Admin API access scopewrite_customers、 read_customers

 

const requestHeaders = {
  'X-Shopify-Access-Token': accessToken,
  'Content-Type': 'application/json',
};

const Payload = {
  customer: {
    first_name,
    last_name,
    metafields: [
        {
          key: metafieldKey_01,
          value: newValue,
          value_type: 'single_line_text_field',
          namespace: metafieldNamespace_01,
        },
        {
          key: metafieldKey_02,
          value: newValue,
          value_type: 'single_line_text_field',
          namespace: metafieldNamespace_02,
        },
    ],
  },
};

const updateCustomer = (payload) => {
  fetch(ApiUrl, {
    method: 'PUT',
    headers: requestHeaders,
    body: JSON.stringify(payload),
  })
    .then((response) => response.json())
    .then((data) => console.log(data))
    .catch((error) => console.error(error, payload));
};
Reply 1 (1)

garyrgilbert
Shopify Partner
391 40 162

If you are updating the metafields you need to include the metafield id in your metafields object.

 

 metafields: [
        {
id:[metafield id],
          key: metafieldKey_01,
          value: newValue,
          value_type: 'single_line_text_field',
          namespace: metafieldNamespace_01,
        },
        {
id:[metafieldid],
          key: metafieldKey_02,
          value: newValue,
          value_type: 'single_line_text_field',
          namespace: metafieldNamespace_02,
        },
    ]

 

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution