GrapgQL api

GrapgQL api

muhammad_haroon
Shopify Partner
32 0 2

I'm currently working on the development of a backup app tailored for Shopify stores. The primary objective of this app is to create a backup of all products within a store and capture all associated product information, including custom metafields, in a CSV file. In my initial attempts, I utilized the Shopify REST API for exporting products. However, I encountered an issue where custom metafields, both with and without definitions, were not being included in the export. Subsequently, I switched to using the Shopify GraphQL API to retrieve all the metafields.

Now, I have a couple of questions. First, when I manually export products from my Shopify store to a CSV file, I can observe the presence of metafields without definitions, but not those with definitions. Second, once I've successfully retrieved all the metafields using the GraphQL API and stored the data in a CSV file, I'm interested in knowing whether it's possible to import these custom metafields (both with and without definitions) back into the same Shopify store or perhaps a different one.

Replies 4 (4)

Liam
Community Manager
3108 341 880

Hi Muhammad,

 

When you manually export products to a CSV file from the Shopify store, the fields that are exported are pre-defined by Shopify. While it's true that custom metafields without definitions will be included, metafields with definitions will not be included in the manual export.

 

This is because defined metafields are expected to have a specific structure and validation rules, which might not be preserved in a CSV export. Exporting such data may lead to potential data corruption or loss when re-imported. The best way to handle this scenario is to use the Shopify API, as you have done, which allows for a more granular control over data exports and imports.

 

As for your second query, yes, you can import metafields back into Shopify. You can use either the REST or GraphQL API, depending on your preference. If you use the REST API, you can use the POST /admin/api/2023-10/products/{product_id}/metafields.json endpoint to create a new metafield for a product. If you prefer GraphQL API, use the productUpdate mutation to add metafields to a product.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

muhammad_haroon
Shopify Partner
32 0 2

Thank you, but I manually import the CSV file and only use the API for exporting products. I retrieve all the information related to the products and save it on my local server in a CSV file. Once the CSV file is created, I manually import it into my store or another store. Could you please clarify if I can export metafields, both with and without definitions, using the REST API? If it's possible, how can I achieve this? Because the REST API response does not seem to include custom created metafields, whether they have definitions or not.

muhammad_haroon
Shopify Partner
32 0 2

My main issue is that when I use the REST API to export products, it doesn't export the metafields, both with and without definitions. That's why I switched to using the GraphQL API, which provides me with both types of metafields, with and without definitions. I want to know if there's a way to get custom-created metafields with or without definitions using the REST API, so I don't have to use the GraphQL API. Both APIs have different structures, so I'm trying to determine which one is more suitable for my needs.

 

ShopifyDevSup
Shopify Staff
1453 238 512

Hi @muhammad_haroon,

 

The REST Admin API should be able to return a product's metafields, with or without definitions, but you will need to use the path from the resource to it's metafields endpoint in another request.

 

  • For a product that would be  admin/api/{{version}}/products/{{product_id}}/metafields.json

 

The GraphQL Admin API offers much more flexibility for retrieving the product/ variant and metafield information all in a single request so I would recommend sticking with that.

 

Hope that helps!

 

- James

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog