Learn how to manage various aspects of Airstack webhooks for your application, from webhook creation, receiving payload, etc.
Create New Webhook
Currently, there is no dedicated API to update an existing webhook. If you would like to make changes to your webhook, you should first delete your webhook and create a new one with a new configuration.
You can simply call the /webhooks API as shown below to create a new webhooks with the receiving endpoint and filter configuration provided to the body request.
To learn more about filter configuration, check this section here.
// Prerequisites: npm install axiosimport axios from'axios';consturl='https://webhooks.airstack.xyz/api/v1/webhooks';constheaders= {'accept':'application/json','Authorization':'YOUR_AIRSTACK_API_KEY','Content-Type':'application/json'};constdata= { endpoint:'YOUR_ENDPOINT',// your endpoint filter_config: { event_type:'profile.updated',// Listen to all profile updates }};axios.post(url, data, { headers }).then(response => {console.log(response.data); }).catch(error => {console.error('There was an error!', error); });
// Prerequisites: npm install axiosconstaxios=require('axios');consturl='https://webhooks.airstack.xyz/api/v1/webhooks';constheaders= {'accept':'application/json','Authorization':'YOUR_AIRSTACK_API_KEY','Content-Type':'application/json'};constdata= { endpoint:'YOUR_ENDPOINT',// your endpoint filter_config: { event_type:'profile.updated',// Listen to all profile updates }};axios.post(url, data, { headers }).then(response => {console.log(response.data); }).catch(error => {console.error('There was an error!', error); });
{"webhook_id":"01HYDYEBHMANSVJ0JKSVK9W3VY", "portal_link": "https://apiserver.instance-fm94fpopa.hc-fhtewk6q9.us-east-2.aws.f2e0a955bb84.cloud/portal?token=am4AdjH0bz67JDDTRwNesZMO",
"authentication": {"header_name":"x-airstack-webhook","header_value":"G85wBFVT1EH7OWrs" },"status":true,// indicates the your webhook has been successfully created"message":"Successfully created subscription"}
Receiving Payload From Webhooks
To receive data payload from webhooks, you will need to have a POST endpoint where you can receive the payload in the body:
import express, { Request, Response } from"express";import bodyParser from"body-parser";constapp=express();app.use(bodyParser.json());app.post("/webhook", (request:Request, response:Response) => {console.log(request.body);// Add your business logic hereresponse.status(200).json("Success");});app.listen(4000, () =>console.log("Running on port 4000"));
constexpress=require("express");constbodyParser=require("body-parser");constapp=express();app.use(bodyParser.json());app.post("/webhook", (request, response) => {console.log(request.body);// Add your business logic hereresponse.status(200).json("Success");});app.listen(4000, () =>console.log("Running on port 4000"));
Then, simply add your business logic before returning status 200 and your endpoint is ready to receive data payload from AIrstack webhooks.
This endpoint have to be accessible through the internet, so if you are developing locally, you should expose your endpoint by creating a secure tunnel with service such as ngrok.
To create a secure tunnel with ngrok, simply run the following command:
ngrokhttp4000# Or replace with the PORT for your endpoint
This will provide you with a tunnel URL that you can provide as an endpoint when creating the webhook.
Once your endpoint is ready for production, simply deploy it to your preferred hosting platform.
Validating Filter Configuration
Filter configuration has 3 available fields:
event_type (Required): specify the type of events on Farcaster network to listen to, check out the list here.
filter: specify to filter based on the available filter field and the value specified. For profile.created and profile.updated events, check out ProfileFilter. For follow.created and follow.deleted events, check out FollowFilter.
payload: Only required if filter field exist. This field accepts a sample payload which needs to have the same field value for fields specified in filter, while others can have arbitrary value. For profile.created and profile.updated events, check out ProfilePayload. For follow.created and follow.deleted events, check out FollowPayload.
In order to ensure that your webhooks are created successfully, it is best practice that you validate your filter configuration as shown below:
In payload, ensure that the value of fields matched to the value provided in filter field. Other fields that is not provided in filter field can just use arbitrary value in payload.
Otherwise, if this is not fulfilled, validation will fail.