Search code examples
apibase64externalvelo

How to add private members to external api


Good afternoon all,

I am attempting to create a function that will automatically create a membership through my external loyalty program (through Whisqr) for the current user on my Wix.com website. I am receiving an error message stating the public key is not found.

Here is my backend code:

 import {fetch} from 'wix-fetch';  
import {wixData} from 'wix-data';


export function postLoyalty() {
 let options ={
 "headers": {        
 "X-Public": "pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76"
        }
  }
 const url = 'https://whisqr.com/api/v1.2/user/customer/';
 const key = '<pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76>';
     console.log("Url: ");

 return fetch(url, {method: 'post'})
    .then(response => {
 return response.json();
    })
    .then((data) => {
      console.log(data);
 return data;
    });
}

Here is my page code:

import {postLoyalty} from 'backend/Loyalty.jsw';
import {wixData} from 'wix-data';
import wixLocation from "wix-location";
import {myFunction} from 'public/core.js';
import wixUsers from 'wix-users';


$w.onReady(function () {
 let publickey = 'pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76';
    myFunction(publickey)
    .then( (response) => {
        console.log(response); //your base64 encoded string
    })});

export function page1_viewportEnter(event) {
 //Add your code for this event here: 
 let email = wixUsers.currentUser.getEmail();
postLoyalty(email)
        .then(LoyaltyInfo => {
            console.log(LoyaltyInfo)
            $w("#text1").text = LoyaltyInfo.Results.Value;
        })
}


Any and all feedback is greatly appreciated!


Solution

  • You are making a call to the URL using the POST method but you are not utilizing any of the keys, headers which you have defined.

    A proper POST call which utilizes the header and body in its request will look like the below:

        export function myFunction(data) {
    
         const url = "https://whisqr.com/api/v1.2/user/customer/";
    
          const headers = {
              "Authorization": "Bearer " + key, //if api key is required like this
              "Content-Type": "application/json" //the content type
            };
    
          return fetch(url, {
                "method": "POST",
                "headers": headers,
                "body": JSON.stringify(data) //if there is a body
            });
        }
    

    You said that you need to create a member on the external platform so you must be needing to send a body with the customer's data. Read the API Documentation.