Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
Problem to call an API inside a Webhook - Javascript

I'm creating a chatbot using a Webhook in Javascript and in a moment I need to connect to my API(using a POST) to get some data and send the information received inside a message to the user. The problem here is the API is not been called and the flow just bypass my connection(I don't even see the connection in the API server).

I've run some testes with the API out of the Webhook and it worked. I've tried to use async function and await but the application continues to "ignore" my API call.

Below is my code with my tests. I've made some changes with the URL just fo security purpose, but the logic is the same.

var price = 0
var payload = {'product_id': 'n/a'} **//Creating this global variable just for testing**

//CREATING THE FUNCTION TO CONNECT TO MY API USING AXIOS
function my_api(payload){
app.get("/", async(request, response) => {

const {data} = await axios.post('https://www.test.com/my_api', payload)
price = data.price
});
}

//FILLING MY TEST PAYLOAD
payload = {'product_id':10}

//CALLING THE API OUT OF THE WEBHOOK AND PRINTING THE RETURN ON MY WEBSITE CONSOLE
my_api(payload) **//If I call the API here, the application is accessed by the function. I confirmed that writing a response.send(price) command in the API function and check my API Server log**


//STARTING MY WEBHOOK
app.post("/webhook", async function (request, response) {
  const agent = new WebhookClient({ request: request, response: response });

//THE FUNCTION WHERE THE API IS CALLED AFTER THE USER'S INPUT IN THE LAST INTENT AND I NEED TO WAIT IT RETURN WITH THE RESULT TO SEND THE MESSAGE
async function product_price(agent){
user_input = agent.query
var payload = {'product_id': user_input}
await my_api(payload) **//When I call the API function here it don't connect to my application**
agent.add('The price for this product is '+price)
}
}

Do you have any idea how can I connect to the API in the Webhook?

I've tried to create a function outside the Webhook and call the my_api(payload) function inside it but it didn't work.

Thank you for your attention.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have a rest endpoint /webhook and in it's handler you create a function product_price but never execute it. To fix it you just have to call it like product_price() but in your code there is no need to create this function at all

//STARTING MY WEBHOOK
app.post("/webhook", async function (request, response) {
  const agent = new WebhookClient({ request: request, response: response });
  const user_input = agent.query
  const payload = {'product_id': user_input}
  await my_api(payload)

  agent.add('The price for this product is '+price)
}

Your code has some weird stuff, that should also be improved: in my_api you create a rest endpoint for / but why? I guess you just want to make a request to get the price?

async function my_api(payload){
  const {data} = await axios.post('https://www.test.com/my_api', payload)
  price = data.price
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda