Cant get products.json from API

Pablo213
Shopify Partner
10 0 0

Hi, is there anybody who can helps me with the fetch request to shopify API? I would like to get all products and use them for product select in form. I dont really know whats wrong with it.

I read docs and i think it should work this way, but i am getting console errors and cant get response.

I had created private app because access and tokens, but still cant find the solution.

 

Výstřižek2.PNG

Výstřižek.PNG
 

 

Replies 9 (9)
Surbhi_Dudhe
Shopify Partner
113 10 11

Hello @Pablo213 

Your Api code is correct, you face this issue because you do not use a header to access JSON data.

Because shopify is for security purposes not allow third-party access to data.

You need to paste the below code into your code file on the above.

 

 header("Access-Control-Allow-Origin: *");

 

Thank you
- Is my reply helpful? Click Like to let me know!
- If you got your answer? Mark it as an Accepted Solution



If you wish to engage the services of a Shopify developer, please contact me via email at surbhidudhe022@gmail.com.
Pablo213
Shopify Partner
10 0 0

Do you mean add this code to headers: {

'Content-Type': 'application/json',
'X-Shopify-Access-Token':"fb70610e4eb8cbd8c14557560e8a190a-1681990272", 'Access-Control-Allow-Origin': '*',

}

this way? Thats what i found on the internet

Pablo213
Shopify Partner
10 0 0

I have it this way now, but still doesnt work

Pablo213_0-1681991470510.png

 

Surbhi_Dudhe
Shopify Partner
113 10 11

I think you used Node language to create an app.

I have an idea about PHP language. 
Could you please check how to apply the header for Allow Access control origin *?

Thank you
- Is my reply helpful? Click Like to let me know!
- If you got your answer? Mark it as an Accepted Solution



If you wish to engage the services of a Shopify developer, please contact me via email at surbhidudhe022@gmail.com.
Pablo213
Shopify Partner
10 0 0

Idk man, but i am doing it with javascript now and still doesnt work..

Surbhi_Dudhe
Shopify Partner
113 10 11

fetch('https://your-shop-name.myshopify.com/admin/api/2023-04/products.json', {

headers: {

'Access-Control-Allow-Origin': '*'

}

})

.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error(error));

Thank you
- Is my reply helpful? Click Like to let me know!
- If you got your answer? Mark it as an Accepted Solution



If you wish to engage the services of a Shopify developer, please contact me via email at surbhidudhe022@gmail.com.
Pablo213
Shopify Partner
10 0 0

I use it that way exactly, but where is the problem? In headers i have an extra access- token which i should use i guess?

Pablo213_0-1681993019224.png

 

Surbhi_Dudhe
Shopify Partner
113 10 11

When I search it's on Google 

// Set the Access-Control-Allow-Origin header to *
response.setHeader("Access-Control-Allow-Origin", "*");

// Define the headers for your API request
const headers = {
'X-Shopify-Access-Token': 'your-access-token-here',
'Content-Type': 'application/json'
};

// Make a GET request to a Shopify API endpoint
fetch('https://your-shop-name.myshopify.com/admin/api/2023-04/your-resource.json', { headers })
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

Thank you
- Is my reply helpful? Click Like to let me know!
- If you got your answer? Mark it as an Accepted Solution



If you wish to engage the services of a Shopify developer, please contact me via email at surbhidudhe022@gmail.com.
Pablo213
Shopify Partner
10 0 0

Am i doing anything wrong besides writing differently?