Documentation
Quick start
Add the donation question to your Stellar app. Try it end to end on testnet with the demo foundation first, then point it at a real foundation.
1. Install the packages
Add spare-sdk and spare-wallets-kit to your app. The kit opens confirmation in wallets such as Freighter, LOBSTR, or Ledger. Both packages are ESM-only and need Node 20 or newer: in a Node project set "type": "module" in package.json.
npm install spare-wallets-kit spare-sdk @creit.tech/stellar-wallets-kit @stellar/stellar-sdkpnpm add spare-wallets-kit spare-sdk @creit.tech/stellar-wallets-kit @stellar/stellar-sdkyarn add spare-wallets-kit spare-sdk @creit.tech/stellar-wallets-kit @stellar/stellar-sdk2. Try it on testnet
Get testnet USDC
You need a testnet account with Circle's testnet USDC (issuer GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5). Create one with stellar keys generate my-user --network testnet --fund and read the secret with stellar keys secret my-user, or let Friendbot fund any keypair. The script below funds it, adds the USDC trustline, and swaps XLM for 5 USDC on the testnet DEX.
import { Asset, BASE_FEE, Horizon, Keypair, Networks, Operation, TransactionBuilder } from "@stellar/stellar-sdk";
const server = new Horizon.Server("https://horizon-testnet.stellar.org");
const keypair = Keypair.fromSecret(process.env.SECRET!); // a testnet secret key (S...)
const usdc = new Asset("USDC", "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5"); // Circle testnet
// 1. Friendbot funds the account with testnet XLM (skip if it already has some).
await fetch(`https://friendbot.stellar.org?addr=${keypair.publicKey()}`);
// 2. Trust Circle's testnet USDC, then swap XLM for exactly 5 USDC on the testnet DEX.
const tx = new TransactionBuilder(await server.loadAccount(keypair.publicKey()), {
fee: BASE_FEE,
networkPassphrase: Networks.TESTNET,
})
.addOperation(Operation.changeTrust({ asset: usdc }))
.addOperation(
Operation.pathPaymentStrictReceive({
sendAsset: Asset.native(),
sendMax: "500", // XLM you are willing to spend
destination: keypair.publicKey(),
destAsset: usdc,
destAmount: "5",
}),
)
.setTimeout(60)
.build();
tx.sign(keypair);
console.log("funded:", (await server.submitTransaction(tx)).hash);Run a donation end to end
This Node script builds a payment, asks Spare for a US$0.50 donation to the demo foundation, signs, and submits to Horizon. The headless signer stands in for a wallet and confirm stands in for the modal. Save it as donate.ts and run it with SECRET=S... npx tsx donate.ts.
import { Asset, BASE_FEE, Horizon, Keypair, Networks, Operation, TransactionBuilder } from "@stellar/stellar-sdk";
import { HorizonAccountPort } from "spare-sdk";
import { withSpare } from "spare-wallets-kit";
const server = new Horizon.Server("https://horizon-testnet.stellar.org");
const keypair = Keypair.fromSecret(process.env.SECRET!); // a testnet secret key (S...)
// 1. The transaction your app already builds: pay 1 XLM to a (freshly funded) merchant.
const merchant = Keypair.random();
await fetch(`https://friendbot.stellar.org?addr=${merchant.publicKey()}`);
const tx = new TransactionBuilder(await server.loadAccount(keypair.publicKey()), {
fee: BASE_FEE,
networkPassphrase: Networks.TESTNET,
})
.addOperation(Operation.payment({ destination: merchant.publicKey(), asset: Asset.native(), amount: "1" }))
.setTimeout(60)
.build();
// 2. A headless "wallet". The kit only needs a signTransaction(xdr, opts) that returns { signedTxXdr }.
const signer = {
async signTransaction(xdr: string, opts?: { networkPassphrase?: string }) {
const toSign = TransactionBuilder.fromXDR(xdr, opts?.networkPassphrase ?? Networks.TESTNET);
toSign.sign(keypair);
return { signedTxXdr: toSign.toXDR() };
},
};
// 3. Spare wraps it. A custom `confirm` replaces the browser modal: return true only on an explicit yes.
const spare = withSpare(signer, {
foundation: { account: "GCAEBBLSVFM2RLNBYMEZLFWHCXFHOGVJAXQK2K3WVHS2UGVDVWJOXGQJ", domain: "donatespare.xyz" },
foundationName: "Spare demo foundation",
mode: { amount: "0.50" },
appId: "quickstart",
maxAmount: "5",
accounts: new HorizonAccountPort({ network: "testnet" }),
network: "testnet",
confirm: async (prompt) => {
console.log(`Add US$${prompt.amount} for ${prompt.foundationName}? (auto-yes in this demo)`);
return true;
},
});
// Pass the passphrase so the kit can parse the XDR.
const result = await spare.signWithDonation(tx.toXDR(), { networkPassphrase: Networks.TESTNET });
console.log(result.outcome, result.reason ?? "", "changed:", result.changed);
// "donated": changed is true and the signed tx carries the donation.
// "declined" | "not-offered" | "passthrough" | "error": the original tx was signed.
// 4. Submitting is your job.
const signed = TransactionBuilder.fromXDR(result.signedTxXdr, Networks.TESTNET);
console.log("submitted:", (await server.submitTransaction(signed)).hash);You should see donated changed: true and a transaction hash. Open it on a testnet explorer: the payment is followed by a 0.50 USDC payment to the demo foundation, with the memo spare:quickstart.
3. Ask at the moment of signing
In a browser app, wrap Stellar Wallets Kit. Before they sign, the user sees the option to add a little or round up for a foundation. The example below is the production shape: replace the placeholder G... and foundation.org with your foundation's registered account and its website domain, and network with "mainnet".
Classic transactions
import { StellarWalletsKit } from "@creit.tech/stellar-wallets-kit";
import { HorizonAccountPort } from "spare-sdk";
import { withSpare } from "spare-wallets-kit";
const network = "testnet"; // "mainnet" in production
const spare = withSpare(StellarWalletsKit, {
foundation: { account: "G...", domain: "foundation.org" }, // listed in its stellar.toml
foundationName: "Tellus Cooperative",
mode: { amount: "0.50" }, // or { roundUp: true } for USDC purchases
appId: "my-app", // <= 22 ASCII bytes
maxAmount: "5",
accounts: new HorizonAccountPort({ network }),
network,
locale: "en",
});
// Same signature as the kit: use it wherever you called kit.signTransaction.
const { signedTxXdr } = await spare.signTransaction(xdr, { address });network is optional when you pass the testnet or mainnet passphrase to signTransaction and your version of spare-wallets-kit infers the network from it. Passing it explicitly always works.
Need to know whether the hash changed, or why nothing was offered? Use signWithDonation instead of signTransaction: it returns { signedTxXdr, donation, changed, outcome, reason? }. The outcome is donated, declined, not-offered, passthrough, or error, and every outcome except donated signs your original transaction.
const result = await spare.signWithDonation(xdr, { address });
// result.outcome: "donated" | "declined" | "not-offered" | "passthrough" | "error"
// result.changed is true only for "donated" (the hash changed); result.donation has the details.
// For the other outcomes the original tx was signed; result.reason says why (when there is one).
if (result.outcome === "donated") console.log("Donated", result.donation!.amount, "USDC");
else console.log(result.outcome, result.reason ?? "");
// Submitting is your job: send result.signedTxXdr as it is (it may carry a different hash).
const signed = TransactionBuilder.fromXDR(result.signedTxXdr, Networks.TESTNET);
await server.submitTransaction(signed);Soroban calls
// A Soroban tx holds one operation: the donation is a separate payment after success.
const follow = await spare.offerFollowUp({ successful: true, sourceAccount: address, hash });
if (follow.status === "signed") await submit(follow.signedTxXdr); // submitting is your jobBuilding your own interface? Use addDonation (classic) or donateAfter (Soroban) from spare-sdk directly.
4. Keep the registry in view
The gift goes straight to the foundation. The indexer and dashboard publish the movement.