Anchor to section titled 'undefined'

productVariantCreate
mutation
deprecated

Requires write_products access scope. Also: The user must have a permission to create a product variant.

Creates a product variant. Use productVariantsBulkCreate instead.


The properties for the new product variant.


Was this section helpful?

The product associated with the variant.

The successfully created variant.

The list of errors that occurred from executing the mutation.


Was this section helpful?

Examples

Hide code
DescriptionCopy
mutation createProductVariantMetafields($input: ProductVariantInput!) {
  productVariantCreate(input: $input) {
    productVariant {
      id
      metafields(first: 3) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
    userErrors {
      message
      field
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-01/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation createProductVariantMetafields($input: ProductVariantInput!) { productVariantCreate(input: $input) { productVariant { id metafields(first: 3) { edges { node { id namespace key value } } } } userErrors { message field } } }",
 "variables": {
    "input": {
      "metafields": [
        {
          "namespace": "my_field",
          "key": "liner_material",
          "type": "single_line_text_field",
          "value": "Synthetic Leather"
        }
      ],
      "productId": "gid://shopify/Product/108828309"
    }
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation createProductVariantMetafields($input: ProductVariantInput!) {
    productVariantCreate(input: $input) {
      productVariant {
        id
        metafields(first: 3) {
          edges {
            node {
              id
              namespace
              key
              value
            }
          }
        }
      }
      userErrors {
        message
        field
      }
    }
  }`,
  {
    variables: {
      "input": {
        "metafields": [
          {
            "namespace": "my_field",
            "key": "liner_material",
            "type": "single_line_text_field",
            "value": "Synthetic Leather"
          }
        ],
        "productId": "gid://shopify/Product/108828309"
      }
    },
  },
);

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 createProductVariantMetafields($input: ProductVariantInput!) {
    productVariantCreate(input: $input) {
      productVariant {
        id
        metafields(first: 3) {
          edges {
            node {
              id
              namespace
              key
              value
            }
          }
        }
      }
      userErrors {
        message
        field
      }
    }
  }
QUERY

variables = {
  "input": {
    "metafields": [{"namespace"=>"my_field", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Synthetic Leather"}],
    "productId": "gid://shopify/Product/108828309"
  }
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation createProductVariantMetafields($input: ProductVariantInput!) {
      productVariantCreate(input: $input) {
        productVariant {
          id
          metafields(first: 3) {
            edges {
              node {
                id
                namespace
                key
                value
              }
            }
          }
        }
        userErrors {
          message
          field
        }
      }
    }`,
    "variables": {
      "input": {
        "metafields": [
          {
            "namespace": "my_field",
            "key": "liner_material",
            "type": "single_line_text_field",
            "value": "Synthetic Leather"
          }
        ],
        "productId": "gid://shopify/Product/108828309"
      }
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  mutation createProductVariantMetafields($input: ProductVariantInput!) {
    productVariantCreate(input: $input) {
      productVariant {
        id
        metafields(first: 3) {
          edges {
            node {
              id
              namespace
              key
              value
            }
          }
        }
      }
      userErrors {
        message
        field
      }
    }
  }
QUERY;

$variables = [
  "input" => [
    "metafields" => [{"namespace"=>"my_field", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Synthetic Leather"}],
    "productId" => "gid://shopify/Product/108828309",
  ],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "input": {
    "metafields": [
      {
        "namespace": "my_field",
        "key": "liner_material",
        "type": "single_line_text_field",
        "value": "Synthetic Leather"
      }
    ],
    "productId": "gid://shopify/Product/108828309"
  }
}
Hide code
Response
JSON
{
  "productVariantCreate": {
    "productVariant": {
      "id": "gid://shopify/ProductVariant/1070325086",
      "metafields": {
        "edges": [
          {
            "node": {
              "id": "gid://shopify/Metafield/1069229040",
              "namespace": "my_field",
              "key": "liner_material",
              "value": "Synthetic Leather"
            }
          }
        ]
      }
    },
    "userErrors": []
  }
}