Detecting app embed blocks

Hello. Can someone explain in more detail about the Detecting app embed blocks?

https://shopify.dev/apps/online-store/theme-app-extensions/extensions-framework#detecting-app-embed-blocks

An app block is given a unique ID - Is this ID unique for every store and given to block once for a lifetime or is this ID changed in every installation?

Hi,

You can make a request to API as follow:

https://{{shop}}/admin/themes/{{active-theme-id}}/assets.json?asset[key]=config/settings_data.json

https://shopify.dev/api/admin-rest/2021-10/resources/asset#get-themes-theme-id-assets?asset[key]=templates-index.liquid

and parse content. You should find it in “blocks” section

export async function checkAppActivated (data) {
console.log(“check app Activated”)
var session = data.res.locals.shopify.session;
const clientRes = new shopify.api.clients.Rest({ session });
var response
try {
response = await clientRes.get({
path: ‘themes’,
query: { role: ‘main’ },
});
} catch (e) {
console.log(${e} session-info ${session});
}

var themeID = response.body.themes[0].id;

// get existing theme.liquid code
var response2
try {
response2 = await clientRes.get({
path: themes/${response.body.themes[0].id}/assets,
query: {
asset: { key: ‘config/settings_data.json’ },
},
});
} catch (e) {
console.log(${e} session-info ${session});
}

// console.log(response2.body.asset.value);

var themeCode = response2.body.asset.value;

if (!themeCode) return false

var json
try {
json = JSON.parse(themeCode)
} catch (error) {
console.log(“error”, error)
}

if (!json.current.blocks) return false

const typeSuffix = blocks/app-embed/6b835701-24aa-464a-b23a-f9592b0522cc

const embedBlock = Object.entries(json.current.blocks).find(([id, info]) => {
return info.type.endsWith(typeSuffix) && !info.disabled
})

// if app is not activated then send activation link
var Actiavted = !!embedBlock
if (Actiavted) {
return ${Actiavted};
}
else {
var shop = session.shop;
return https://${shop}/admin/themes/${themeID}/editor?context=apps&appEmbed=6b835701-24aa-464a-b23a-f9592b0522cc%2Fapp-embed
}
}

I have the app enabled in the theme customization. but my json[‘current’][‘blocks’] nil , you can help me

Can you explain more about input data
checkAppActivated (data)

Thanks

I think It’s ID. ( Red Box )