This guide will teach you how to catch events from Dynamic webhooks using a serverless function.

Prerequisites

Step 1: Scaffold a serverless function

You can do this directly through AWS, but we prefer to use Serverless which creates a helpful layer of abstraction on top of Lambda.

First install their framework:

npm install -g serverless

Then run their setup command:

serverless

You will need to plug in your AWS credentials during setup, which you can find in your AWS console.

When asked which template you want to use, select AWS - Node.js - HTTP API as this tutorial will be using Node.js.

If you cd into the directory that was created, you should see an index.js file which is where we will be writing our code.

It will look something like this:

module.exports.handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};

Step 2: Create our webhook inside Dynamic

We need to create a webhook inside Dynamic that will send a POST request to our serverless function whenever a new user signs up.

To do this we can visit the webhooks section of the dashboard and click “Create Webhook”. It will ask you for a URL and to mark the events you want to listen to.

For now we can use a fake URL, which we can generate from webhook.site. This will give us a URL that we can use to test our webhook, and will show us the data that is being sent to it.

For the events, you should make sure user.created is selected, and that’s it! Save away and a test event will be sent to your URL.

Step 3: Adapt the function to receive a Dynamic webhook event

Let’s say in this instance We are most interested in the user object, as this is returned by the user.created event. You can find the full list of events here.

As you’ll see from the event reference, the only required field on the object is the ID, so if you want to collect more information you’ll need to make sure you’re doing Info Capture on signup.

In this tutorial we will assume we are using email signup with embedded wallets, so we’ll always have email available on the user object at time of signup

In the test event, when you inspect it using webhook.site, you’ll see the payload comes with a data field, and it’s inside this field that the user object will be present. Here’s how we can use the email inside the function:

module.exports.handler = async (event) => {

  const email = event.data.email;

  console.log(email);

  ...
};

Note that we are not adding error handling in the end response, you should do so by adapting the returned status code and message etc.

Step 4: Test & deploy your function

Serverless hooks into the AWS local testing functionality using invoke local.

You can find the function name in the serverless.yml file, and then run the following command to test it:

serverless invoke local --function FUNCTION_NAME_HERE --data '{data: {"email":"[email protected]"}}'

If you get a log of the user email, you’re ready to deploy your function so you can run:

serverless deploy

As long as this is successful, you should get a URL for your function and you can use that to update your webhook in Dynamic from the webhook.site URL.

Now you’re ready to run it! Feel free to use one of the example apps to test out creating a new user via the end UI!

Step 5: Going further

You can use this approach to send notifications for any event, and you can use any service you like to handle the processing of the event.

Don’t hesitate to check out Zapier Webhooks to hook into many other services, CRMs and so on.