---
title: metafieldsSet - Customer API
description: >-
  Sets metafield values. Metafield values will be set regardless if they were
  previously created or not.


  Allows a maximum of 25 metafields to be set at a time.


  This operation is atomic, meaning no changes are persisted if an error is
  encountered.


  As of `2024-07`, this operation supports compare-and-set functionality to
  better handle concurrent requests.

  If `compareDigest` is set for any metafield, the mutation will only set that
  metafield if the persisted metafield value matches the digest used on
  `compareDigest`.

  If the metafield doesn't exist yet, but you want to guarantee that the
  operation will run in a safe manner, set `compareDigest` to `null`.

  The `compareDigest` value can be acquired by querying the metafield object and
  selecting `compareDigest` as a field.

  If the `compareDigest` value does not match the digest for the persisted
  value, the mutation will return an error.

  You can opt out of write guarantees by not sending `compareDigest` in the
  request.
api_version: unstable
api_name: customer
source_url:
  html: 'https://shopify.dev/docs/api/customer/unstable/mutations/metafieldsSet'
  md: 'https://shopify.dev/docs/api/customer/unstable/mutations/metafieldsSet.md'
---

# metafields​Set

mutation

Sets metafield values. Metafield values will be set regardless if they were previously created or not.

Allows a maximum of 25 metafields to be set at a time.

This operation is atomic, meaning no changes are persisted if an error is encountered.

As of `2024-07`, this operation supports compare-and-set functionality to better handle concurrent requests. If `compareDigest` is set for any metafield, the mutation will only set that metafield if the persisted metafield value matches the digest used on `compareDigest`. If the metafield doesn't exist yet, but you want to guarantee that the operation will run in a safe manner, set `compareDigest` to `null`. The `compareDigest` value can be acquired by querying the metafield object and selecting `compareDigest` as a field. If the `compareDigest` value does not match the digest for the persisted value, the mutation will return an error. You can opt out of write guarantees by not sending `compareDigest` in the request.

## Arguments

