# Mint with the SDK

Mint BTC.R in code: order, register, pay, track. Minting needs no Maxy sign-in: every action is authorized by a wallet signature.

Mints have no sandbox, so this runs on mainnet with your live key, real BTC and ETH for gas on Robinhood Chain. In a browser, use your wallet adapters ([Signers](https://maxy.co/docs/install-and-signers#signers)) in place of the keys.

```ts
import { bitcoinSignerFromKey, createMaxyClient, evmSignerFromPrivateKey, isMaxyError, preparedPostOf } from '@maxy-labs/sdk';

const maxy = createMaxyClient(); // reads MAXY_API_KEY
const evm = evmSignerFromPrivateKey(process.env.EVM_PRIVATE_KEY!);
const bitcoin = bitcoinSignerFromKey(process.env.BTC_PRIVATE_KEY!);

// 0. What the mint accepts today.
const limits = await maxy.mint.limits();
if (limits.closedReason) throw new Error(`Minting is closed: ${limits.closedReason}`);

// 1. Create the order. The wallet signs one message.
let created;
try {
  created = await maxy.mint.createOrder({ amountSats: 100_000n, evm, bitcoin, confirmations: 1 });
} catch (error) {
  // A timeout or 5xx is an unknown result. Resend the SAME request; it never creates a second order.
  const prepared = preparedPostOf(error);
  if (isMaxyError(error) && error.retryable && prepared) created = await maxy.mint.resumeCreateOrder(prepared);
  else throw error;
}

// 2. Register on Robinhood Chain (gas in ETH).
await maxy.mint.register(created, evm);

// 3. Pay: one Bitcoin payment, checked before it is broadcast.
const paid = await maxy.mint.pay(created, bitcoin);
console.log(paid.txid, paid.outcome.status); // 'ambiguous': do NOT pay again; look the txid up first

// 4. Track until it is minted or needs attention.
const final = await maxy.mint.track(created.order.orderId, { intervalMs: 15_000 });
if (final.view.phase === 'minted') console.log('minted in', final.view.proofTxHash);
else if (final.state === 'refundable') console.log('see Mint refunds');
```

`maxy.mint.limits()` is the first call to try: it reads, and moves nothing. To retry after an unknown result across a restart, keep `created.prepared` and pass it to `resumeCreateOrder`. Never rotate the idempotency key.

## If it stops

| Case | Do |
| --- | --- |
| `paid.outcome.status` is `ambiguous` | Do not pay again. Look the txid up first |
| `limits.closedReason` is set | Minting is closed |
| `conflict`, `IDEMPOTENCY_CONFLICT` | Reload the order. Do not change the key |
| `final.state` is `refundable` | [Mint refunds](#mint-refunds) |

If your wallet signs PSBTs only in its own interface, use `buildDepositPayment`.

## Mint refunds

A payment that cannot mint stays with the issuer until the depositor asks for it back.

| State | Meaning |
| --- | --- |
| `UNDERPAID` | Less than the amount arrived |
| `OVERPAID` | More than the amount arrived |
| `LATE` | It arrived after the deposit window closed |
| `REPEATED` | More than one payment reached the address |

1. Request a refund quote: `mint.requestRefundQuote(orderId)`.
2. Sign it: `mint.refund(orderId, quote, evm)`.
3. The issuer sends the Bitcoin and reports the transaction.

The refund address is fixed when the order is signed. A quote expires; if you miss it, request a new one.

## Redeem BTC.R

Burn BTC.R, and the issuer pays Bitcoin to your address. The wallet needs ETH for gas.

```ts
// 1. Ask for a quote: the wallet signs a RedemptionIntent (ProofMintRedemption version 1, chain 4663). It expires in 9 minutes.
const quote = await maxy.mint.requestRedemptionQuote({ grossSats: 50_000n, destination: 'bc1q...your address', evm });
console.log(`burn ${quote.grossSats} sats of BTC.R, receive ${quote.netSats} sats`);

// 2. Redeem: checks the quote hash on chain, the quote signer, the token, that the quote is unused and the balance covers
//    it; approves exactly grossSats × 10^10 units if the allowance is short; then burns.
const { approveHash, redeemHash } = await maxy.mint.redeem(quote, evm);

// 3. Track the Bitcoin payout (ACCEPTED → SIGNED → BROADCAST → CONFIRMED, or EXPIRED if never redeemed).
const { response, view } = await maxy.mint.getRedemption(quote.epochId, quote.quoteId);
console.log(view.phase, response.confirmedTxid);
```

The burn is final. After it, the Bitcoin payout depends on the issuer. The net payout must exceed the dust threshold of the destination type:

| Destination | Minimum |
| --- | --- |
| P2WPKH | 294 sats |
| P2TR | 330 sats |
| P2PKH, P2SH | 546 sats |

---
Source: https://maxy.co/docs/mint-with-the-sdk (Maxy Docs › Mints (Maxy Mint) › Build with it)
