---
title: "useCredits"
description: "useCredits, auto-imported in the app: its options, return value and types."
canonical_url: "https://nuxt-backend.dev/docs/reference/api/use-credits"
---
# useCredits

Version: nuxt-backend 0.6.x · Tab: Reference · Kind: reference · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

## Interfaces

### CreditCycle

Defined in: [src/runtime/vue/composables/use-credits.ts](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts)

The billing period a meter's credited units belong to.

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="start">
        
      </a>
      
       <code>
        start?
      </code>
    </td>
    
    <td>
      <code>
        Date
      </code>
    </td>
    
    <td>
      Start of the current cycle, when the provider reports one.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="end">
        
      </a>
      
       <code>
        end?
      </code>
    </td>
    
    <td>
      <code>
        Date
      </code>
    </td>
    
    <td>
      End of the current cycle — when unspent credits expire, if they do.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### UseCreditsOptions

Defined in: [src/runtime/vue/composables/use-credits.ts](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts)

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="meter">
        
      </a>
      
       <code>
        meter?
      </code>
    </td>
    
    <td>
      <code>
        MaybeRefOrGetter
      </code>
      
      <<code>
        string
      </code>
      
       | <code>
        null
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Which meter to read: a configured meter name (<code>
        'credits'
      </code>
      
      ) or a raw meter id, as a value, ref or getter. Omitted or blank, the entity's first configured meter.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="api">
        
      </a>
      
       <code>
        api?
      </code>
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/api/use-billing#billingapi">
        <code>
          BillingApi
        </code>
      </a>
    </td>
    
    <td>
      Override the injected <code>
        api.billing
      </code>
      
       namespace.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

---

### UseCreditsReturn

Defined in: [src/runtime/vue/composables/use-credits.ts](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts)

