Atul
Atul

Reputation: 21

Server side API call in Shopify cart page

We are currently calling third party(our API to OPT-IN that user in our system) API using ajax in cart page for OPT-IN feature. Consider that user is already created in our system at the time of registration using webhook. Now just need to OPT-IN that user.

But by calling API using ajax we are making Access Token Visible. So, it's not secure way to implement API'S.

Need API call to create shopify public APP. In that checking the user status in our system using API. Depending upon API response have to decide Show/Hide one button (that button is added in cart page.). I am talking about implementation of our API for creating APP. That API need Access Token which is provided by us.

So, for security purpose of access token need to implement server side API in Shopify cart page.

async function getData(){
            const result= await fetch("https://s15.socialannex.net/apiv2/userstatus/SITE_ID/{{ customer.email }}?access_token=ACCESS_TOKEN",{
                method: 'POST',
                data: {
                    'first_name': 'Atul'
                },
            });
            var res = await result.json();
            if(res.error_code == 0){
                $(".join-loyalty-button").css("display","none");
            }
          }

Above code is working fine but its ajax call. I want to call above API at server side.

Upvotes: 1

Views: 1404

Answers (1)

David Lazar
David Lazar

Reputation: 11425

You want to use the App Proxy pattern. See the documentation here:

https://help.shopify.com/en/api/guides/application-proxies

With that, you can callback using Ajax to your API with any information important to your callback. Example, the customer ID. The callback is secure, and no security tokens are exposed. You can return JSON meaning your front-end code can show/hide buttons based on an answer from your internal App.

Upvotes: 1

Related Questions