# Usage meter

> This period's usage per meter against the plan's quota, with the reset date.

- Block: [usage](https://bettersupabase.com/blocks/usage.md)
- Kind: component
- Hook: `useUsageMeter` from `@/hooks/use-usage`
- Page: https://bettersupabase.com/recipes/usage-meter

## Install the block

```bash
npx better-supabase add usage
```

## Web (Next.js)

Add the shadcn components:

```bash
npx shadcn add card progress skeleton
```

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-usage
```

From `apps/examples/nextjs/src/features/dashboard/components/usage-card.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/nextjs/src/features/dashboard/components/usage-card.tsx):

```tsx
"use client";

import { tenantOf } from "better-supabase/react";
import { useExtracted, useFormatter } from "next-intl";

import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";
import { Skeleton } from "@/components/ui/skeleton";
import { useUsageMeter } from "@/hooks/use-usage";
import { useAuth } from "@/lib/hooks";
import { usageConfig } from "@/lib/supabase/blocks/slices";

/** The API schema `gen` wrote for the usage block. */
const API_SCHEMA = usageConfig.api;

/** This period's usage per meter against the plan's quota. */
export function UsageCard() {
  const t = useExtracted("dashboard");
  const format = useFormatter();
  const organizationId = tenantOf(useAuth()) ?? null;

  const { meters } = useUsageMeter({
    organizationId,
    meters: ["api.calls", "customers.created"],
    schema: API_SCHEMA,
  });

  if (!organizationId) {
    return null;
  }

  if (meters === undefined) {
    return <UsageCardSkeleton />;
  }

  const labels = new Map([
    ["api.calls", t("API calls")],
    ["customers.created", t("Customers added")],
  ]);

  return (
    <Card data-testid="usage">
      <CardHeader>
        <CardTitle>{t("Usage this period")}</CardTitle>
        <CardDescription>
          {t("Counted by the usage module, limited by your plan.")}
        </CardDescription>
      </CardHeader>
      <CardContent className="space-y-5">
        {meters.map((meter) => (
          <div key={meter.meter} className="space-y-2">
            <div className="flex items-baseline justify-between gap-2 text-sm">
              <span className="font-medium">
                {labels.get(meter.meter) ?? meter.meter}
              </span>
              <span
                className={
                  meter.exceeded
                    ? "text-destructive tabular-nums"
                    : "text-muted-foreground tabular-nums"
                }
              >
                {meter.limit === undefined
                  ? t("{used} used, no limit", {
                      used: format.number(meter.used),
                    })
                  : t("{used} of {limit}", {
                      used: format.number(meter.used),
                      limit: format.number(meter.limit),
                    })}
              </span>
            </div>
            <Progress value={(meter.fraction ?? 0) * 100} />
            <p className="text-xs text-muted-foreground">
              {t("Resets {date}", {
                date: format.dateTime(
                  new Date(meter.resetsAt.epochMilliseconds),
                  { dateStyle: "medium" },
                ),
              })}
            </p>
          </div>
        ))}
      </CardContent>
    </Card>
  );
}

export function UsageCardSkeleton() {
  return <Skeleton className="h-64 rounded-xl" aria-busy="true" />;
}
```

## Native (Expo)

The snippet uses `ActivityIndicator`, `Text`, `View` from `react-native`, so it needs no styling library.

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-usage
```

From `apps/examples/expo-powersync/src/components/usage-meter.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/expo-powersync/src/components/usage-meter.tsx):

```tsx
import { tenantOf, useAuth } from "better-supabase/react";
import { ActivityIndicator, Text, View } from "react-native";

import { useUsageMeter } from "../hooks/use-usage";
import { API_SCHEMA } from "../lib/blocks";

const labels = new Map([
  ["api.calls", "API calls"],
  ["customers.created", "Customers added"],
]);

/** This period's usage per meter against the plan's quota. */
export function UsageMeter() {
  const organizationId = tenantOf(useAuth()) ?? null;

  const { meters } = useUsageMeter({
    organizationId,
    meters: ["api.calls", "customers.created"],
    schema: API_SCHEMA,
  });

  if (!organizationId) {
    return null;
  }

  if (meters === undefined) {
    return <ActivityIndicator />;
  }

  return (
    <View style={{ gap: 12, padding: 16, borderWidth: 1, borderRadius: 12 }}>
      <Text style={{ fontSize: 17, fontWeight: "600" }}>Usage this period</Text>
      {meters.map((meter) => (
        <View key={meter.meter} style={{ gap: 4 }}>
          <View style={{ flexDirection: "row" }}>
            <Text style={{ flex: 1 }}>
              {labels.get(meter.meter) ?? meter.meter}
            </Text>
            <Text style={{ color: meter.exceeded ? "#b91c1c" : "#737373" }}>
              {meter.limit === undefined
                ? `${meter.used} used, no limit`
                : `${meter.used} of ${meter.limit}`}
            </Text>
          </View>
          <View
            style={{ height: 6, borderRadius: 3, backgroundColor: "#e5e5e5" }}
          >
            <View
              style={{
                height: 6,
                borderRadius: 3,
                backgroundColor: meter.exceeded ? "#b91c1c" : "#111",
                width: `${(meter.fraction ?? 0) * 100}%`,
              }}
            />
          </View>
          <Text style={{ color: "#737373", fontSize: 12 }}>
            Resets{" "}
            {meter.resetsAt.toLocaleString(undefined, { dateStyle: "medium" })}
          </Text>
        </View>
      ))}
    </View>
  );
}
```

## Prompt

Paste one of these into a coding agent to build the recipe in your app.

### Web

```text
Build "Usage meter" in a React web app that uses shadcn/ui components. This period's usage per meter against the plan's quota, with the reset date.

It uses the better-supabase `usage` block (Usage metering and quotas). It calls the hook `useUsageMeter` from `@/hooks/use-usage`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add usage` and apply the migration.
2. Add the shadcn components the recipe uses: `npx shadcn add card progress skeleton`.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-usage`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useUsageMeter` from `@/hooks/use-usage`, starting from the recipe's snippet (`apps/examples/nextjs/src/features/dashboard/components/usage-card.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/usage-meter.md
- Block docs: https://bettersupabase.com/docs/blocks/usage
```

### Native

```text
Build "Usage meter" in a React Native app built with Expo. This period's usage per meter against the plan's quota, with the reset date.

It uses the better-supabase `usage` block (Usage metering and quotas). It calls the hook `useUsageMeter` from `@/hooks/use-usage`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add usage` and apply the migration.
2. The recipe uses `ActivityIndicator`, `Text`, `View` from `react-native`, so it needs no extra UI library. Use the app's own components if it has them.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-usage`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useUsageMeter` from `@/hooks/use-usage`, starting from the recipe's snippet (`apps/examples/expo-powersync/src/components/usage-meter.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/usage-meter.md
- Block docs: https://bettersupabase.com/docs/blocks/usage
```
