Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hello,
We are trying to create public app and created the module configuration page. We need to know how can we store the module configuration values? Also how we store these values for each customer/user separately?
Thanks!
Hope you're having a great day!
You may want to add a database to your app or depending on what data you are storing you could use metafields. Metafields are useful for attaching specialized information to Shopify resources, such as part numbers or additional variant options.
Useful resources:
If you have any further questions, please do reach out either here on the forum, or via a private message/email. If you found this comment useful then please hit Like and Accept the Solution!
Hello,
Thank you for your response but we need further clarification because we have just started building with shopify and we don't have much knowledge about this. So as per your reply we have below questions:
1. If we go with database option then this database will be on the shopify side or we need to create it in our live site or in our localhost? Means where this database will be present?
2. If we go with metafields then how we will store it for different customers?
3. If two or more customers purchases our app then how we will insure that they can only see their configuration? For example if we have two fields name and password in our app configuration then how we will manage it for different customers?
Thanks!
Hope you're having a great day!
In fact, it completely depends on what kind of information you want to store. The decision of where to store a Shopify app’s data will influence the overall data management strategy for an app.
1. If we go with database option then this database will be on the shopify side or we need to create it in our live site or in our localhost? Means where this database will be present?
With the database option, data will be stored externally. Storing Shopify app data externally means the data would live outside of the Shopify ecosystem. For example, the data may live in another database on your side or another storage mechanism that is hosted outside of Shopify. Each Shop that will need access to this external data would need to fetch the data from the external source using Javascript. The data would then need to be injected into this store's theme page using JavaScript.
Pros:
Cons:
2. If we go with metafields then how we will store it for different customers?
They are useful for storing information that doesn’t otherwise have a home in the Shopify admin. Shopify provides direct support for accessing Metafield within Liquid, or Javascript may also be used.
To create, update, or delete metafields, you need to use the GraphQL Admin API or the REST Admin API. With the Storefront API, you can retrieve metafields but not create or update them.
You can add metafields to many Shopify resources:
Pros:
Cons:
Some use cases:
"namespace" : "seo"
"key" : "hidden"
"value" : 1
"value_type" : "integer"
To store data for different customers, you could use Shop Shopify resources (example with REST API endpoint):
Request
Response
For example, a clothing shop might use metafields to store the care instructions for its clothing products. The shop could create separate metafields for washing and drying instructions and group them by using the namespace instructions.
Useful resources:
3. If two or more customers purchase our app then how we will insure that they can only see their configuration?
Case: Metafields
Metafields are visible to all apps. If you want to restrict the visibility of the data that you're adding to a resource, then you can use private metafields.
Important: Only the GraphQL Admin API supports private metafields.
For example, suppose that App A creates 2 metafields and 10 private metafields for a product, and App B creates 10 private metafields for the same product. Both apps can access the two standard metafields created by App A, but each app can access only the 10 private metafields that it created.
Case: database option
In this case, you will have full control over the data, so you could manage it exactly as you want.
For example, if we have two fields name and password in our app configuration then how we will manage it for different customers?
What would password be used for? For access to the various APIs of Shopify, you do not need to manage passwords. You need to use the OAuth protocol.
What does the name represent here? If this is the name of your customer's store, you don't need to register it, you could access it at any time through the Shopify API.
To get shop name in Liquid code:
{{ shop.name }}
Shop object can be used and accessed from any file in a Liquid theme and is defined as a global object.
To get shop name with REST API:
Response:
To get shop name with GRAPHQL API:
Each of these choices has its own advantages and disadvantages. You should choose the approach with which the advantages will outweigh the disadvantages in your case.
If you have any further questions, please do reach out either here on the forum, or via a private message/email. If you found this comment useful then please hit Like and Accept the Solution!
Hello,
Thank you for your quick and detailed reply. It helps us a lot to move further. As per your details, we are going to use the metafields first. We have one last doubt. As you mentioned in your example also, let us say our public app configuration has a field colour. So how different customers(who purchased our app or using in trial mode) will see the value they stored? For example, 10 different customers stored 10 different values. So they should not mesh with each other. Please explain in the metafield context.
We are clear about this point. So please explain in detail as you explained in your previous reply.
Thanks!
Hope you're having a great day!
Good question.
So how different customers(who purchased our app or using in trial mode) will see the value they stored? For example, 10 different customers stored 10 different values. So they should not mesh with each other. Please explain in the metafield context.
You should create a private metafield for value (color) to be saved and attach it to the Shop resource. The Shop resource represents a store and therefore a merchant (your customer). Each Shop resource has a 64-bit unsigned integer unique ID.
Metafields are visible to all apps. If you want to restrict the visibility of the data you're adding to a resource, you can use private metafields. Private metafields are a distinct type of metafield that function almost entirely like standard metafields, but with a few key differences:
See this example where I create a metafield attached to the current shop with "blue" color as value:
mutation privateMetafieldUpsert($input: PrivateMetafieldInput!) {
privateMetafieldUpsert(input: $input) {
privateMetafield {
id
}
userErrors {
field
message
}
}
}
Query variables:
{
"input": {
"namespace": "colors",
"key": "shop_colors",
"valueInput": {
"value": "blue",
"valueType": "STRING"
}
}
}
JSON Response:
{
"data": {
"privateMetafieldUpsert": {
"privateMetafield": {
"id": "gid://shopify/PrivateMetafield/44316033069"
},
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 990,
"restoreRate": 50
}
}
}
}
If you have any further questions, please do reach out either here on the forum, or via a private message/email. If you found this comment useful then please hit Like (and Accept the Solution)!
Hello,
Thanks for the response. Just last question, we are trying to execute this code via our app. We have added the code in our pages/index.js file but it is not working. So can you please let us know if we are adding code in correct page and if you may share one complete example of this page then it will be very helpfull.
Thanks!