Admin api in javascript

Viktor4
Shopify Partner
1 0 0

I try get some data from store, in some storepage. It is product count and product list. I try use Admin api, but it not work. 

I have this code:

$.ajax({
    url: "https://[removed]:[removed]@asinstore.myshopify.com/admin/products.json",
    type: 'get'
}).then(function (res) {
    console.log(res);
});

But in res i get html code for some page, not product json. What do I do wrong?

 

 

Replies 2 (2)
Jason
Shopify Expert
10873 190 2177

First up - you just posted a key name and password for a private key on a public forum. I've removed them from your post, but you should immediately delete that key and create a new one. 

As for the question - you should not be using the API in this way. Much like the issue here on the forums - you're exposing the key details for the world to see and use however they like.

Make sure your API calls are authenticated, and key details kept private.

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
Arundas
Explorer
70 3 70

The expected way for you to do this would be to create a private app (hosted on your own server) and use that to manipulate the Admin API. You should also setup an endpoint on your private app, so that your JS can send the required data there.

However, if that's too much headache to even think about, look at this app called Mechanic. It's a very powerful app where you can set up a webhook, and you can simply call the webhook from your JS, and send the data in JSON format. Then you need to have a task setup in Mechanic in which you can do literally anything that's possible using the REST or GraphQL versions of the Admin API!

The advantage is that you don't need to write your own private app and host it on your own server. Mechanic takes care of all the hosting for you, and they have a collection of task scripts you can easily adapt and modify. They also have support to do custom coding for you.

Disadvantages: Third-party app, and also Mechanic webhooks don't give any response beyond 204 No Content, so your Javascript won't be able to tell whether your request was a success or failure, and you can't retrieve data. (You can use the regular Storefront API to retrieve data though, as this doesn't need any authorization.)

Disclaimer: I'm not paid by Mechanic. I just think it's really awesome for some use cases.

If my reply helped you, please give it a Like and mark it as Accepted Solution.
Do you need help customizing your theme or editing code? Email me!