#### Properties

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Defined in
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <a id="balance">
        
      </a>
      
       <code>
        balance
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        number
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Remaining prepaid credit balance for the meter: <code>
        undefined
      </code>
      
       while loading, and when the read failed (see <code>
        error
      </code>
      
      ) or the backend exports no <code>
        getCredits
      </code>
      
      ; <code>
        0
      </code>
      
       while signed out or before the billing entity has any.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="credited">
        
      </a>
      
       <code>
        credited
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        number
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Total credits granted for the meter (top-ups + plan grants), or <code>
        undefined
      </code>
      
       while loading.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="consumed">
        
      </a>
      
       <code>
        consumed
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        number
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Credits consumed for the meter, or <code>
        undefined
      </code>
      
       while loading.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="overage">
        
      </a>
      
       <code>
        overage
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        number
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Units consumed beyond what was credited — what a pay-as-you-go meter has run up this cycle and the provider will invoice. <code>
        0
      </code>
      
       for a meter that is still in prepaid credit; <code>
        undefined
      </code>
      
       while loading.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="cycle">
        
      </a>
      
       <code>
        cycle
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<a href="#creditcycle">
        <code>
          CreditCycle
        </code>
      </a>
      
       | <code>
        null
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      The billing cycle these credits belong to: <code>
        undefined
      </code>
      
       while loading, <code>
        null
      </code>
      
       for a meter with no cycle at all (credits bought as a one-time pack), otherwise the current period.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="expiresatcycleend">
        
      </a>
      
       <code>
        expiresAtCycleEnd
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        boolean
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      Whether the remaining balance expires at <a href="#end">
        CreditCycle.end
      </a>
      
       — <code>
        true
      </code>
      
       only when every benefit crediting the meter is known not to roll over and a cycle end is known. <code>
        undefined
      </code>
      
       while loading; <code>
        false
      </code>
      
       when any of the credits carry over (a pack bought on top of a plan's allowance), or the provider has not said.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="meterid">
        
      </a>
      
       <code>
        meterId
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        string
      </code>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      The resolved meter id (the one read above) — pass it to your server-side spend.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="unit">
        
      </a>
      
       <code>
        unit
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      < | <a href="https://nuxt-backend.dev/docs/reference/api/billing#meterunit">
        <code>
          MeterUnit
        </code>
      </a>
      
       | <code>
        undefined
      </code>
      
      >
    </td>
    
    <td>
      What the meter counts, as the catalog's <code>
        meters.<key>.unit
      </code>
      
       names it; <code>
        undefined
      </code>
      
       without one, or while loading (the packaged labels then say "credits").
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="isloading">
        
      </a>
      
       <code>
        isLoading
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        boolean
      </code>
      
      >
    </td>
    
    <td>
      <code>
        true
      </code>
      
       until credit balances have loaded (<code>
        false
      </code>
      
       once the read failed).
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="ispending">
        
      </a>
      
       <code>
        isPending
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      <<code>
        boolean
      </code>
      
      >
    </td>
    
    <td>
      <code>
        true
      </code>
      
       while <code>
        topUp
      </code>
      
       or <code>
        refresh
      </code>
      
       runs.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="error">
        
      </a>
      
       <code>
        error
      </code>
    </td>
    
    <td>
      <code>
        ComputedRef
      </code>
      
      < | <a href="https://nuxt-backend.dev/docs/reference/api/functions#backenderror">
        <code>
          BackendError
        </code>
      </a>
      
       | <code>
        null
      </code>
      
      >
    </td>
    
    <td>
      Why the last action failed, else why the balances could not be read, else <code>
        null
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="topup">
        
      </a>
      
       <code>
        topUp
      </code>
    </td>
    
    <td>
      (<code>
        productIds
      </code>
      
      , <code>
        options?
      </code>
      
      ) => <code>
        Promise
      </code>
      
      <<code>
        string
      </code>
      
      >
    </td>
    
    <td>
      Buy a credit pack (a one-time product) via checkout — returns the URL. Rejects on failure, which <code>
        error
      </code>
      
       also holds.
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <a id="refresh">
        
      </a>
      
       <code>
        refresh
      </code>
    </td>
    
    <td>
      () => <code>
        Promise
      </code>
      
      <<code>
        void
      </code>
      
      >
    </td>
    
    <td>
      Refresh the cached balance from the provider (e.g. right after a top-up completes). Rejects on failure, and when the backend does not export <code>
        syncEntitlements
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts" rel="nofollow">
        src/runtime/vue/composables/use-credits.ts
      </a>
    </td>
  </tr>
</tbody>
</table>

## Functions

### useCredits()

```ts
function useCredits(options?): UseCreditsReturn;
```

Defined in: [src/runtime/vue/composables/use-credits.ts](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-credits.ts)

Reactive prepaid-credit balance for the current user, plus a `topUp()` checkout
and a `refresh()` re-sync. Credits are the provider's native model: a credit pack is a
one-time product whose Credits benefit tops up a meter balance, drawn down by
server-side consumption (`setupBilling().spendCredits`). Reads the component's
webhook-synced cache via `getCredits`. Zero-arg via the auto-provided
`api.billing` namespace; pass `{ api }` to override.

Alongside the balance it exposes what a credits UI actually needs to say:
the current `cycle`, whether the remainder `expiresAtCycleEnd`, and the
`overage` a pay-as-you-go meter has run up. The balance read never throws
(a failed read shows in `error`); the actions reject on failure and
mirror it into `error`.

#### Parameters

<table>
<thead>
  <tr>
    <th>
      Parameter
    </th>
    
    <th>
      Type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        options?
      </code>
    </td>
    
    <td>
      <a href="#usecreditsoptions">
        <code>
          UseCreditsOptions
        </code>
      </a>
    </td>
  </tr>
</tbody>
</table>

#### Returns

[`UseCreditsReturn`](#usecreditsreturn)

#### Example

```vue
<script setup lang="ts">
const { balance, topUp, isPending, error } = useCredits({ meter: 'credits' })
</script>
<template>
  <p>{{ balance ?? '—' }} credits</p>
  <p v-if="error">{{ error.message }}</p>
  <button :aria-disabled="isPending || undefined" @click="isPending || topUp(creditPackId)">Buy 100 credits</button>
</template>
```


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
