DEV Community

Meredith Hassett
Meredith Hassett

Posted on

Exposing HTTP functions with Corvid

Creating and exposing HTTP functions from your site doesn't have to be a long and tedious process. Sometimes you may need to share access to your data or functionality from your site. Let's take a look at an easy (and FREE!) way to expose some site data!

Alt Text

Using Corvid, we can easily expose an HTTP function version of our site data or site functionality. I am working on building a Kickstarter style site, so I am going to work on building a function that GETs the current total value of pledges on the product. The site only has one product, so this makes it easier :)

The first thing we need to do is create a new file on the backend of our application called https-functions.js. This is where we'll create and our expose our site's data.

Alt Text

We'll need the Wix HTTP Functions API as it will help us do the hefty lifting of our HTTP functions. Go ahead and import any methods you'll need from the wix-http-functions API at the top of your http-functions.js file. I'll be focusing on the happy path and only using the OK method. If you need other methods or are looking for error handling, check out the Wix HTTP Functions API Docs.

import {ok} from 'wix-http-functions';
Enter fullscreen mode Exit fullscreen mode

While we're importing, let's also grab the Wix Data API so we can query our Data Collections (aka our database).

import wixData from 'wix-data';
Enter fullscreen mode Exit fullscreen mode

When defining our function name for our HTTP function, we have to be careful about our naming our function. The function name starts out with the verb we are looking to associate with the function. So in this case, the function name will start with get. The next part of the function name is what it will do and how it will be called, so be sure to make it descriptive. Since we're looking at getting the total pledge amount, I'm going to name it totalPledges. That looks like this:

export function get_totalPledges() {
}
Enter fullscreen mode Exit fullscreen mode

Don't forget it needs to be exported so it's available outside of this file!

We need to set up a basic Options object to include the data we need to pass back via the HTTP function return. The only property we need to setup now in the JSON object is the header's content-type and set it to "application/json".

let options = {
        "headers": {
            "Content-Type": "application/json"
        }
    };
Enter fullscreen mode Exit fullscreen mode

Now we need to retrieve the data to share when our new function is called. We can do this using Wix Data. Wix Data allows us to aggregate on a field name (aka column) from our database. We'll need to create an aggregate on Table Name and then run the sum method on the field we want to aggregate. We'll then run this aggregate query. This is a promised method, so we'll handle the Promise return next.

wixData.aggregate("TableName")
    .sum("fieldName")
    .run()
Enter fullscreen mode Exit fullscreen mode

On the return of this promise, let's check to make sure there is data in the result object and then return the result of the query as the content body for the HTTP function result which will live in the options object as the body property. The query result can be found using results.item.

.then((results) => {
            if (results.items.length > 0) {
                options.body = {
                    "total": results.items
                };
            }
        });
Enter fullscreen mode Exit fullscreen mode

The last think to do is return the OK method with the Options data object we've constructed. Inside the if() statement, add the return of OK.

return ok(options);
Enter fullscreen mode Exit fullscreen mode

To test out if our new HTTP function works, make sure to SAVE AND PUBLISH and then we can use a tool like Postman to call the API. In order to access your function, you'll need to construct the URL for the REST API call. It would be your Wix Site name, the site's name, _functions, and then the function name without the verb.

https://mysite.wixsite.com/sitename/_functions/functionName
Enter fullscreen mode Exit fullscreen mode
https://meredith.wixsite.com/demo/_functions/totalPledges
Enter fullscreen mode Exit fullscreen mode

Make sure to set the proper verb in a tool like Postman or your cUrl request.

Alt Text

And it's that easy it is to create and expose HTTP function!

Keep checking back for more tips and tricks on working with Corvid!

Top comments (0)