How to store app configuration values?

opencartcity
New Member
7 0 0

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!

0 Likes
Michal_Morek
Shopify Partner
820 73 140

Hi @opencartcity 

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 metafieldsMetafields 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!

Founder of Shopify agency - Accomplishify.com
If you would like to hire us as Shopify experts, please reach out via:
  • Email michal.morek@accomplishify.com
  • WhatsApp +48 507 613 425
0 Likes
opencartcity
New Member
7 0 0

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!

0 Likes
Michal_Morek
Shopify Partner
820 73 140

Hi @opencartcity 

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:

  • full control of the data

Cons:

  • data can’t be handled directly within the theme via Liquid
  • external calls for the data may be slower
  • an external dependency for the data means a higher risk of the data being unavailable due to downtime of the external server

 

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. 

scrnli_06_06_2021_12-49-31.png

You can add metafields to many Shopify resources:

  • Product,
  • Blog post (Article).
  • Customer,
  • Shop
  • etc

Pros:

  • data can be accessed directly from Liquid
  • everything stays within the Shopify ecosystem - no external server requests are needed

Cons:

  • data management is a bit harder; multiple additional Shopify API calls are required for updating metafields

Some use cases:

  • You can use metafields to add extra content to a product page, add tabs to a product page, add links to related products, group related products, nest products, or store attributes about a product. For example, you could create a metafield to store the color of a product and then set the background of the product page to the same color.
  • You can use metafields to keep track of your inventory. For example, you could use this information to show delivery timeframes, backorder dates, and units sold on a checkout page.
  • You can use metafields to track your inventory across multiple locations. For example, you could use this information to help track where your products are located.
  • You can use metafields to store SEO data, such as custom URLs and meta tags. For example, to add noindex and nofollow meta tags to a product page, create a metafield for the object with the following attributes:

 

 

 

"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

scrnli_06_06_2021_12-40-42.png

Response

scrnli_06_06_2021_12-42-44.png

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:

scrnli_06_06_2021_13-29-45.png

Response:

scrnli_06_06_2021_13-34-38.png

To get shop name with GRAPHQL API:

scrnli_06_06_2021_13-38-56.png

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!

Founder of Shopify agency - Accomplishify.com
If you would like to hire us as Shopify experts, please reach out via:
  • Email michal.morek@accomplishify.com
  • WhatsApp +48 507 613 425
0 Likes
opencartcity
New Member
7 0 0

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!

0 Likes
Michal_Morek
Shopify Partner
820 73 140

Hi @opencartcity 

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:

  • Private metafields are accessible only by the app that created them.
  • Private metafields are deleted when the app that created them is uninstalled.
  • Private metafields are not accessible through Liquid.
  • Private metafields are not accessible through the Storefront API.
  • An app can create a maximum of 10 private metafields per shop resource.

See this example where I create a metafield attached to the current shop with "blue" color as value:

4f0a33df-14b1-472a-8ffa-9fd2ee28667a.png

 

 

 

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)!

 

Founder of Shopify agency - Accomplishify.com
If you would like to hire us as Shopify experts, please reach out via:
  • Email michal.morek@accomplishify.com
  • WhatsApp +48 507 613 425
0 Likes
opencartcity
New Member
7 0 0

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!

0 Likes