Skip to main content
This example demonstrates how to make x402 payments using Crossmint custodial wallets. Crossmint provides managed wallet infrastructure, so you don’t need to handle private keys directly. Source: GitHub › scripts/solana-example/crossmint-payment.ts

When to Use Crossmint

Use Crossmint when:
  • You need custodial wallet management
  • You want to avoid handling private keys
  • You’re building enterprise applications with compliance requirements
  • You need wallet recovery and account management features
Use local wallets when:
  • You need full control over keys
  • You’re building non-custodial applications
  • You want to minimize third-party dependencies

Full Example

import "dotenv/config";
import { logResponse } from "../logger";
import { createCrossmintWallet } from "@faremeter/wallet-crossmint";
import { createPaymentHandler } from "@faremeter/x-solana-settlement";
import { wrap as wrapFetch } from "@faremeter/fetch";

// Address of your crossmint wallet
const crossmintWallet = process.env.CROSSMINT_WALLET;
const crossmintApi = process.env.CROSSMINT_API_KEY;

if (!crossmintWallet || !crossmintApi) {
  throw new Error(
    "Missing required environment variables: CROSSMINT_WALLET and CROSSMINT_API_KEY",
  );
}

const wallet = await createCrossmintWallet(
  "devnet",
  crossmintApi,
  crossmintWallet,
);
const fetchWithPayer = wrapFetch(fetch, {
  handlers: [createPaymentHandler(wallet)],
});

const req = await fetchWithPayer("http://127.0.0.1:3000/protected");
await logResponse(req);

Step-by-Step Breakdown

1. Get Crossmint Credentials

const crossmintWallet = process.env.CROSSMINT_WALLET;
const crossmintApi = process.env.CROSSMINT_API_KEY;
You’ll need:
  • CROSSMINT_WALLET: Your Crossmint wallet address (obtained from the Crossmint dashboard)
  • CROSSMINT_API_KEY: Your Crossmint API key with the wallets scope

2. Create Crossmint Wallet

const wallet = await createCrossmintWallet(
  "devnet",           // Solana network
  crossmintApi,       // API key
  crossmintWallet,    // Wallet address
);
This connects to Crossmint’s API and creates a wallet interface that:
  • Fetches your wallet using the API key
  • Exposes the same interface as local wallets
  • Handles transaction signing through Crossmint’s infrastructure

3. Create Payment Handler

const fetchWithPayer = wrapFetch(fetch, {
  handlers: [createPaymentHandler(wallet)],
});
The payment handler works identically to local wallets—the Crossmint integration is transparent to the payment logic.

Crossmint vs Local Wallets

import { createCrossmintWallet } from "@faremeter/wallet-crossmint";

const wallet = await createCrossmintWallet(
  "devnet",
  process.env.CROSSMINT_API_KEY!,
  process.env.CROSSMINT_WALLET!,
);

const fetchWithPayer = wrapFetch(fetch, {
  handlers: [createPaymentHandler(wallet)],
});
Tradeoffs:
  • Crossmint: No key management, recovery features, but requires API key and internet connection
  • Local: Full control, no external dependencies, but you manage keys and security

Environment Variables

  • CROSSMINT_WALLET: Your Crossmint wallet address
  • CROSSMINT_API_KEY: Your Crossmint API key with wallets scope

Setting Up Crossmint

  1. Sign up at crossmint.com
  2. Create a wallet through their dashboard or API
  3. Generate an API key with wallet permissions
  4. Add credentials to your .env file