---
title: "Mint discounts from your server"
description: "Mint, list and remove discounts from server code, let customers type codes at checkout, pass a campaign's code, or apply a discount your server chooses. Discounts you plan ahead belong in the catalog."
canonical_url: "https://nuxt-backend.dev/docs/build/growth/offers/discounts"
---
# Mint discounts from your server

> Mint, list and remove discounts from server code, let customers type codes at checkout, pass a campaign's code, or apply a discount your server chooses. Discounts you plan ahead belong in the catalog.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · 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.

- [Try it live in the playground](https://nuxt-backend.dev/playground/pricing)
- [API reference](https://nuxt-backend.dev/docs/reference/api/use-billing)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/runtime/vue/composables/use-billing.ts)

You mint, list and remove the provider's discounts from your own server code with `billing.discounts`, and decide which one a checkout gets. A discount you plan ahead (a launch offer, founder pricing, a win-back offer) belongs in the catalog instead, where `billing sync` keeps it and your code names it by key: see [Run a promotion](https://nuxt-backend.dev/docs/build/growth/offers).

- **Natively:** Polar keeps discounts: a percentage or a fixed amount, a duration, redemption limits, products and dates. It applies a code a customer types at its hosted checkout, or a discount a checkout names by id (checked on 2026-10-25).
- **The package adds:** server-only methods to mint, list and remove them, a guard so only server code names a discount by id, and a campaign link's code applied before the customer sees the checkout.

Minting a discount is privileged: a public function that creates discounts would let anyone make a 100%-off code. So `billing.discounts.create`, `list` and `remove` are methods for your server code, never registered functions, and nothing in the browser can call them.

## Mint a discount

The scaffold ships no discount function: add one of your own as an `internalAction`, so only the command line, the dashboard and your server code reach it.

```ts [backend/discounts.ts]
import { v } from 'convex/values'
import type { DiscountInput } from 'nuxt-backend/billing'
import { internalAction } from './_generated/server'
import { billing } from './billing'

export const createDiscount = internalAction({
  args: { name: v.string(), percent: v.number(), code: v.optional(v.string()) },
  handler: async (ctx, { name, percent, code }) => {
    const basisPoints = Math.round(Math.min(Math.max(percent, 0), 100) * 100)
    const discount: DiscountInput = { type: 'percentage', name, code, duration: 'once', basisPoints }
    return billing.discounts.create(discount)
  },
})
```

Run it from the command line:

```bash
npx convex run discounts:createDiscount '{"name":"Launch","percent":20,"code":"LAUNCH20"}'
```

`billing.discounts.create(discount)` takes the provider's full shape (`DiscountInput`: a percentage or fixed amount, the duration, redemption limits, product restrictions, dates) and resolves with `{ id, code }`. For an admin screen, declare the same action with the `admin.action` builder from `backend/functions.ts` instead of `internalAction` ([Protect backend functions](https://nuxt-backend.dev/docs/build/auth/access/functions)).

## List and remove

`list` reads the provider's discounts a page at a time, and `remove` deletes one:

```ts
const page = await billing.discounts.list({ query: 'LAUNCH', limit: 20 })   // { items, pagination, nextCursor? }
await billing.discounts.remove(discountId)   // nobody redeems it any more; its code is free again
```

The provider's list filters by name in its documentation, and matches part of a code too. To find a discount by its code, list with the code as `query`, compare `code` on what comes back, and keep the id.

Removing a discount stops new redemptions and frees its code for another discount. The subscriptions that hold it keep it for its duration.

## Codes at checkout

Customers type a code into the provider's hosted checkout. `allowDiscountCodes` is on by default; turn it off for a checkout that must not take one:

```ts
await useBilling().checkout(products.value!.pro!.id, { allowDiscountCodes: false })
```

A campaign link that carries a code passes it as `discountCode`, and the server applies it before the customer sees the checkout: see [Campaign links](https://nuxt-backend.dev/docs/build/billing/subscriptions/checkout#campaign-links).

## A discount the server chooses

A discount without a code is applied by its id, and only server code may name one: an id can name any discount, including a 100% one minted for a single claim. Pass it to `billing.checkout` from your own action, after deciding the caller qualifies. A client that sends `discountId` to `generateCheckoutLink` is refused as `invalid_input`:

```ts [backend/founders.ts]
import { v } from 'convex/values'
import { backendError } from 'nuxt-backend/functions'
import { action } from './_generated/server'
import { billing } from './billing'

export const founderCheckout = action({
  args: { productId: v.string(), origin: v.string(), successUrl: v.string() },
  handler: async (ctx, { productId, ...urls }) => {
    if (!(await isFounder(ctx))) throw backendError('forbidden', { reason: 'permission' })
    return billing.checkout(ctx, { productIds: [productId], ...urls, discountId: FOUNDER_DISCOUNT_ID })
  },
})
```

`isFounder` and `FOUNDER_DISCOUNT_ID` are your own. A catalog promotion needs no id: checkout and `billing.applyDiscount` name it by key.

## What happens when…

- **A client sends discountId:** `generateCheckoutLink` refuses it as [`invalid_input`](https://nuxt-backend.dev/docs/reference/backend/errors#invalid_input). Only server code names a discount by id.
- **A typed or linked code is unknown, expired or used up:** the provider refuses it, and the checkout is refused as `invalid_input` on `discountCode`. `<PricingTable>` opens it again without the code.
- **create gets a code another discount holds:** the provider refuses it, and the call throws its message. Remove the old discount first: removing frees its code.
- **Your own check says no:** throw `backendError('forbidden', …)` before you call `billing.checkout`, as the founder example does ([`forbidden`](https://nuxt-backend.dev/docs/reference/backend/errors#forbidden)).

## Limits

- **No stacking.** A checkout takes one discount: a typed code, a linked code or one your server names.
- **No drift checks.** `billing sync` never sees discounts made here. Declare the ones you reuse in the catalog.
- **Removing never ends a redeemed discount.** Subscriptions keep it for its duration.

## Test it

Mint a code against Polar's sandbox and buy with it:

```bash
npx convex run discounts:createDiscount '{"name":"Panels beta","percent":50,"code":"PANELSBETA"}'
```

Open `/pricing?discount=PANELSBETA`, pick a plan and pay with the test card `4242 4242 4242 4242`. The order in Polar's sandbox dashboard names the discount. Remove the discount afterwards with `billing.discounts.remove`.

## Related

- [Polar](https://nuxt-backend.dev/docs/services/polar): the account and sandbox the discounts live in.
- [Run a promotion](https://nuxt-backend.dev/docs/build/growth/offers): a discount declared in the catalog, kept in line by `billing sync` and named by key.
- [Open checkout and the portal](https://nuxt-backend.dev/docs/build/billing/subscriptions/checkout#what-the-server-decides): the one discount a checkout takes, and who may name it.
- [Share campaign links](https://nuxt-backend.dev/docs/build/growth/offers/campaign-links): a link that carries a code into checkout.


## Sitemap

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