Want to store privateMetafields with Shopify resources and access them in liquid files

I want to store privateMetafields with Shopify resources and access them in liquid files!

I don’t want to expose data to other apps or merchants so storing in metafields is not an option. But I can’t seem to access privateMetafields from liquid templates,I read in the docs that

  1. privateMetafields can’t be accessed from liquid files

  2. meta fields can be read/edited by accessed by merchants or other apps

Now, using meta fields to store dynamic data for Shopify resources is a security risk and privateMetafields can’t be accessed from liquid so it kinda defeats the purpose! I would need to have an API call or something to get data into templates.

Any help would highly be appreciated.

Thank you very much.

2 Likes

I am running into this exact same issue. I want to use the new Theme App Extensions so that customers can add my app blocks within the theme customizer. I can pass the data to the theme extension’s Liquid templates through public JSON metafields, however that is going to confuse storeowners when they see it. I would much rather store it in private metafields so they don’t have to see it, and so they can’t mess things up by trying to edit it.

1 Like

Same blocker, I also mentioned in https://community.shopify.com/c/shopify-apis-and-sdks/how-to-store-external-api-key-with-theme-extensions/m-p/1335916

I was happy with ScriptTag implementation. Online Store 2.0 and Theme Extensions isn’t featured enough for my business logic, or I’m missing something for weeks.

1 Like

There could be one good solution to this issue: Use App owned Metafields, Wrote in detail in the thread.

There could be one good solution to this issue: Use App owned Metafields,

EFFECTIVE APRIL 01, 2022

As of GraphQL Admin API version 2022-04, a new owner type ApiPermission is now available for metafields. A metafield with this permission type will only be readable and writable by the app that owns the metafield.

The app metafields are actually on app installations (as one app can have many installations based on merchants). Now these are somewhat safer than putting data on shop owned public metafields.

These are accessilbe in the liquid file with the app object. {{ app.metafelds.namespace.key}}

Step1: Get App installation id

query {
  currentAppInstallation {
    id
  }
}

Step 2: Use that id as owner id to save metafield(s)

mutation ($metafields: [MetafieldsSetInput!]!) {
  metafieldsSet(metafields: $metafields) {
    metafields {
      id
    }
    userErrors {
      code
      field
      message
    }
  }
}

with metafilds variable:

metafields = [
  {
    ownerId: appInstallationId,
    namespace: 'namespace',
    key: 'key',
    value: 'value',
    type: "single_line_text_field"
  }
]

Step 3: Use those metafields in liquid files

{{ app.metafieds.namespace.key }}

That’s it. Try it out let us know how it went.

2 Likes

@BullConvert I am not able to get the app meta value {{ app.metafieds.namespace.key }} I used same reference your code. not Error on theme code.

also i am not able to access those data into the theme app extension.

Hello @Anonymous !

Make sure you set the orwnerId to the **appInstallationId** (which is unique for every installation of the app). When you use the appInstallationId for the ownerId field of the metafield, the meta field is created for that specific installation and will be removed automatically when the app is uninstalled.

You can make sure if the meta field is created or not using the following query:

query($namespace: String!) {
  currentAppInstallation {
    metafields(first: 50, namespace: $namespace) {
      edges {
        node {
          id
          namespace
          key
          value
        }
      }
    }
  }
}

It works on my end seamlessly.

1 Like

@BullConvert yes i can see the app meta was gql i will get meta value but i want access into the those data into the theme app extension.

i have query on

Step 3: Use those metafields in liquid files

{{ app.metafieds.namespace.key }}

https://community.shopify.com/c/shopify-apis-and-sdks/want-to-access-the-app-meta-field-to-the-theme-app-extension/m-p/1723040

@Anonymous Great if you could get the meta field using the query above, that makes sure that we indeed created the meta field with this specific app installation. Now just access those metafields in the theme app extension with {{ app.metafields.namespace.key }} in the liquid file (You’ll need to replace your namespace and key here).

If you still don’t see that meta field in the theme app extension, one caveat I can think of is you are trying to access a JSON field, without the “.value” method. If this is the case, the best would be just set a metafeild to a string value and make sure you can access that in the theme app extension using the {{app.metafields.namespace.key}}

When you are sure you can access the string metafield(s) then you can experiment with JSON fields.

1 Like

@BullConvert my meta filed type is

{
              "namespace": "xxxx_app_keys",
              "key": "xxx_api_key",
              "type": "single_line_text_field",
              "value": "aSxxxxxxxxxxxxxxxxxxxx==",
              "ownerId": "gid://shopify/AppInstallation/xxxxxxxxxxxx"
  }

theme app extension code to access that meta filed .

#   {{app.metafields.xxxx_app_keys.xxx_api_key }} 

i am not getting any error on theme app extension.

@Anonymous You are on the very right track. The above code you have mentioned works. Try debugging where else it might have been causing the issue.

Possible checks:

  1. Check API version, it has to be on or above 2022-04
  2. Make sure this is the same app installation
  3. Make sure you get that meta field using currentAppInstallation GraphQL query
  4. … etc.
1 Like

@BullConvert Thanks lot for depth debug

  1. Check API version, it has to be on or above 2022-04 - > gql “2022-07”

  2. Make sure this is the same app installation → yes it was the same app in my store.

  3. Make sure you get that meta field using currentAppInstallation GraphQL query → i will get that data using the gql query.

    4 fixed the issue with publish extension.
    @BullConvert Thanks lots your answer really help.

Hi Sanjay,

Could you please confirm if you were able to access the App Owned Metafields within the Theme Liquid Files as well?, They are coming blank for me. It looks like they can be accessed via App Theme Extensions only?

I would highly appreciate a help here.

@rohan_5894 yes you can access that meta filed into the theme liquid file. if you facing any Errors please share what you do still now write all the steps by step so I can guide you the right way.

my app meta filed value access on theme liquid file and also app extension code too.

Hi @Anonymous

Thank you for the prompt response. Below are the steps i followed.

  1. Created App Owned Meta Fields with GraphQL using Insomnia Client with OwnerID as AppInstallation ID. Attached is the screenshot for the same.

  1. Added {{ app.metafields.axtrics.material }} in main-product.liquid within Dawn theme.

After checking in the respective page on frontend, It is coming blank.

1 Like

@rohan_5894

this is the correct code.

###  {{ app.metafields.axtrics.material.value| strip_html }} }}  

add into your theme file it was work.

Thanks @Anonymous

Unfortunately this also didn’t worked. Kindly check the screenshots for the same

Frontend:

I am not sure what could be the reason. I can confirm that the App is installed and i am testing on correct store.

@rohan_5894 I think you putting the wrong App “Ownerid” please double check that.

Share the code snippets of All steps you perform. I will check my end.

Hi @Anonymous

I used the Staging App now on different store and still the same results, Below are the detailed steps I followed:

  1. Get the AppInstallationID along with other info to confirm the correct App name.

  1. Created the AppOwned Metafield using the above AppInstallationID as Owner ID.

  1. Confirmed if Metafields are created by calling the relevant GraphQL.

  1. Added the Code Snippet in theme.liquid just under Body tag
## App =====> {{ app.metafields.axtrics.recipe.value }} 
  1. Visited frontend to confirm but it was blank.

Thanks Again

@rohan_5894 share the code i can copy past else you can try into the https://shopify.dev/apps/tools/graphiql-admin-api try this one run your code you will get the meta value.