A space to discuss online store customization, theme development, and Liquid templating.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
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.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
Solved! Go to the solution
This is an accepted solution.
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.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
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.
This is an accepted solution.
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.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
@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.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
@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 }}
@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.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
@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 .
<h1> {{app.metafields.xxxx_app_keys.xxx_api_key }} </h1>
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:
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!
@BullConvert Thanks lot for depth debug
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.
2. Added {{ app.metafields.axtrics.material }} in main-product.liquid within Dawn theme.
After checking in the respective page on frontend, It is coming blank.
this is the correct code.
<h3> {{ app.metafields.axtrics.material.value| strip_html }} }} </h3>
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.
2. Created the AppOwned Metafield using the above AppInstallationID as Owner ID.
3. Confirmed if Metafields are created by calling the relevant GraphQL.
4. Added the Code Snippet in theme.liquid just under Body tag
<h2>App =====> {{ app.metafields.axtrics.recipe.value }} </h2>
5. 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.
Hi @Anonymous
I have not written any code but used the GraphQL queries within Insomnia API Tool.
I am still sharing the GQL queries for your to check
1. Get App Installation ID
POST Request to https://yourshopname.myshopify.com/admin/api/2022-10/graphql.json
{
app {
id,
title
developerName
installation{
id
activeSubscriptions{name}
launchUrl
}
}
}
2. Create Metafields using InstallationID
POST Request to https://yourshopname.myshopify.com/admin/api/2022-10/graphql.json
mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
Query Variables
{
"metafields": [
{
"key": "recipe",
"namespace": "axtrics",
"ownerId": "gid://shopify/AppInstallation/YourID",
"type": "single_line_text_field",
"value": "This is the recipe"
}
]
}
3. Add below code to theme.liquid
{{ app.metafields.axtrics.recipe.value }}
Thanks Again
@Anonymous
I even installed the Shopify GraphiQL App on that same store and create the Metafields using the AppInstallationId for GraphQL App. The metafields again got created but not showing on frontend.
Below are the screenshots:
Very strange.
Hi @rohan_5894
I am was able to create and get app-owned metafield with code i got the app-owned metafield value into theme liquid file also.
when I try your code with Shopify graphql explore I was successfully able to read the field value but it was not shown on the storefront. so I think it was app access scopes not properly defiled on your app.
i will suggest use any client library to perform this operation.
# read the metafiled
query AppInstallationMetafield($namespace: String!, $key: String!, $ownerId: ID!) {
appInstallation(id: $ownerId) {
apiKey: metafield(namespace: $namespace, key: $key) {
value
}
}
}
Thank You @Anonymous for your support.
I don't think it is an issue with App Scope because we were able to create Metafields using the same App Token. Anyways we have created tickets with Shopify to see if they can share some insights.
Thanks Again.
Okay, I have been finally been able to get the App Owned Metafields value at Storefront and I thought i should share the information with others as well.
Kindly note that you cannot access App Owned Metafield values directly at the Theme Liquid files.
You need to create App Block Extensions for your App and within thse extensions you can access the App Owned Metafield values. This makes sense as well because if these Metafields can be access at Liquid level then it violates the purpose of keeping App Owned data only to App Owners.
I hope this will be helpful for others.
@rohan_5894 Thanks for the update I was access the app meta direct into the theme liquid. still Tuesday 04 October 2022 12∶21∶01 PM IST.
After that we are not able to access into the direct theme files. but yes using theme app extension work well.
@Anonymous wrote:
4 fixed the issue with publish extension.
Can you provide more info about this comment?
Thanks!
@edo888 what Error you getting https://shopify.dev/apps/online-store/theme-app-extensions
follow this guide if it is still an Error gives me the complete information on what steps you doing. i will help you
I have described it here: https://community.shopify.com/c/online-store-2-0/accessing-app-owned-metafields-from-theme-extension...
Thanks!
Same blocker, I also mentioned in https://community.shopify.com/c/shopify-apis-and-sdks/how-to-store-external-api-key-with-theme-exten...
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.
There could be one good solution to this issue: Use App owned Metafields, Wrote in detail in the thread.
Sagar @ BullConvert - Modern Sales Booster | Find us on Shopify App Store
✓ Helping merchants increase sales with smarter conversion tools.
✓ Was this helpful? Hit Like or mark as Accepted Solution!