Sebastian Scholl (00:04)
Hey there, Sebastian here at 8base. Today, I want to walk you through a way of creating a custom billing alert for if your workspaces or one of your workspaces hits overages. Lately, we've actually had a number of customers that have scaled up their applications like crazy.
Sebastian Scholl (00:20)
They've had really great growth for their products that they're building. In doing so they've started to ask, "Well, how can we make sure that we are getting notifications when we hit certain overages or pass certain thresholds?" We are building this as a native feature into 8base.
Sebastian Scholl (00:36)
However, in the interim, I wanted to show you a way that you could implement your own solution, and by doing so, introduce you to the system part of your API, which is a really great thing to know about. Let's dive into it.
Sebastian Scholl (00:48)
All right. Here I am in my IDE and I'm just going to go ahead and create a new functions directory. So 8base init and I'm going to call this overages-alert-fn, even though you just generate a new function if you were doing this in an existing project.
Sebastian Scholl (01:04)
Sebastian Scholl (01:21)
Now let me go ahead and add this to my, open folder, add this to my project. So overages-alert-fn. Don't save. Now I am there. Go to my terminal and we are good to go. Awesome.
Sebastian Scholl (01:41)
Sebastian Scholl (02:11)
If you wanted this to run more frequently, you could. If you wanted to run on a crazy conundrum and do it once a week or whatever it might be, you can do that as well. But we'll just stick in basics and have that at a rate of once a day here.
Sebastian Scholl (02:24)
Now we have our task function. We know the cadence on which that function is going to invoke and we have our handler here, which is where we're going to do the code.
Sebastian Scholl (02:34)
One, I'm just going to change this to say ''Task completed'' because we're not returning anything from a task function. Now what we want to do is go ahead and jump into our workspace to just work inside the API Explorer, show you a little bit of the system API, and construct the query, which is going to give us the information we need to perform this task effectively.
Sebastian Scholl (02:57)
Here I am in my API Explorer and I just copy and pasted the query over so I didn't have to write in front of you. However, we are going to explore where we are getting this information from right now. What's really cool about an 8base workspace is that this whole interface we call the console is just an interface to the API which is your workspace API.
Sebastian Scholl (03:19)
Anything, absolutely anything that you can do in the console, you can do programmatically via the API, and that's even things like getting information about your billing, your workspace plan, all that type of stuff. If you open a query, and I'm just going to copy all that. If you open a query and you go into your system, you can then see all of the system-level information or queries, mutations, all that stuff is all there for you.
Sebastian Scholl (03:50)
Even things like if you wanted to build a workflow where... If you wanted to run a mutation that created a table in your data model from a user application, you can totally do that, which is super cool. Inside of here, I'm going to paste back now in the billingMetricsUsagesList query that we're running.
Sebastian Scholl (04:10)
Here what we can see is that we are running a query in our system, gets back the billing metrics usages. For each metric or limit that we have, we want the name of it and the unit in which it's measured. We then want the value of its current value and then we want to know if there's any overage and the warning of that overage.
Sebastian Scholl (04:33)
If I run that query, I don't think I can't have overages because this is an 8base-owned workspace. But we get all that information back right here so you can see API requests, all that type of stuff. Let me hide that. Now what I'm going to do is I'm going to take this query and bring it back into my task function.
Sebastian Scholl (05:00)
Let me go here, and I'm going to create a constant called query, put it in a string, drop it in there. Now we have our query that will actually give us back the information about our current usage and we know that it's going to run once a day.
Sebastian Scholl (05:22)
Now that we have that inside this function, I'm actually going to run that query. I'm going to say await the response from the context api.gqlRequest where I pass it my query. I then pass an empty object because we're not giving it any variables here. Then we're going to say checkPermissions: false.
Sebastian Scholl (05:47)
We're doing that because we know that no one is invoking this function and so being run in a safe environment on the server side. We don't really care about our roles and permissions engine, we are just bypassing it. From that we get the response.
Sebastian Scholl (06:09)
Now, we are also getting the member email from this query. I forgot to mention that. That's the owner of the workspace, and we would want that information if we were to want to email them that something's happening. So play with that. I'm going to go ahead and unpack this a little bit, the response, because it is pretty big.
Sebastian Scholl (06:34)
Inside the... We're getting the system back, and then in there we got two items, which is this one, and then member account. We're going to do that and then we're also going to do member account. From the member account, we know we get the email. From the billing metrics, we know that we get the items. We're just going to call those usageMetrics.
Sebastian Scholl (07:11)
Cool. Just unpack the response there. All right. Now with those, we want to go through each one and see whether or not an overage is being reported. If I jump back here and I look at that response that we're getting, we can see that I don't have any overages, but if I did, the value would be reflected within that specific metric. We're going to go look through if any overages occurred.
Sebastian Scholl (07:34)
However, if you wanted to, you could select or potentially monitor a single one that would be important to you. How we're going to do that is we're going to say that there's a new constant which is called overages, which equals the usageMetrics, and we are going to filter those. For each, call it metric. We are going to filter down the ones where there is the metric.overage.value. That would work, overage, value.
Sebastian Scholl (08:19)
We're going to say if it's greater than zero, I think that would make sense. If the overage is reported, it will have a value greater than zero. Then we have our overages.
Sebastian Scholl (08:35)
Then what we can do in there is we can say, okay, well, if overages.length, this filter is going to return an array. If the array length is greater than zero, then we know we have overages.
Sebastian Scholl (08:48)
Now, inside here, I'm just going to say console.log ('You have overages!' Then pass in the overages as the argument. However, what's important to remember is that in here, if you wanted to, this is where you could go ahead and say, "Well, I want to be emailed that overage. I want my system to send me a text message." However, you want to be alerted. You can go ahead and implement that yourself in here.
Sebastian Scholl (09:22)
Let's just go ahead and run this to make sure that we don't have any errors coming back. Task completed. No overages. Let's just change this to say if it equals zero. We know exactly what this would do. I don't even know why I'm experimenting with that, but we can see that you have overages and it would pass them in.
Sebastian Scholl (09:51)
Really the important part of this tutorial is showing that you can leverage the system API to get this information out of your 8base workspace programmatically and then do whatever you want with it. If you really want to see me take the next steps on saying, okay, well, we're going to send an email, send a text message, we would use a provider like Twilio or SendGrid to do that.
Sebastian Scholl (10:14)
Let me know in the comments below if you want to see that happen and I can do that in a subsequent video. However this is really it. I hope you found this video helpful and as a good introduction to the system API. Like I said during the tutorial, everything that is your workspace is programmatically accessible via the API. While this is a very simple straightforward use case that could potentially unlock that alert capability, you can do so much more with that.
Sebastian Scholl (10:47)
Everything from programmatically generating workspaces with base templates really is supposed to be that type of dynamic tool for you to dream up your own solutions of the things that you're building. With that said, thank you for watching the video, subscribe if you want to get alerted for future updates, and looking forward to seeing you in subsequent videos. Take it easy.