Exposing HTTP functions with Corvid
Tags: webdev, tutorial
Published: 2020-07-30 15:26:13

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](https://dev-to-uploads.s3.amazonaws.com/i/gyt829pchqfkldaymkeo.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/xj8zambfkyl1fehje1sf.png)

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](https://www.wix.com/corvid/reference/wix-http-functions#top).

```javascript
import {ok} from 'wix-http-functions';
```

While we're importing, let's also grab the [Wix Data API](https://www.wix.com/corvid/reference/wix-data#top) so we can query our Data Collections (aka our database). 

```javascript
import wixData from 'wix-data';
```

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:

```javascript
export function get_totalPledges() {
}
```

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

```javascript
let options = {
		"headers": {
			"Content-Type": "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. 

```javascript
wixData.aggregate("TableName")
	.sum("fieldName")
	.run()
```

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

```javascript
.then((results) => {
			if (results.items.length > 0) {
				options.body = {
					"total": results.items
				};
			}
		});
```

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

```javascript
return ok(options);
```

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
```

```
https://meredith.wixsite.com/demo/_functions/totalPledges
```

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gyt829pchqfkldaymkeo.png)

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!