Samples
CSharp
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace WIS_Webhook_Sample
{
public class WisEvent
{
public required string TaskId { get; set; }
public required string Status { get; set; }
}
public class WisEventWebhookResponse
{
public required string TaskId { get; set; }
}
}
namespace WIS_Webhook_Sample.Controllers
{
[ApiController]
[Route("[controller]")]
public class WisEventWebhookController : ControllerBase
{
[ProducesResponseType(typeof(WisEventWebhookResponse), StatusCodes.Status200OK)]
[HttpPost(Name = "WisEventWebhook")]
public IActionResult Post(WisEvent myEvent, [FromHeader(Name = "x-auth-token")][Required] string authToken)
{
// Check that the event comes from WIS by checking x-auth-token
if (authToken != "YOUR TOKEN HERE")
{
return new UnauthorizedObjectResult(new { error = "Wrong x-auth-token" });
}
// you can do some processing there
// (in our example, we print the task's id and the status in the console)
Console.WriteLine(string.Format("Task {0} is now {1}.", myEvent.TaskId, myEvent.Status));
// --
// The route MUST return the event
return this.Ok(new WisEventWebhookResponse { TaskId = myEvent.TaskId });
}
}
}
NodeJS/Typescript
WebhookHandler.ts
import { Request, Response } from "express";
export class WebhookHandler {
/**
* @param token
*/
constructor(private token: string) {}
/**
* Handle incoming request
*/
public handle(req: Request, res: Response): void {
const data = this.preflightCheck(req, res);
if (data === false) {
res.status(400).send();
}
this.run(data);
res.status(200).send();
}
/**
* Check the request is correctly formed
*/
private preflightCheck(req: Request, res: Response): false | any {
const method = req.method;
const tokenHeader = req.headers["x-auth-token"];
// Ignore token
if (this.token !== tokenHeader) {
this.log(`Invalid X-Auth-Token: ${tokenHeader}`, true);
res.status(400).send(`Invalid X-Auth-Token: ${tokenHeader}`);
return false;
}
// Ignore non POST requests
if (method !== "POST") {
this.log(`Invalid method: ${method}`, true);
res.status(400).send(`Invalid method: ${method}`);
return false;
}
const body = req.body;
// Ignore invalid bodies
if (!body) {
this.log("Invalid request body", true);
res.status(400).send("Invalid method");
return false;
}
return body;
}
/**
* Process the request
*/
private run(data: any): void {
// Log the request in the built-in server log
// Here you should handle the request to do whatever you want to do
this.log(`Received request - Task ID : ${data.taskId} - Status : ${data.status}`);
}
/**
* Log a message
*/
private log(message: string, isError: boolean = false): void {
const output = `[${isError ? "ERROR" : "SUCCESS"}] ${message}`;
if (isError) {
console.log(output);
} else {
console.error(output);
}
}
}
webhook_endpoint.ts
import * as dotenv from "dotenv";
import express, { Request, Response } from "express";
import { WebhookHandler } from "./WebhookHandler";
import bodyParser from "body-parser";
dotenv.config();
const handler = new WebhookHandler(process.env.BLURIT_WEBHOOK_TOKEN!);
const app = express();
app.use(bodyParser.json());
app.post("/", (req: Request, res: Response) => handler.handle(req, res));
const port = process.env.BLURIT_WEBHOOK_PORT || 8000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Last updated