# metafieldsSet - customer - MUTATION
Version: 2024-07

## 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.

### Access Scopes



## Arguments
* [metafields](/docs/api/customer/2024-07/input-objects/MetafieldsSetInput): MetafieldsSetInput! - The list of metafield values to set. Maximum of 25.


## Returns
* [metafields](/docs/api/customer/2024-07/objects/Metafield): Metafield The list of metafields that were set.
* [userErrors](/docs/api/customer/2024-07/objects/MetafieldsSetUserError): MetafieldsSetUserError! The list of errors that occurred from executing the mutation.


## Examples
### Create and update metafields
Curl example: "curl -X POST \\\nhttps://shopify.com/<shop-id>/account/customer/api/2024-07/graphql/api/2024-07/graphql.json \\\n-H 'Content-Type: application/json' \\\n-H 'Authorization: {access_token}' \\\n-d '{\n\"query\": \"mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { key namespace value createdAt updatedAt } userErrors { field message code } } }\",\n \"variables\": {\n    \"metafields\": [\n      {\n        \"key\": \"nickname\",\n        \"namespace\": \"my_fields\",\n        \"ownerId\": \"gid://shopify/Customer/624407574\",\n        \"type\": \"single_line_text_field\",\n        \"value\": \"Big Tuna\"\n      },\n      {\n        \"key\": \"title\",\n        \"namespace\": \"my_fields\",\n        \"ownerId\": \"gid://shopify/Customer/624407574\",\n        \"type\": \"single_line_text_field\",\n        \"value\": \"Dr.\"\n      }\n    ]\n  }\n}'\n"
Node example: "const client = new shopify.clients.Graphql({session});\nconst data = await client.query({\n  data: {\n    \"query\": `mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n      metafieldsSet(metafields: $metafields) {\n        metafields {\n          key\n          namespace\n          value\n          createdAt\n          updatedAt\n        }\n        userErrors {\n          field\n          message\n          code\n        }\n      }\n    }`,\n    \"variables\": {\n      \"metafields\": [\n        {\n          \"key\": \"nickname\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Big Tuna\"\n        },\n        {\n          \"key\": \"title\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Dr.\"\n        }\n      ]\n    },\n  },\n});\n"
Ruby example: null
Remix example: "const { admin } = await authenticate.admin(request);\n\nconst response = await customer.graphql(\n  `#graphql\n  mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n    metafieldsSet(metafields: $metafields) {\n      metafields {\n        key\n        namespace\n        value\n        createdAt\n        updatedAt\n      }\n      userErrors {\n        field\n        message\n        code\n      }\n    }\n  }`,\n  {\n    variables: {\n      \"metafields\": [\n        {\n          \"key\": \"nickname\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Big Tuna\"\n        },\n        {\n          \"key\": \"title\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Dr.\"\n        }\n      ]\n    },\n  },\n);\n\nconst data = await response.json();\n"
Graphql query: "mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n  metafieldsSet(metafields: $metafields) {\n    metafields {\n      key\n      namespace\n      value\n      createdAt\n      updatedAt\n    }\n    userErrors {\n      field\n      message\n      code\n    }\n  }\n}"
#### Graphql Input
{
  "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."
    }
  ]
}
#### Graphql Response
{
  "data": {
    "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)
Curl example: "curl -X POST \\\nhttps://shopify.com/<shop-id>/account/customer/api/2024-07/graphql/api/2024-07/graphql.json \\\n-H 'Content-Type: application/json' \\\n-H 'Authorization: {access_token}' \\\n-d '{\n\"query\": \"mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { key namespace value createdAt updatedAt } userErrors { field message code } } }\",\n \"variables\": {\n    \"metafields\": [\n      {\n        \"key\": \"nickname\",\n        \"namespace\": \"my_fields\",\n        \"ownerId\": \"gid://shopify/Customer/624407574\",\n        \"type\": \"single_line_text_field\",\n        \"value\": \"Big Tuna\",\n        \"compareDigest\": \"8ac49f51d020905227c0bc6d040a035396ec518dbda59d6bb53df3f718fb9f35\"\n      },\n      {\n        \"key\": \"title\",\n        \"namespace\": \"my_fields\",\n        \"ownerId\": \"gid://shopify/Customer/624407574\",\n        \"type\": \"single_line_text_field\",\n        \"value\": \"Dr.\",\n        \"compareDigest\": null\n      }\n    ]\n  }\n}'\n"
Node example: "const client = new shopify.clients.Graphql({session});\nconst data = await client.query({\n  data: {\n    \"query\": `mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n      metafieldsSet(metafields: $metafields) {\n        metafields {\n          key\n          namespace\n          value\n          createdAt\n          updatedAt\n        }\n        userErrors {\n          field\n          message\n          code\n        }\n      }\n    }`,\n    \"variables\": {\n      \"metafields\": [\n        {\n          \"key\": \"nickname\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Big Tuna\",\n          \"compareDigest\": \"8ac49f51d020905227c0bc6d040a035396ec518dbda59d6bb53df3f718fb9f35\"\n        },\n        {\n          \"key\": \"title\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Dr.\",\n          \"compareDigest\": null\n        }\n      ]\n    },\n  },\n});\n"
Ruby example: null
Remix example: "const { admin } = await authenticate.admin(request);\n\nconst response = await customer.graphql(\n  `#graphql\n  mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n    metafieldsSet(metafields: $metafields) {\n      metafields {\n        key\n        namespace\n        value\n        createdAt\n        updatedAt\n      }\n      userErrors {\n        field\n        message\n        code\n      }\n    }\n  }`,\n  {\n    variables: {\n      \"metafields\": [\n        {\n          \"key\": \"nickname\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Big Tuna\",\n          \"compareDigest\": \"8ac49f51d020905227c0bc6d040a035396ec518dbda59d6bb53df3f718fb9f35\"\n        },\n        {\n          \"key\": \"title\",\n          \"namespace\": \"my_fields\",\n          \"ownerId\": \"gid://shopify/Customer/624407574\",\n          \"type\": \"single_line_text_field\",\n          \"value\": \"Dr.\",\n          \"compareDigest\": null\n        }\n      ]\n    },\n  },\n);\n\nconst data = await response.json();\n"
Graphql query: "mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {\n  metafieldsSet(metafields: $metafields) {\n    metafields {\n      key\n      namespace\n      value\n      createdAt\n      updatedAt\n    }\n    userErrors {\n      field\n      message\n      code\n    }\n  }\n}"
#### Graphql Input
{
  "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
    }
  ]
}
#### Graphql Response
{
  "data": {
    "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": []
    }
  }
}