# Guide

This guide builds on the [SDK quickstart](https://docs.hypertune.com/getting-started/set-up-hypertune) 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](https://docs.hypertune.com/getting-started/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.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FJ8sIhcJil8ii1tmJEJXY%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(10).png?alt=media&#x26;token=460aa436-9a29-45bd-b62f-41dec7a922bf" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FSiA1y9TtdsXyC6YidWjm%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(13).png?alt=media&#x26;token=726e2097-6894-4cbd-959a-cf70ace3b2cd" alt=""><figcaption></figcaption></figure>

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**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2Fb4XbxhElq9RX9hPmyTba%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(11).png?alt=media&#x26;token=07ea447b-4d79-48b0-a0ee-a553ea4a2727" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FvfYc8VUUdKkJ2mKfPCb9%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(12).png?alt=media&#x26;token=ad614e3e-1739-4302-bc82-7d88ab96838f" alt=""><figcaption></figcaption></figure>

```graphql
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**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2F0lRopPzw3vPiRT9cELbw%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(2).png?alt=media&#x26;token=3103c37d-54ec-4ac4-b5fe-59ff7b55d2e8" alt=""><figcaption></figcaption></figure>

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**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FlemQj9xXQzdHNz647uFP%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(3).png?alt=media&#x26;token=7c84d280-0f5a-40f9-966c-05d8aa34e779" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2F8n6qQu8gC1V4UeyZZPA7%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(4).png?alt=media&#x26;token=baac0299-3f1d-4a9a-ae4e-67749e42b9db" alt=""><figcaption></figcaption></figure>

```graphql
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**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FqNVcJ8L8YxEWwtFAxXxc%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(6).png?alt=media&#x26;token=c2fe0024-c210-4353-9bc6-a8fa31825767" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FH5D1dHAeF2uh1mPhDQXV%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(7).png?alt=media&#x26;token=a97024de-d797-4b3f-b5c5-eb208dc5f872" alt=""><figcaption></figcaption></figure>

Enter the initial values, then click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FFcuKCkAnnWz1goHpFsMV%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(8).png?alt=media&#x26;token=7829495c-fb9b-42fd-8399-752917242af9" alt=""><figcaption></figcaption></figure>

## Access flags to get entitlements

Regenerate the client:

{% tabs %}
{% tab title="npm" %}

```bash
npx hypertune
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn hypertune
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm hypertune
```

{% endtab %}
{% endtabs %}

Then use the generated methods to access your flags:

{% code title="app/api/add-team-member/route.ts" %}

```typescript
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,
  })
}

```

{% endcode %}

## 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.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FMoIyZgdQkR0i4ZCZ8gyi%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%20(9).png?alt=media&#x26;token=f4040be2-dbc7-44d5-9359-0a4557976cd9" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FyVBVO9lDv7DKZycztyuK%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253Eentitlements%253EmaxTeamSize%26selected_schema_type%3D%257B%2522type%2522%253A%2522enum%2522%252C%2522name%2522%253A%2522Plan%2522%252C%2522selectedChildName%2522%253Anull%252C%2522count%2522%253A1%257D%26sche.png?alt=media&#x26;token=3c06069c-a474-47ee-813c-3d078953dc86" alt=""><figcaption></figcaption></figure>

## Next steps

* Use Hypertune to manage your [pricing](https://docs.hypertune.com/pricing) too, and centralize all the configuration logic for your pricing and entitlements in a single system.
