I have a web trigger setup to run a fetch request after an external webhook sends a POST request to the web trigger’s URL. The web trigger works and receives the requests from the webhooks successfully.
However, I need to run a fetch request to an external API after this happens and I cannot figure out how to get it to run. I have a console.log after the fetch request and nothing gets logged.
Any pointers on another approach or where to look would be greatly appreciated. Thanks for the help!
Here is my code if that helps:
import { fetch } from "@forge/api";
const token =
"token123";
exports.runAsync = (request) => {
const requestJSON = JSON.parse(request.body);
if (request.method != "GET") {
const target = requestJSON.data.links.target;
// The target is the request URL to the external API which is always successfully logged
// I tried hardcoding it and got the same result
sendRequest(target, "get").then((data) => {
console.log(data);
});
} else {
// A POST request is sent to authorize the webhook
const validationToken = request.queryParameters.validationToken[0];
const result = forgeWebhookResponse(validationToken);
return result;
}
};
// Response used to authorize webhook
const forgeWebhookResponse = (token) => ({
body: token,
headers: {
"Content-Type": ["text/plain"],
Accept: ["*/*"],
},
statusCode: 200,
});
// Function to send fetch request to external API
async function sendRequest(url, method) {
const fetch = await fetch(url, {
method: method,
headers: {
"Authorization": token,
"Content-Type": "application/json",
},
redirect: "follow",
});
const data = await fetch.json();
return data;
}