DEV Community

Cover image for How to set up WebHooks on SashiDo
Vesi Staneva for SashiDo.io

Posted on • Originally published at blog.sashido.io

How to set up WebHooks on SashiDo

SashiDo launched Advanced Cloud Code so you can have the ability to write more complex logic, add 3rd party API libraries and packages which provide you a lot of ready-to-use functionality. That’s awesome, but what happens if you want to implement your server-side logic in a language other than Javascript? Or if you want to use in your project a tool, not supported by Cloud Code? Then the perfect solution is to set up WebHooks on your application. They are an HTTP POST callback request sent to URL of a user's choice in response to some occurring event. They offer simple and effective server-to-server communication without long-running connections.

WebHooks give you the freedom to use the programming language that you want and to add specialized libraries and technologies. You can write functions on your preferred language and host locally for testing before deployment.

Webhooks use cases ideas

You can use WebHooks whenever you want to add technology, not supported by Cloud Code. For example - integration with Billing, e-mail templates, PayPal, Slack etc. A webhook delivers data to other applications as it happens, meaning you get data immediately.

How to use WebHooks?

It’s quite easy on SashiDo. Let’s imagine we have an app with active paid users and free trial. We need to know which users are paid and which not so we can stop their access to the system. We’ll do this by implementing a simple function in Python and framework Flask, but the principle is the same with other languages and technologies. If you want to run the sample code locally, you’ll need a tunneling service like ngrok or ultrahook to map your local http endpoints to public URLs.

  • Go to SashiDo’s Dashboard > Browser and create a new class User with two instances: jane and john so we can test.
  • Add two properties to the class: plan and payment. Set value “try” to plan.

  • Now let’s write the function: Function webhook() has POST method and requests the current user to check its payment status. We also implemented a simple help function make_payment() to fake the payment logic in order to do our test.

  • Now go back to SashiDo’s Dashboard Core -> WebHooks. Press Create a WebHook and choose a type, a Class, and add the URL of the WebHook:
    We have deployed for your convenience the Python code from above here so you can try with it without any coding right now: https://webhook-sashido-examples.scalabl.cloud/webhook

  • It’s time to test :) Go back to the Browser and fill property plan with value “try”. After refreshing we see the results:

Looks like our function has done its jobs. Now you can use any framework or language to build awesome apps.

If you are fans of Python and want to play with the example on your local computer, we uploaded the source here: https://github.com/SashiDo/webhook-handler-example

If you don’t want to clone and run the whole example, we uploaded it live for quick tests:
https://webhook-sashido-examples.scalabl.cloud/webhook

What about Cloud functions and triggers?

Let’s take a more detailed look to what Webhooks Cloud functions and triggers offer:

Cloud function: A webhook request for a Cloud function. It must contain the following parameters:

  • master: True // if the master key was used and false otherwise.
  • user: If set, this will contain the Parse User who made the request, in our REST API format. This is not set if the master key is used.
  • installationId: If available, the installationId which made the request.
  • params: A JSON object containing the parameters passed to the function. For example: { "foo": "bar" }
  • functionName: The name of the Cloud function.

Send a JSON object with the key error or success set to respond to this request. If the result is a success: send back the expected data or simply “true”. Otherwise: return the error message.

For beforeSave, afterSave, beforeDelete, afterDelete triggers , the following parameters are sent to your webhook.

  • master: True // if the master key was used and false otherwise.
  • user: If set, this will contain the Parse User who made the request, in our REST API format.
  • installationId: If available, the installationId which made the request.
  • object: For triggers, this will contain the Parse Object, in our REST API format. For example: { "className": "TestObject", "foo": "bar" }. triggerName: "beforeSave"

Send a JSON object for beforeSave trigger with the keyerror or success set to respond to a beforeSave request - same as Cloud function. The difference is when you return an error, you will cancel the save request and the object will not be stored.

No response is required for afterSave and afterDelete triggers.

To respond to a beforeDelete request, send a JSON object with the key error or success set. If you return an error this will cancel the delete and the object will remain unchanged in your database.

If you have any issues or questions contact us at support@sashido.io

Happy coding :)

Top comments (1)

Collapse
 
mixoglad profile image
Mish

Sashido is boss💪💪. It's user friendly. I had a wonderful experience. 😎😎