> ## Documentation Index
> Fetch the complete documentation index at: https://docs.useroutr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Game top-ups

> Let players buy gems, coins and passes with whatever token they hold. Your game prices in USDC and receives exactly that.

![Players pay with ETH on Base, USDC or XLM, and the game adds 500 gems.](https://mintcdn.com/thirtn/tEtIKPxuXx_PTNHq/images/illustrations/use-case-games-light.svg?fit=max\&auto=format\&n=tEtIKPxuXx_PTNHq\&q=85\&s=97eafc1a98f23bff535c7bd746a9338e)

Embedded checkoutFunding intentsWebhooks

## The idea

Your shop sells 500 gems for 4.99 USDC. Your players hold ETH on Base, XLM, or the USDC they won
last tournament. Every one of them can tap **Top up** and pay with what they have, and your game
only ever sees 4.99 USDC arrive. Nobody has to visit an exchange first.

## How it plays out

**1. The player picks a bundle**

Your server creates a funding intent for the bundle's price and tags it with the player and
the bundle.

**2. They pay in their own token**

The [embedded checkout](https://docs.useroutr.com/payments/embedded-checkout.md) opens inside your game. If they pay with
something other than USDC, a [quote](https://docs.useroutr.com/concepts/quotes-and-routes.md) works out exactly how much
of their token covers 4.99 USDC.

**3. The gems land**

When the `funding.credited` webhook arrives, your server adds the gems. The checkout's
`onSuccess` is for the celebration animation, not for granting anything.

## The code

```ts Start a top-up theme={null}
import { Useroutr } from "@useroutr/sdk";

const useroutr = new Useroutr({ apiKey: process.env.USEROUTR_API_KEY });

const BUNDLES = {
  starter: { gems: 500, price: "4.99" },
  hoard: { gems: 2800, price: "24.99" },
} as const;

export async function startTopUp(
  playerId: string,
  bundleId: keyof typeof BUNDLES,
  orderId: string,
) {
  const bundle = BUNDLES[bundleId];
  const intent = await useroutr.fundingIntents.create(
    {
      amount: bundle.price,
      asset: "asset_usdc_stellar",
      reference: orderId,
      metadata: { player_id: playerId, bundle: bundleId, gems: bundle.gems },
    },
    { idempotencyKey: orderId },
  );
  const url = intent.hosted_funding_url ?? "";
  // The last segment is the funding token the embedded checkout needs
  return url.slice(url.lastIndexOf("/") + 1);
}
```

```ts Grant the gems theme={null}
// Inside your webhook handler, after verifying the event
if (event.type === "funding.credited") {
  const intent = event.data.object;
  await grantGems({
    playerId: String(intent.metadata.player_id),
    gems: Number(intent.metadata.gems),
    orderId: intent.reference, // grant each order once: deliveries can repeat
  });
}
```

The [webhooks guide](https://docs.useroutr.com/guides/webhooks.md) has the full handler, with signature verification.

## Level up

* **Make it look like your game.** Pass `appearance` to the checkout for your colour, logo,
  corners and font. See [Branding](https://docs.useroutr.com/payments/branding.md).
* **First top-up bonus.** You know every credited order, so doubling the first one is a check in
  your own database.
* **Season passes.** A pass is one more bundle: same flow, different metadata.
* **Keep the money on Base.** Ask for `asset_usdc_base` instead and it settles to your address
  there. See [A treasury on any chain](https://docs.useroutr.com/use-cases/treasury.md).

## Good to know

* Price bundles in a stable asset so 500 gems always cost the same. The quote does the
  conversion; you never handle exchange rates.
* An intent takes payment for 30 minutes by default and 24 hours at most. Create it when the
  player taps **Top up**, not when the shop opens.
* Grant on `funding.credited`, keyed by your order id. A webhook can be delivered more than once,
  and your handler should shrug it off.
* Which tokens players can pay with depends on the network and the environment; the sandbox lists
  fewer than production. Read them from `useroutr.assets.list()`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.