API monetization challenges: charge once
Tags: api, monetization, challenges, solution
Published: 2023-06-22 21:28:46

## Problem
In most cases, API pricing models imply that each call to a paid endpoint is billed. However, there are scenarios when you’d like to charge your API consumer only for the first call.

One example could be an endpoint that performs computations upon first call, caches the results, and returns them from cache in response to subsequent calls.

Another example could be an API with two endpoints: one to initiate calculations, the other to retrieve the results, and the API provider would like to charge the customer only when the results are retrieved for the first time.

## Solution
Let’s see how a new [Project X](https://www.projectx.biz/) feature can help implement these pricing models with ease.

Say, there is a ChatGPT-like API that caches responses from the LLM and returns them from cache for subsequent calls with the same prompt.

`GET /prompt?p=<prompt text>`

The API provider would like to charge the customer for the number of tokens (words) in the prompt, but only once for each unique prompt.

Project X quotas allow for flexible configuration of when and how a specific API call consumes the quota volume.

Let’s start with a quota imposed on the number of tokens: 10k per month.

![](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*Cpsv58RDZX_MRPHuZ6NE6g.png)

The two fields on the screenshot below define, how and in what cases each call to GET /prompt uses the quota volume.

![Quota consumption is flexibly configurable](https://miro.medium.com/v2/resize:fit:4800/format:webp/1*B7bJZIevv6S4ZSxRcSGT_Q.png)

By default, each call consumes one quota unit. In our case, it should be as many quota units as words in the prompt. A simple JavaScript snippet will do the job:

`request.query.p.match(/\b(\w+)\b/g).length`

What happens here? We take the request query string parameter “p”, call JS string function “match()” on it, which returns an array of words. And then the “.length” property gives the number of elements in the array, i.e. the number of words in the prompt.

![Configuring each call to consume as many quota units as words in the prompt](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*H8KbiXcEc1OxoTXVkn3eeQ.png)

Configured this way, for each call, Project X engine will parse the query string parameter with the prompt, find out how many words are there, and count the number as the actual usage.

But there is one detail: Project X will account for usage each time an endpoint call is made.

To change that and bill only once per prompt, we need to leverage the second config field and a new function called “once()”.

> Recently released, the new function “once(value)” returns “true” only once for each specific “value”.
> 
> It is subscription-scoped.

Quota usage condition is a Javascript snippet evaluated to boolean. If it’s true for the current call — the quota usage is recorded. By default, the field is empty, meaning, each call uses the quota.

![](https://miro.medium.com/v2/resize:fit:1020/format:webp/1*3w8fG58h8mrtoa6BXSeuGw.png)

`once(request.query.p)` will return true only the first time it sees a specific prompt, which will make the engine account for usage only for the first call with that prompt.

It’s that easy. Register on [https://app.projectx.biz/](https://app.projectx.biz/) and try it out.