Understanding the Developers - Webhook section

What is a Webhook?

Webhooks are a powerful feature that enables seamless communication between your application and external services or systems. They allow real-time data exchange, making them a valuable tool for developers to automate workflows, trigger actions, and enhance the functionality of their applications.

In this article, we will walk you through the process of using Webhooks for Developers in Dataslayer, a new feature we've added to our platform. When sharing an authentication link to your clients, we will automatically sent you a notification email to let you know that someone has granted you access to his/her credentials. However, this is not the only way to get notified about this. By using the webhooks, we will sent directly to your app some parameters to notify you.

This is an advanced functionality that requires some coding knowledge.

Using webhooks in Dataslayer 

By following these steps, you'll be able to set up and use webhooks efficiently to enhance your application's capabilities.

 

faqs-2_mg1ddh

1. Access the Developers Page

To get started, navigate to the Developers section within our platform. This is where you will manage and configure webhooks for your application.

2. Add a New Webhook

Once you're in the Developers section, click on the "+ Add Webhook" button on the right. This action will initiate the process of creating a new webhook. You will need to specify the URL of your endpoint where you want to receive webhook payloads. This URL should be the location where your application will receive incoming data and has to be public.

3. Copy the Secret Key

We recommend copying the secret key generated by our platform during the webhook setup process. This secret key will be used to verify the authenticity of incoming webhook payloads.

 

faqs-1_1qw6l9z

4. Environment Variable for Secret

For enhanced security, consider creating an environment variable on your server to store the secret key (copied in Step 3). This practice ensures that your secret key remains protected.

5. Installing the Fernet Package (NPM)

To decrypt the content of webhook payloads, you will need to install the Fernet package using npm. This package allows you to securely decode the data received from the webhook. Find below how to install the package:

npm i fernet

We ONLY allow webhook connections from this package.

6. Creating a Function or Class

You will need to create a function or class in your programming language that references your endpoint (URL specified in Step 2).

7. Implementing the Webhook Handling

In the controller (or equivalent) of your application, implement one of the following codes (or the language of your choice) to manage the POST request:

PHP code sample

use Fernet\Fernet;
// ...

$secret = env("DATASLAYER_WEBHOOK_SECRET");
$fernet = new Fernet(base64_encode($secret));
$payload = @file_get_contents('php://input');
$parsePayload = json_decode($payload, true);
$decryptedData = $fernet->decode($parsePayload["encrypted_data"]);
$decodeData = json_decode($decryptedData, true);
// ... Your custom logic here

PYTHON code sample

import base64
import json
from cryptography.fernet import Fernet
from os import getenv
# ...

secret = getenv('DATASLAYER_WEBHOOK_SECRET')
fernet = Fernet(base64.b64encode(secret.encode()))
# payload is the parameter passed in the POST request, this is what we want to decrypt
decrypted_data = fernet.decrypt(payload.encrypted_data)
decode_data = json.loads(decrypted_data.decode())
# ... Your custom logic here

Understanding the codes above:

  • secret: we retrieve the secret key obtained in the Step 3 and established in Step 4.
  • fernet: we initialize a variable using the Fernet library with the previously obtained secret key.
  • payload: we fetch the encrypted data received from the webhook.
  • parsePayload: we parse the content of the webhook payload.
  • decryptedData: we decode the content using the Fernet library.
  • decodeData: we further decode the content into a JSON format that we can work with. After this, you can add any additional processing or logic as needed.

8. Handling the Decoded Data

Once the data is decoded, you can access the payload, which will contain valuable information for your application. This is the information we will send you once a user successfully log in with the shareable link you sent to him/her:

PHP code sample

$params = [
'type' => 'shared_connection',
'datasource' => $datasource,
'email' => $email,
'created' => time(),
];

PYTHON code sample

params = {
type: 'shared_connection',
datasource: datasource,
email: email,
created: time()
}
  • The time() function returns the date in UNIX TIMESTAMP. 

By following these steps, you can successfully set up and use webhooks for developers. This feature will enable your application to receive real-time data updates, enhancing its functionality and automation capabilities. Feel free to customize the decoded data to meet your specific requirements and unlock the full potential of webhooks in your application.

As always, please contact us via our live chat on our website or via email if you still have doubts or questions. We are happy to help!