Skip to main content
If your app signs users in with Better Auth, the @agentaos/better-auth plugin connects AgentaOS to those users. It adds three endpoints to your auth server:

Before you start

  1. In the dashboard, create the products your users can buy. Copy each product’s link ID from the product page.
  2. Open Developers → API keys and create a key. A key that starts with sk_test_ works in test mode.
  3. 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
A user can only buy a product that is in 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
When AgentaOS refuses a checkout, the error keeps its reason and status. In test mode, pay with the card 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.product to know what was bought. On checkout.session.completed, event.data.linkId is the payment link’s public token, not the link ID in products.
  • Save the link from event.data.metadata.subscriptionId to your user from the first event, if you need it for later events.
  • Read metadata keys in camelCase. A key you send as user_id arrives as userId.
  • 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.
The full list of events is in Webhook events. For a Next.js app without Better Auth, use the Next.js route handlers.