Manage values
Metafields are key-value pairs that let you store custom data on Shopify resources like products, customers, and orders. This guide shows you how to create, read, update, and delete metafield values using the GraphQL Admin API.
Anchor to RequirementsRequirements
- Your app can make authenticated requests to the GraphQL Admin API.
- You have the appropriate scopes, such as
write_productsandwrite_customers, based on owner type. - You've created a metafield definition for your metafield. The definition establishes the schemas (structure and rules) for metafields. If you're creating metafields for common use cases, you can skip this step.
Anchor to Creating metafieldsCreating metafields
Create metafields by including them when creating or updating resources (like productCreate or productUpdate). Alternatively, use metafieldsSet for standalone metafield operations.
The metafield's namespace, key, and type must match its corresponding metafield definition.
Anchor to App-owned metafield exampleApp-owned metafield example
Create app-owned metafields for your app-owned defitions. This gives your app exclusive control over both the definition (schema) and the metafield (value). App ownership is established using the namespace $app.
This example uses productCreate to create a product along with an app-owned metafield:
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonAnchor to Merchant-owned metafieldsMerchant-owned metafields
Create merchant-owned metafields for your merchant-owned definitions. Merchant-ownership ensures shared management access across apps and in the Shopify admin. Merchant ownhership is established using any namespace that isn't reserved for apps or Shopify (see Metafield ownership).
This example uses productCreate to create a product along with a merchant-owned metafield.
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonAnchor to Creating multiple metafieldsCreating multiple metafields
Set multiple metafields at once by passing an array. This is more efficient than making separate requests for each metafield. See metafield limits for maximum batch sizes.
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonAnchor to Using standard definitionsUsing standard definitions
Shopify provides pre-defined metafield schemas for common use cases like ISBN numbers, ingredients, and product specifications. Using these standard definitions saves you from creating custom definitions and ensures compatibility across themes and apps.
Standard definitions auto-enable when you create metafield values using their namespace and key (like facts and isbn):
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonOnce enabled, Shopify owns the schema, but the metafields (values) created are merchant-owned. See the standard definitions list for available standard namespaces and keys.
Anchor to Reading metafieldsReading metafields
Query metafields through their parent resource using GraphQL. This example retrieves a product along with its metafields, demonstrating three common patterns: fetching metafields (with pagination), getting a specific metafield by namespace/key, and filtering by namespace.
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonFor advanced querying techniques, see Query using metafields.
Anchor to Updating metafieldsUpdating metafields
Update metafields using the resource mutation approach. Metafields automatically update if the namespace/key combination already exists.
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonThe type must match the original definition. You can't change a metafield's type by updating its value.
Anchor to Deleting metafieldsDeleting metafields
Delete metafields using the metafieldsDelete mutation with their namespace, key, and owner ID.
GraphQL
POST https://{shop}.myshopify.com/api/{api_version}/graphql.jsonDeleting a resource (using productDelete or customerDelete for example) automatically deletes all associated metafields. Use metafieldsDelete when you want to remove specific metafields while keeping the resource.
Anchor to Error handlingError handling
Common errors and their solutions when working with metafields.
| Error | Cause | Solution |
|---|---|---|
| "Value is invalid for type" | Wrong format for type | Check type formats |
| "Validation failed" | Value doesn't meet validation rules | Check definition's validation constraints |
| "Type mismatch" | Type doesn't match definition | Use the exact type from definition |
| "JSON parse error" | Invalid JSON format | Validate JSON and escape properly |
Anchor to Best practicesBest practices
Following these practices helps ensure efficient, reliable metafield management and prevents common issues:
- Batch operations: Set multiple metafields in one request by passing an array (see limits).
- Validate before saving: Check value format matches type requirements
- Use consistent formatting: Maintain consistent JSON structures and date formats
- Handle references carefully: Verify referenced resources exist
- Escape JSON properly: Use JSON.stringify() for complex values
- Query efficiently: Use specific namespaces/keys instead of fetching all
- Cache metafield IDs: Store IDs for frequently accessed metafields
Anchor to Next stepsNext steps
- Learn how to work with metafield definitions.
- Learn how to use metafields to query resources.
- Learn how to enable filtering and other advanced features.