Answered

Call URL endpoint when button is pressed

  • 11 March 2024
  • 1 reply
  • 16 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?

icon

Best answer by Ebenezer.Laleye 20 March 2024, 17:08

View original

1 reply

Userlevel 2
Badge +3

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