Integrate PriceOS on the frontend using our React hooks.
Our React hooks make it a lot easier to evaluate feature access and usage on the frontend.Since PriceOS can only be safely used on the backend, using these hooks still requires server-side setup, but we’ve created helpers to make this really easy for you.
Make sure your API key is only used server side. It should never be used client side.
Be sure to read Linking Stripe Customers to learn how to link Stripe customers to your own interal ID.
1
Install
Create an API key in the PriceOS dashboard and add to your environment variables.
.env
Copy
PRICEOS_API_KEY=pos_ab234cdef...
Copy
npm i priceos
2
Add server side handlers
Next.js
Other backends
Copy
// app/api/priceos/[...path]/route.tsimport { priceosHandler } from "priceos/next";export const { GET, POST } = priceosHandler({ identifyCustomer: async (req: Request) => { // Return a customer identity from your server },});
If you need to include an auth token from the client, you can do so on the Provider.
We are working on adding more helpers for other backends. If there’s a backend you would like us to create helpers for, please reach out and let us know.
Depending on which react hooks you want to use, you’ll need to implement the following routes…
Hook
Endpoint(s) you must add
useCustomer*
GET /priceos/v1/customer
useFeatureAccess
GET /priceos/v1/feature-access
useTrackUsage
POST /priceos/v1/usage
useUsageEvents
GET /priceos/v1/usage-events
useCheckout
POST /priceos/v1/checkout
useCustomerPortal
POST /priceos/v1/customer-portal
* If you use trackUsage from useCustomer, add POST /priceos/v1/usage. If you use checkout from useCustomer, add POST /priceos/v1/checkout.
Make sure you resolve customerId from your server-side auth/session.
If you need to include an auth token from the client, you can do so on the Provider.
// layout.tsx or wherever your providers areimport { PriceOSProvider } from "priceos/react";<PriceOSProvider> {/* children */}</PriceOSProvider>;
For full Provider configuration details, see PriceOSProvider.
4
Generate types (optional, but highly recommended)
Copy
npx priceos generate-types
This will generate a local types file that you can include so all of your features are fully typed.Run with --help to see more configuration options.
Remember to regenerate types any time you create a new feature.
5
Read feature access
Copy
import { useFeatureAccess } from "priceos/react";import type { MyFeatures } from "./priceos.types";export function PrioritySupportButton() { const { hasAccess } = useFeatureAccess<MyFeatures>("priority_support"); const onClick = () => { if (hasAccess){ // Allow access } } return <button onClick={onClick}>Contact support</button>;}
6
Track usage (optional)
Only if you’re using PriceOS to track your customers’ usage for limit features.
Copy
import { useTrackUsage } from "priceos/react";import type { MyFeatures } from "./priceos.types";export function SeatButton() { const { trackUsage } = useTrackUsage<MyFeatures>(); async function handleClick() { // Add teammate first await trackUsage({ featureKey: "team_seats", amount: 1, eventKey: `add_team_seat_${Date.now()}`, }); } return <button onClick={handleClick}>Add teammate</button>;}
If the action happens on your backend (for example in a server action, webhook, or background job), it usually makes more sense to track usage on the backend instead.
useTrackUsage with the built-in Next.js handlers (priceos/next) only accepts positive amount values (so that clients can’t reverse their usage). For negative adjustments (for example when removing a team seat), track usage from your backend via the Node.js SDK or REST API endpoints.