* metafields

  [\[Metafields​Set​Input!\]!](https://shopify.dev/docs/api/customer/unstable/input-objects/MetafieldsSetInput)

  required

  The list of metafield values to set. Maximum of 25.

***

## Metafields​Set​Payload returns

* metafields

  [\[Metafield!\]](https://shopify.dev/docs/api/customer/unstable/objects/Metafield)

  The list of metafields that were set.

* user​Errors

  [\[Metafields​Set​User​Error!\]!](https://shopify.dev/docs/api/customer/unstable/objects/MetafieldsSetUserError)

  non-null

  The list of errors that occurred from executing the mutation.

***

## Examples

* ### Create and update metafields

  #### Description

  Create and update operations are combined in the \`metafieldSet\` mutation. The following example shows you how to create one new metafield, \`key: "title"\`, and update an existing metafield, \`key: "nickname"\` in one mutation.

  #### Query

  ```graphql
  mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
    metafieldsSet(metafields: $metafields) {
      metafields {
        key
        namespace
        value
        createdAt
        updatedAt
      }
      userErrors {
        field
        message
        code
      }
    }
  }
  ```

  #### Variables

  ```json
  {
    "metafields": [
      {
        "key": "nickname",
        "namespace": "my_fields",
        "ownerId": "gid://shopify/Customer/624407574",
        "type": "single_line_text_field",
        "value": "Big Tuna"
      },
      {
        "key": "title",
        "namespace": "my_fields",
        "ownerId": "gid://shopify/Customer/624407574",
        "type": "single_line_text_field",
        "value": "Dr."
      }
    ]
  }
  ```

  #### cURL

  ```bash
  curl -X POST \
  https://your-development-store.myshopify.com/customer/api/unstable/graphql \
  -H 'Content-Type: application/json' \
  -H 'Authorization: {access_token}' \
  -d '{
  "query": "mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { key namespace value createdAt updatedAt } userErrors { field message code } } }",
   "variables": {
      "metafields": [
        {
          "key": "nickname",
          "namespace": "my_fields",
          "ownerId": "gid://shopify/Customer/624407574",
          "type": "single_line_text_field",
          "value": "Big Tuna"
        },
        {
          "key": "title",
          "namespace": "my_fields",
          "ownerId": "gid://shopify/Customer/624407574",
          "type": "single_line_text_field",
          "value": "Dr."
        }
      ]
    }
  }'
  ```

  #### Response

  ```json
  {
    "metafieldsSet": {
      "metafields": [
        {
          "key": "nickname",
          "namespace": "my_fields",
          "value": "Big Tuna",
          "createdAt": "2024-11-13T20:03:35Z",
          "updatedAt": "2024-11-13T20:03:38Z"
        },
        {
          "key": "title",
          "namespace": "my_fields",
          "value": "Dr.",
          "createdAt": "2024-11-13T20:03:38Z",
          "updatedAt": "2024-11-13T20:03:38Z"
        }
      ],
      "userErrors": []
    }
  }
  ```

* ### Creating and updating metafields using compare-and-swap (CAS)

  #### Description

  Create and update operations are combined in the \`metafieldSet\` mutation. The following example shows you how to create one new metafield, \`key: "title"\`, and update an existing metafield, \`key: "nickname"\` in one mutation in a safer way with compare-and-swap (CAS) through the \`compareDigest\` field.

  #### Query

  ```graphql
  mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
    metafieldsSet(metafields: $metafields) {
      metafields {
        key
        namespace
        value
        createdAt
        updatedAt
      }
      userErrors {
        field
        message
        code
      }
    }
  }
  ```

  #### Variables

  ```json
  {
    "metafields": [
      {
        "key": "nickname",
        "namespace": "my_fields",
        "ownerId": "gid://shopify/Customer/624407574",
        "type": "single_line_text_field",
        "value": "Big Tuna",
        "compareDigest": "8ac49f51d020905227c0bc6d040a035396ec518dbda59d6bb53df3f718fb9f35"
      },
      {
        "key": "title",
        "namespace": "my_fields",
        "ownerId": "gid://shopify/Customer/624407574",
        "type": "single_line_text_field",
        "value": "Dr.",
        "compareDigest": null
      }
    ]
  }
  ```

  #### cURL

  ```bash
  curl -X POST \
  https://your-development-store.myshopify.com/customer/api/unstable/graphql \
  -H 'Content-Type: application/json' \
  -H 'Authorization: {access_token}' \
  -d '{
  "query": "mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { key namespace value createdAt updatedAt } userErrors { field message code } } }",
   "variables": {
      "metafields": [
        {
          "key": "nickname",
          "namespace": "my_fields",
          "ownerId": "gid://shopify/Customer/624407574",
          "type": "single_line_text_field",
          "value": "Big Tuna",
          "compareDigest": "8ac49f51d020905227c0bc6d040a035396ec518dbda59d6bb53df3f718fb9f35"
        },
        {
          "key": "title",
          "namespace": "my_fields",
          "ownerId": "gid://shopify/Customer/624407574",
          "type": "single_line_text_field",
          "value": "Dr.",
          "compareDigest": null
        }
      ]
    }
  }'
  ```

  #### Response

  ```json
  {
    "metafieldsSet": {
      "metafields": [
        {
          "key": "nickname",
          "namespace": "my_fields",
          "value": "Big Tuna",
          "createdAt": "2024-11-13T20:03:38Z",
          "updatedAt": "2024-11-13T20:03:39Z"
        },
        {
          "key": "title",
          "namespace": "my_fields",
          "value": "Dr.",
          "createdAt": "2024-11-13T20:03:39Z",
          "updatedAt": "2024-11-13T20:03:39Z"
        }
      ],
      "userErrors": []
    }
  }
  ```

* ### metafieldsSet reference
