Force free users to subscribe after x API calls

Hi there, Need some help. I have a free try out for visitors on the site. No log in needed. But once they have used up 3x API calls, they cannot longer use the function and are forced to subscribe to a plan. (SignUp-> Stripe gateway → back to the application)
How can I set this up in the best way ? I see little to none documentation about this.
Thanks

You can set up a counter state cookie on the app that tracks the usage…some APIs also return a usage count from which you can poll data from. If you want you can also track using the user object in Bubble but that may be lost once the user closes the browser and opens the application again so a cookie option might be best. Do you know how to set cookies?

1 Like

Thanks for your reply. No I do not

Requires some custom js…you could ask ChatGPT

1 Like

you don’t need custom js to use cookies, the data saved to the ‘Current User’ applies to logged in users and logged out users in cookies.

create a new field on the user type called APItrials and set it to 0 default

just log each API use as [current user’s APItrials = current user’s APItrials + 1] and once this is greater than or equal to 3 lock them out and redirect to subscription page

will work seamlessly for both your logged in and logged out users

This could work, however they could close the browser and come back after the session expires and use it again…

Not fool proof for a business case. Might have malicious users bleeding him that way. Best option would be to set cookies via js that persists and tracks every time the user comes back unless they manually clear the cookies.

i believe users will be able to shake off a Current user cookie as easy as a cookie set by js. the only way to strictly enforce the amount of usage per free user is to have them log in.

even while logged out, the Current user cookie set by default by bubble does last quite some time per my own testing, existing after closing browser, restarting pc, etc

You can set the expiration time of a cookie set by JS.