You can also check for permissions in your routes. Let’s say you have a route that requires a specific read access for profile data.
You want to check if the API key has the required scope before returning the data. You can pass that scope to the middleware.
Let’s modify the above route to check for billing read access.
You can add usage to your API key by passing the usage parameter to the middleware. This will allow you to track how much of your API is being used.
Usage object takes in value and id (optional). If id is not passed, the usage will be tracked per customer based on the customerId in the API key.
In the example below, we are passing in customer_123----fine_tune as the id. In this case we have built a custom meter for tracking the usage of fine-tuning a model for a customer by creating a hash of the customerId and the meter name.
You can retrieve the usage of your API key by using Oneloop typescript SDK. Here’s an example of how you can retrieve the usage of an API key.
This will return the usage for the meterId that was passed in on a daily basis.
const oneloopClient = new OneloopClient({ token: process.env.ONELOOP_KEY ?? "",});const meter = await oneloopClient.getBillingUsage("customer_123----fine_tune");res.send("Meter: " + meter + " Value: " + JSON.stringify(meter));