PubNub FunctionsBuilding HTTP Webhooks and Microservices with Endpoints

Endpoints allow you to spin up Webhooks and Microservices in minutes. In this exercise we will show you how to build a Microservice using Endpoints.

App registration flows commonly require looking up the existence of a new username in the database to prevent multiple users from using the same username. Let's see how we would implement this check as a Microservice using Endpoints.

Endpoints are provided with On Request Function type. Let's start by creating a new Function with On Request type.

Create New Function

A Function will be created with a code template that shows you how to interact with the request and response objects. For this example, all we need to know is how to extract the value of a query parameter. We will be passing the username to be checked for uniqueness using a query parameter named, "username".

The line below shows how to extract the value of the query parameter and assign it to a creatively named variable "username".

    const username = request.params.username;

Next, we will make a query to our database to see if the username in question is already taken by another user and therefore stored in our database.

If we don't find the username in our database, we will reserve it, return a 200 for status and a response body consisting of the string "UNIQUE".

If we do, the status will be a 400 and the response body will say "NOT UNIQUE".

We will also send some log messages to the console to make our code easier to follow.

    const db = require("kvstore");
    return db.get(username).then((dataFromDb) => {
        if (!dataFromDb)  {
            console.log("The username does not exist!");
            db.set(username, {});
            response.status = 200;
            return response.send("UNIQUE");
       } else {
           console.log("The username already exists!");
           response.status = 400;
           return response.send("NOT UNIQUE");

We're done! Make sure to save and hit the Start Module button at the top right and your Microservice is deployed globally!

Now, let's test our Microservice using the mini HTTP client on the left side.

The text box that starts with a question mark is for passing query parameters. You can either type your query parameters here or click the wrench icon to add them using a GUI.

Once you add the query parameter, it will look like the screenshot below.

Test Payload

The mini HTTP client allows you to send HTTP requests using GET, POST, PUT and DELETE methods. However, the payload textbox will only be enabled if the method is a POST or a PUT.

For our example a GET method will happily submit a query parameter to our Microservice. Hit the GET button and trigger your Microservice!

If the value you have passed as the username is available, you should see log lines similar to what's below.

Unique Parameters

The body says that it's unique and the log line says that the username does not exist. A healthy response with a 200 status code.

Now hit it a second time without changing the value of the parameter and the output should look something similar to what's below.

Non-Unique Parameters

Our first call checked the database for uniqueness and reserved the username. The second call proves that by showing that the username already exists.

Now all you have to do is to integrate this Microservice to your app. Grab the URI path by hitting the COPY URL button shown in the screenshot below and you're done!


Happy hacking!

PubNub Functions provides a rich set of tools, and this documentation does not cover all of the potential situations you may encounter. If you need help with a situation not covered by the documentation, please contact PubNub Support.