@agentaos/better-auth plugin connects AgentaOS to those users. It adds three endpoints to your auth server:
Before you start
- In the dashboard, create the products your users can buy. Copy each product’s link ID from the product page.
- Open Developers → API keys and create a key. A key that starts with
sk_test_works in test mode. - Open Settings → Developers → Webhooks and set the URL to
https://your-domain.com/api/auth/agentaos/webhook. Click Reveal signing secret and copy the secret.
Install
Add the plugin to your auth server
auth.ts
products, by its slug. The product, the price and the return URLs come from your server, not from the browser.
Start a checkout from the browser
auth-client.ts
4242 4242 4242 4242, any future date and any CVC.
Handle events
You can set one function per event:onCheckoutCompleted, onSubscriptionCreated, onSubscriptionRenewed, onSubscriptionUpdated, onSubscriptionPaymentFailed, onSubscriptionCanceled, onDisputeCreated and onDisputeClosed. Each receives { event, userId }. onEvent receives every event, after the function for that event. If a function throws an error, the endpoint answers 500 and AgentaOS sends the event again.
userId is the user who started the checkout. It is set on the first checkout.session.completed of that checkout only. Renewals and upgrade charges also arrive as checkout.session.completed, with userId empty. In your functions:
- Use
event.data.metadata.productto know what was bought. Oncheckout.session.completed,event.data.linkIdis the payment link’s public token, not the link ID inproducts. - Save the link from
event.data.metadata.subscriptionIdto your user from the first event, if you need it for later events. - Read metadata keys in camelCase. A key you send as
user_idarrives asuserId. - Ignore test events in live mode: check
event.data.livemode. - Give access only once per checkout, for example by saving
event.data.sessionId. An event can arrive more than once.