Announcement

Collapse
No announcement yet.

Need Javascript help with API call

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need Javascript help with API call

    Hello forum:
    I am attempting to create a JS api call that will return the sales tax percentage to a web page when a zip code is sent with the API call
    I am using example code from rapidapi.com.
    My javascript editor gives me 3 errors but I cannot determine what is causing them.
    Maybe someone here can help me?
    This is the example code:

    const options = {
    method: 'GET',
    headers: {
    'X-RapidAPI-Key': 'MY API Key goes here',
    'X-RapidAPI-Host': 'sales-tax-rates1.p.rapidapi.com'
    }
    };

    fetch('https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=98661', options)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));

    Cheers,
    Larry
    Larry
    Luce Kanun Web Design
    www.facebook.com/wajake41
    www.plus.google.com/116415026668025242914/posts?hl=en



    #2
    I think the response.json function returns a data object. Try:

    Code:
    fetch('https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=98661', options)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(err => console.error(err));
    What error message are you getting?
    http://www.alphabetsigns.com/

    Comment


      #3
      Hello alphabet:
      Thanks for the reply.
      Actually I changed course on this and am using the following XMLHttpRequest to get the sales tax rate in a javascript function:
      const data = null;

      const xhr = new XMLHttpRequest();
      xhr.withCredentials = true;

      xhr.addEventListener("readystatechange", function () {
      if (this.readyState === this.DONE) {
      console.log(this.responseText);
      }
      });

      xhr.open("GET", "https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=99502");
      xhr.setRequestHeader("X-RapidAPI-Key", "My Key");
      xhr.setRequestHeader("X-RapidAPI-Host", "sales-tax-rates1.p.rapidapi.com");

      xhr.send(data);

      I use JS functions to extract the rate from responseText.
      Thanks again for your answer. I am keeping it for future use.
      Larry
      Larry
      Luce Kanun Web Design
      www.facebook.com/wajake41
      www.plus.google.com/116415026668025242914/posts?hl=en


      Comment


        #4
        Great, I'm glad you got it to work.

        You should take a look at fetch when you get a chance. It is incredibly fast, easy to use, works with Promises and avoids all the callbacks from the old days!

        http://www.alphabetsigns.com/

        Comment


          #5
          Hello alphabet:
          Congratulations a bit too soon. I still need to understand how I can send a zip code from my html page to the the XMLHttpRequest :(
          zipcode is a var in my function
          I am trying this:
          let url = new URL("https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=98661");
          url.searchParams.set('zip', zipcode);
          xhr.open('GET', url);
          in place of this:
          xhr.open("GET", "https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=98661");

          Just the statement defining the variable url breaks my code,
          Also the parms set breaks the code

          Have got a few hours into this but I am stumped

          I sure need some help with this. Any ideas?

          Larry
          Larry
          Luce Kanun Web Design
          www.facebook.com/wajake41
          www.plus.google.com/116415026668025242914/posts?hl=en


          Comment


            #6
            I guess you are trying to send this from the OSEL page so the zip code should be available in the basket array and set as a param:

            Code:
            url.searchParams.set('zip',&mvt:global:Basket:ship_zip;);
            Also, your code in post #5 is different than post #1 so you should double check how you are sending your credentials for authorization.

            Have you heard of Postman? It is an easy way to develop and test API's https://www.postman.com/
            http://www.alphabetsigns.com/

            Comment


              #7
              Hello:
              Actually this is for a HTML desktop application to be used for phone in orders that do not come through Miva.
              The idea is to calculate the sales tax amount using the result of the zip code sales tax rate times the sum of the shipping and order total.
              I'm using Rapidapi right now as my host but I have exhausted all of my free queries for a month with my testing.
              Going to check out Postman to see what they offer.
              Thanks again
              Larry
              Larry
              Luce Kanun Web Design
              www.facebook.com/wajake41
              www.plus.google.com/116415026668025242914/posts?hl=en


              Comment


                #8
                Got it working and learned a bit doing it. Always great to learn!
                Looked at Postman but Rapidapi had exactly the API I needed.
                Cheers
                Larry
                Luce Kanun Web Design
                www.facebook.com/wajake41
                www.plus.google.com/116415026668025242914/posts?hl=en


                Comment


                  #9
                  Comments about the final resolution on this:
                  We decided to use the full address API "Sales Tax Calculator" that is available on RapidAPI. Entering the complete property address returns the sales tax rate for that location and a flag indicating if shipping is taxed,
                  If shipping is non taxable we subtract the shipping amount from the order total before the sales tax calculation.
                  We have this in a desktop html/javascript page that will be used to calculate the sales tax amount for orders coming from other than Miva.
                  So far it appears to be accurate, if we find it is not I will update this post.

                  Cheers.
                  Larry
                  Luce Kanun Web Design
                  www.facebook.com/wajake41
                  www.plus.google.com/116415026668025242914/posts?hl=en


                  Comment


                    #10
                    Originally posted by wajake41 View Post
                    Hello forum:
                    I am attempting to create a JS api call that will return the sales tax percentage to a web page when a zip code is sent with the API call
                    I am using example code from rapidapi.com.
                    My javascript editor gives me 3 errors but I cannot determine what is causing them.
                    Maybe someone here can help me?
                    This is the example code:

                    const options = {
                    method: 'GET',
                    headers: {
                    'X-RapidAPI-Key': 'MY API Key goes here',
                    'X-RapidAPI-Host': 'sales-tax-rates1.p.rapidapi.com'
                    }
                    };

                    fetch('https://sales-tax-rates1.p.rapidapi.com/v/api/?zip=98661', options)
                    .then(response => response.json())
                    .then(response => console.log(response))
                    .catch(err => console.error(err));

                    Cheers,
                    Larry
                    wajake41, interesting idea. However, it is my understanding (at least in Florida) that appropriate sales tax calculation requires full mailing address (e.g.: Street, City, State and Zip Code) to insure the proper tax has been collected.

                    Its is for this reason we opted to subscribe to TaxJar for $20.00 a month.
                    Thank you, Bill Davis

                    Comment


                      #11
                      Hi William:
                      We came to the same conclusion regarding tax rate using zip code so we've opted to use "Sales Tax Calculator" from RapidAPI.
                      "Sales Tax Calculator" uses the full address to provide the sales tax rate. It also provides a flag indicating if shipping should be included in the sales tax calculation.
                      This will be only used for sales outside of Miva. Our Miva stores are using TaxJar.
                      Thank you for the comment,
                      Larry
                      Larry
                      Luce Kanun Web Design
                      www.facebook.com/wajake41
                      www.plus.google.com/116415026668025242914/posts?hl=en


                      Comment


                        #12
                        Originally posted by wajake41 View Post
                        Hi William:
                        We came to the same conclusion regarding tax rate using zip code so we've opted to use "Sales Tax Calculator" from RapidAPI.
                        "Sales Tax Calculator" uses the full address to provide the sales tax rate. It also provides a flag indicating if shipping should be included in the sales tax calculation.
                        This will be only used for sales outside of Miva. Our Miva stores are using TaxJar.
                        Thank you for the comment,
                        Larry
                        wajake41 Ah... I was kinda hoping you found a solution with a recurring cost.
                        Thank you, Bill Davis

                        Comment


                          #13
                          Added the USPS address verification to the module. Verified street address is passed to the Sales Tax Calculator. Much help with completing this modification was obtained via the Edge AI Chat.
                          USPS verification is returned as XML. Edge AI Chat gave me the code I needed to parse the response XML.
                          Larry
                          Luce Kanun Web Design
                          www.facebook.com/wajake41
                          www.plus.google.com/116415026668025242914/posts?hl=en


                          Comment

                          Working...
                          X