API call

jplourenco
Shopify Partner
4 0 0

Hi, I'm quite new to the shopify dev side even thought I have been using this platform for nearly 4 years. I understand html, css and javascript but I learn it all myself so I noticed sometimes that are even basic things I don't know.

 

Right now, I have a client that want to have dynamic headers on his store. Most of it can be done with the metafields on the backend, but the customer/login, etc and the search pages cannot.

 

Hence I have been trying to figure out a way. I tried several things, including a localhost variable but then I couldn't make that into a liquid variable, even if I used a form. That wasn't very clean either.

 

So I have added a global variable in the settings.schema.json:

 

{
"name": "global-variable",
"settings": [
{
"type": "text",
"id": "global-variable-id",
"label": "Global Variable",
"default": "default",
"info": "This is a global variable that will tell which header to show"
}
]
}

 

 

And I can reference this very well using {{ settings.global-variable-id }}, even in if statements

 

However it is missing the dynamic change of this value, which - for what I have read - I need to connect to the Shopify REST API. I was never very good at this, but I have wrote the following code:

fetch('https://theendlessmarket.myshopify.com/admin/api/2023-01/{endpoint}.json', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': 'MYPRIVATEACCESSTOKEN'
},
body: JSON.stringify({
"settings": {
"global-variable-id": "guitar"
}
})
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});

 

I believe all this is correct except the URL as I cannot find anywhere the endpoint for the settings.schema. I'm running this code between the {% javascript %} tags on the header,  for reference.

 

Any help on this would be appreciated. I'm using Shopify 2.0 refresh

Reply 1 (1)

jplourenco
Shopify Partner
4 0 0

Does anyone have any idea? Any guidance would be much appreciated