Skip to main content
Answered

Call URL endpoint when button is pressed

  • March 11, 2024
  • 1 reply
  • 100 views

Hi there! 

I am building an app to be used in Inbox using Glitch. 
In this app I need two buttons: 
  - One to call my endpoint API address.

  - Another button to call my another endpoint API address.
Obs: I don't want to open a link when button is pressed

How could I build this?

Best answer by Ebenezer.Laleye

Hi @Carlos062 ! Ebenezer here from Engineering Support👋.

Getting this built may require some developer muscle.

I can provide a rough outline of how this would look like 👇

document.getElementById('callApi1').addEventListener('click', function() {
      callApi('YOUR_API_ENDPOINT_1');
    });

    document.getElementById('callApi2').addEventListener('click', function() {
      callApi('YOUR_API_ENDPOINT_2');

function callApi(endpoint) {
      // Make an API request to the specified endpoint
      fetch(endpoint, {
        method: 'GET', // Or 'POST', 'PUT', etc. depending on your API
        headers: {
          'Content-Type': 'application/json',
          // Add any required headers here
        },
        // Add any required body parameters here for POST requests
      })
      .then(response => {
        if (!response.ok) {
          throw new Error('Network response was not ok');
        }
        // Handle successful API response here
        console.log('API call successful');
      })
      .catch(error => {
        // Handle errors here
        console.error('There was a problem with the API request:', error);
      });

 

Hope this helps!

View original
Did this topic help you find an answer to your question?

1 reply

Forum|alt.badge.img+4

Hi @Carlos062 ! Ebenezer here from Engineering Support👋.

Getting this built may require some developer muscle.

I can provide a rough outline of how this would look like 👇

document.getElementById('callApi1').addEventListener('click', function() {
      callApi('YOUR_API_ENDPOINT_1');
    });

    document.getElementById('callApi2').addEventListener('click', function() {
      callApi('YOUR_API_ENDPOINT_2');

function callApi(endpoint) {
      // Make an API request to the specified endpoint
      fetch(endpoint, {
        method: 'GET', // Or 'POST', 'PUT', etc. depending on your API
        headers: {
          'Content-Type': 'application/json',
          // Add any required headers here
        },
        // Add any required body parameters here for POST requests
      })
      .then(response => {
        if (!response.ok) {
          throw new Error('Network response was not ok');
        }
        // Handle successful API response here
        console.log('API call successful');
      })
      .catch(error => {
        // Handle errors here
        console.error('There was a problem with the API request:', error);
      });

 

Hope this helps!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings