Back

Best case use of webhooks.?

  • 0
  • Flutter
  • Webhooks
  • Databases
  • Accounts
  • Functions
VincentGe
27 Sep, 2023, 15:13

You can write an Appwrite function to make HTTP request to the endpoints, and also update Appwrite collections that you need to update.

TL;DR
Webhooks are used to trigger events based on specific actions. The payload of the event is the body of the event. For schedule events, it's better to use function variables or store data in the database instead of a body. In a C.function, you can fetch the JSON and perform tasks like updating a payments table. To access the request info, you can use `req.body`, which will already be parsed if it's JSON. The function will be triggered by the provider sending a JSON response with payment details. You can handle the request how you wish. The provider will make a request to the function's callback URL, so make
frankenstein
27 Sep, 2023, 22:39

Payments providers have a callback url which is where the response is sent after user has paid. How can I use this to automatically trigger a function that updates my table

VincentGe
27 Sep, 2023, 22:40

Ohh I see, create a function

VincentGe
27 Sep, 2023, 22:40

The function will have it's own domain

VincentGe
27 Sep, 2023, 22:40

Use that domain to be the call back URL

VincentGe
27 Sep, 2023, 22:40

The provider will make a request to that callback URL (i.e. your function)

VincentGe
27 Sep, 2023, 22:40

And you can handle the request how you wish

frankenstein
27 Sep, 2023, 22:41

Yes yes

frankenstein
27 Sep, 2023, 22:42

The provider sends the json response with payment details like transaction ID, payment and status

frankenstein
27 Sep, 2023, 22:43

So does the cloud function pick this json response or its just triggered

VincentGe
27 Sep, 2023, 22:44

It will be sent in body right?

frankenstein
27 Sep, 2023, 22:44

Yes as a json

VincentGe
27 Sep, 2023, 22:44

The body will be passed in

TypeScript
export default async ({ req, res, log }) => {
    log(req.bodyRaw);                     // Raw request body, contains request data
    log(JSON.stringify(req.body));        // Object from parsed JSON request body, otherwise string
    log(JSON.stringify(req.headers));     // String key-value pairs of all request headers, keys are lowercase
    log(req.scheme);                      // Value of the x-forwarded-proto header, usually http or https
    log(req.method);                      // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
    log(req.url);                         // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
    log(req.host);                        // Hostname from the host header, such as awesome.appwrite.io
    log(req.port);                        // Port from the host header, for example 8000
    log(req.path);                        // Path part of URL, for example /v1/hooks
    log(req.queryString);                 // Raw query params string. For example "limit=12&offset=50"
    log(JSON.stringify(req.query));       // Parsed query params. For example, req.query.limit

    return res.send("All the request parameters are logged to the Appwrite Console.");
};
VincentGe
27 Sep, 2023, 22:45

If it's json, req.body will already be parsed automatically

VincentGe
27 Sep, 2023, 22:45

So you can access the request info

frankenstein
27 Sep, 2023, 22:47

Sorry am left behind.

frankenstein
27 Sep, 2023, 22:50

Can you give me an idea how to fetch the json in c.function and use the to perform other tasks like updating payments table

Drake
27 Sep, 2023, 23:00

i highly suggest you take a look at the docs and play around with the functions to get a better idea

frankenstein
27 Sep, 2023, 23:24

Thanks

frankenstein
27 Sep, 2023, 23:25

I've read the docs. I have got an idea where to start.

frankenstein
27 Sep, 2023, 23:26

But I guess you can't pass the json body when you execute on certain events or schedule

Drake
27 Sep, 2023, 23:28

for events, the body will be the payload of the event. for schedule, it doesn't make sense to have a body...you would use function variables or store data in the database

frankenstein
27 Sep, 2023, 23:29

What do you mean by payload of the event.??

Drake
27 Sep, 2023, 23:59

so for example, if the event is a document update, the body will be the updated document...

frankenstein
28 Sep, 2023, 05:45

Thanks 🤜🤛

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more