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!

GET request in Postman

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.

file structure

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.

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

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:

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".

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.

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.

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.

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.

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

Making GET request in PostMan

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!

Source link

Write A Comment