Anchor to section titled 'undefined'

privateMetafieldUpsert
mutation
deprecated

Creates or updates a private metafield. Use private metafields when you don't want the metafield data to be accessible by merchants or other apps. Private metafields are accessible only by the application that created them and only from the GraphQL Admin API.

An application can create a maximum of 10 private metafields per shop resource. Metafields created using a reserved namespace are private by default. See our guide for migrating private metafields.


Specifies the input fields for the private metafield.


Was this section helpful?

The private metafield that was created or updated.

The list of errors that occurred from executing the mutation.


Was this section helpful?

Examples

Hide code
DescriptionCopy
mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
  privateMetafieldUpsert(input: $input) {
    privateMetafield {
      id
      namespace
      key
      value
      valueType
    }
    userErrors {
      field
      message
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2025-01/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) { privateMetafieldUpsert(input: $input) { privateMetafield { id namespace key value valueType } userErrors { field message } } }",
 "variables": {
    "input": {
      "namespace": "private_keys",
      "key": "fantastic_app",
      "valueInput": {
        "value": "TA710SP",
        "valueType": "STRING"
      }
    }
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
    privateMetafieldUpsert(input: $input) {
      privateMetafield {
        id
        namespace
        key
        value
        valueType
      }
      userErrors {
        field
        message
      }
    }
  }`,
  {
    variables: {
      "input": {
        "namespace": "private_keys",
        "key": "fantastic_app",
        "valueInput": {
          "value": "TA710SP",
          "valueType": "STRING"
        }
      }
    },
  },
);

const data = await response.json();
session = ShopifyAPI::Auth::Session.new(
  shop: "your-development-store.myshopify.com",
  access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
  session: session
)

query = <<~QUERY
  mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
    privateMetafieldUpsert(input: $input) {
      privateMetafield {
        id
        namespace
        key
        value
        valueType
      }
      userErrors {
        field
        message
      }
    }
  }
QUERY

variables = {
  "input": {
    "namespace": "private_keys",
    "key": "fantastic_app",
    "valueInput": {
      "value": "TA710SP",
      "valueType": "STRING"
    }
  }
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
      privateMetafieldUpsert(input: $input) {
        privateMetafield {
          id
          namespace
          key
          value
          valueType
        }
        userErrors {
          field
          message
        }
      }
    }`,
    "variables": {
      "input": {
        "namespace": "private_keys",
        "key": "fantastic_app",
        "valueInput": {
          "value": "TA710SP",
          "valueType": "STRING"
        }
      }
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
    privateMetafieldUpsert(input: $input) {
      privateMetafield {
        id
        namespace
        key
        value
        valueType
      }
      userErrors {
        field
        message
      }
    }
  }
QUERY;

$variables = [
  "input" => [
    "namespace" => "private_keys",
    "key" => "fantastic_app",
    "valueInput" => [
      "value" => "TA710SP",
      "valueType" => "STRING",
    ],
  ],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "input": {
    "namespace": "private_keys",
    "key": "fantastic_app",
    "valueInput": {
      "value": "TA710SP",
      "valueType": "STRING"
    }
  }
}
Hide code
Response
JSON
{
  "privateMetafieldUpsert": {
    "privateMetafield": {
      "id": "gid://shopify/PrivateMetafield/1060470837",
      "namespace": "private_keys",
      "key": "fantastic_app",
      "value": "TA710SP",
      "valueType": "STRING"
    },
    "userErrors": []
  }
}