I did setup a REST source and I would like to trigger an API call to this source.
This is essentially a webhook. How can I trigger such call ?
What are the different ways to perform this call from a function ?
1 Like
There are multiple ways:
One way is to directly use the source to fetch secrets stored on it, and to perform fetch/request calls to a url using the base url specified in the source.
For example:
@ExternalSystems({ sources: [WebhookTestSource] })
@Function()
public async exampleWebhookFetchCall(): Promise<string> {
// const apiKey = WebhookTestSource.getSecret("...");
const baseUrl = WebhookTestSource.getHttpsConnection().url;
console.log("baseUrl", baseUrl)
const endpoint = baseUrl + "compass/api/ping"
console.log("endpoint", endpoint)
const response = await fetch(endpoint, {
method: "GET",
headers: new Headers({
"Content-Type": "application/json",
// "Authorization": `Bearer ${apiKey}`
})
// body: ...
})
if (response.ok) {
// const response = await response.json();
console.log("Success !")
return "success"
} else {
// TODO: handle error
console.log("Error !")
return "error"
}
}
If you define the webhook in your source directly, you can simply call this webhook with the expected inputs arguments.
@Function()
public async exampleWebhookFunctionTrigger(): Promise<string> {
const result = await WebhookTestSource.webhooks.ExamplePingWebhook.call();
if (isOk(result)) {
// handle success
console.log("Success !")
return "success"
} else {
// handle error
console.log("Error !")
return "error"
}
}
It is as well possible to have a function generating the payload the webhook can send, and configure this function in the webhook configuration in the Source itself. Such function looks like :
@Function()
public returnWebhookInput(): MyWebhookInput {
return {
name: "exampleCompanyName",
industry: "exampleCompanyIndustry",
country: "exampleCompanyCountry",
}
}
A function can also create multiple payload, hence triggering multiple webhooks:
@Function()
public createWebhookRequest(): MyWebhookInput[] {
return [
{ name: "exampleCompanyName1", industry: "exampleCompanyName1", country: "exampleCompanyCountry1" },
{ name: "exampleCompanyName2", industry: "exampleCompanyName2", country: "exampleCompanyCountry2" }
];
}
Here is the full Typescript v1 example below
import { Function, isOk } from "@foundry/functions-api";
import { ExternalSystems } from "@foundry/functions-api";
import { WebhookTestSource } from "@foundry/external-systems/sources";
export interface MyWebhookInput {
name: string;
industry: string;
country: string;
}
export class MyFunctions {
@Function()
public returnWebhookInput(): MyWebhookInput {
return {
name: "exampleCompanyName",
industry: "exampleCompanyIndustry",
country: "exampleCompanyCountry",
}
}
@Function()
public createWebhookRequest(): MyWebhookInput[] {
return [
{ name: "exampleCompanyName1", industry: "exampleCompanyName1", country: "exampleCompanyCountry1" },
{ name: "exampleCompanyName2", industry: "exampleCompanyName2", country: "exampleCompanyCountry2" }
];
}
@ExternalSystems({ sources: [WebhookTestSource] })
@Function()
public async exampleWebhookFetchCall(): Promise<string> {
// const apiKey = WebhookTestSource.getSecret("...");
const baseUrl = WebhookTestSource.getHttpsConnection().url;
console.log("baseUrl", baseUrl)
const endpoint = baseUrl + "compass/api/ping"
console.log("endpoint", endpoint)
const response = await fetch(endpoint, {
method: "GET",
headers: new Headers({
"Content-Type": "application/json",
// "Authorization": `Bearer ${apiKey}`
})
// body: ...
})
if (response.ok) {
// const response = await response.json();
console.log("Success !")
return "success"
} else {
// TODO: handle error
console.log("Error !")
return "error"
}
}
@Function()
public async exampleWebhookFunctionTrigger(): Promise<string> {
const result = await WebhookTestSource.webhooks.ExamplePingWebhook.call();
if (isOk(result)) {
// handle success
console.log("Success !")
return "success"
} else {
// handle error
console.log("Error !")
return "error"
}
}
}
3 Likes