Guide

This guide builds on the SDK quickstart and shows you how to model and manage your entitlements using Hypertune.

You'll learn how to:

  • Create a custom input type to model the user's organization

  • Create custom object types to model your entitlements

  • Create flags that use those custom object types

  • Access those flags to get entitlements in your code

  • Update entitlements

Prerequisites

Set up Hypertune

Create a custom input type to model the user's organization

Go to the Schema view in the dashboard and select the Context input type. Click the + Add button to add a new field, enter a name, and select New input type from the Type dropdown.

Enter a name for the new input type and click Create.

By default the new input type has no fields.

Click + Add to add a new field. Enter a name, set its type, and click Create.

Repeat for each field you want to add. You can switch to the code view to make this easier. Then click Save.

input Organization {
  id: String!
  plan: Plan!
}

enum Plan { free, pro, enterprise }

Create custom object types to model your entitlements

Go to the Schema view in the dashboard. Click the + button in the top-right of the sidebar. Select Object, enter a name, and click Create.

By default, the new object type has no fields.

Click + Add to add a new field. Enter a name, set its type, and click Create.

Repeat for each field you want to add. You can switch to the code view to make this easier. Then click Save.

type Entitlements {
  teamRolesEnabled: Boolean!
  maxTeamSize: Int!
  maxApiRequestsPerDay: Int!
  versionHistoryEnabled: Boolean!
}

Create flags for your entitlements

Go to the Flags view in the dashboard. Click the + button in the top-right of the sidebar and select Flag.

Enter a name, set its type to the one you created earlier, and click Create.

Enter the initial values, then click Save.

Access flags to get entitlements

Regenerate the client:

npx hypertune

Then use the generated methods to access your flags:

app/api/add-team-member/route.ts
import { waitUntil } from '@vercel/functions'
import { NextResponse } from 'next/server'
import addUserToTeam from '@/lib/addUserToTeam'
import getHypertune from '@/lib/getHypertune'
import getTeamSize from '@/lib/getTeamSize'

export const runtime = 'edge'

export async function POST(request: Request) {
  const hypertune = await getHypertune({ isRouteHandler: true })

  const { userId, teamId } = await request.json()

  const teamSize = getTeamSize()

  const maxTeamSize = hypertune.entitlements().maxTeamSize({
    args: {},
    fallback: 5,
  })

  waitUntil(hypertune.flushLogs())

  if (teamSize >= maxTeamSize) {
    return NextResponse.json(
      {
        error: 'Team size limit reached',
        teamSize,
        maxTeamSize,
        message: `Your team has reached the maximum size of ${maxTeamSize} members. Please upgrade your plan to add more members.`,
      },
      { status: 403 }
    )
  }

  const result = await addUserToTeam(userId, teamId)

  return NextResponse.json({
    success: true,
    currentTeamSize: teamSize + 1,
    maxTeamSize,
    result,
  })
}

Update entitlements

Go to the Flags view in the dashboard, and select the nested flag for the entitlements you want to update from the left sidebar.

Make your changes, then open the Diff view to review them. Click Save.

Next steps

  • Use Hypertune to manage your pricing too, and centralize all the configuration logic for your pricing and entitlements in a single system.

Last updated