Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda