Skip to main content
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
PRICEOS_API_KEY=pos_ab234cdef...
npm i priceos
2

Add server side handlers

// app/api/priceos/[...path]/route.ts
import { 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.
3

Add provider component

// layout.tsx or wherever your providers are
import { PriceOSProvider } from "priceos/react";

<PriceOSProvider>
  {/* children */}
</PriceOSProvider>;
For full Provider configuration details, see PriceOSProvider.
4

Generate types (optional, but highly recommended)

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

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.
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.
For more info see Tracking usage.