React & Next.js Quickstart

Wire BitBadges into a scratch React or Next.js app in under 10 minutes. This page walks through the canonical frontend flow: install β†’ connect wallet β†’ query a collection β†’ sign + broadcast a transaction as one copy-pasteable set of snippets.

Framing this page assumes: Next.js 14+ with the App Router, Keplr as the featured wallet (BitBadges is a Cosmos chain, so Keplr is the native choice), and the testnet (bitbadges-2, EVM chain 50025) for examples. Plain React (Vite/CRA) works identically β€” only the "use client" directive is Next.js-specific. Pages Router users should add the same snippets to a component rendered inside _app.tsx.

MetaMask is also fully supported via the EVM path β€” see the EVM wallet section below.

1. Install

npm install bitbadges ethers

ethers is only required if you plan to use MetaMask or any EVM wallet. Keplr-only apps can skip it.

2. Connect Wallet (Keplr β€” default)

Keplr is the Cosmos-native wallet and the recommended path for BitBadges. Users install the Keplr browser extension once, then your app calls GenericCosmosAdapter.fromKeplr(chainId) to get a wallet adapter.

// app/components/ConnectKeplr.tsx
'use client';

import { useEffect, useState } from 'react';
import { GenericCosmosAdapter, type WalletAdapter } from 'bitbadges';

export function ConnectKeplr() {
  const [adapter, setAdapter] = useState<WalletAdapter | null>(null);
  const [address, setAddress] = useState<string>('');
  const [error, setError] = useState<string>('');

  async function connect() {
    try {
      // 'bitbadges-2' = testnet. Use 'bitbadges-1' for mainnet.
      const a = await GenericCosmosAdapter.fromKeplr('bitbadges-2');
      setAdapter(a);
      setAddress(a.address);
    } catch (e: any) {
      setError(e.message ?? 'Failed to connect Keplr');
    }
  }

  return (
    <div>
      {address ? (
        <p>Connected: {address}</p>
      ) : (
        <button onClick={connect}>Connect Keplr</button>
      )}
      {error && <p style={{ color: 'red' }}>{error}</p>}
    </div>
  );
}

Note: If the user does not have Keplr installed, fromKeplr throws. Detect with typeof window !== 'undefined' && (window as any).keplr and prompt the user to install from keplr.apparrow-up-right.

3. Connect Wallet (MetaMask β€” EVM path)

BitBadges also supports any EVM wallet (MetaMask, Rabby, Coinbase Wallet) via precompiles. The SDK routes transactions through the EVM JSON-RPC automatically when you use GenericEvmAdapter.

Cosmos vs EVM addresses: The same user gets a different address depending on which adapter they use (Cosmos derivation vs Ethereum derivation). Decide up front which path your app supports, and fund the right address. See Signing Client β€” Server-Side for details.

4. Query a Collection

The BitBadgesAPI class is wallet-independent β€” you can instantiate it as soon as the page loads and query public data without a wallet connection.

API key on the client: NEXT_PUBLIC_* env vars are exposed to the browser. That is fine for read-only keys from bitbadges.io/developer. Keep write keys server-side only β€” call your own API route instead of hitting BitBadges from the browser.

5. Sign + Broadcast a Transaction

Once you have an adapter (from step 2 or 3), create a BitBadgesSigningClient and send messages. The client auto-estimates gas, handles sequence numbers, and retries on mismatch.

For write flows that only need a signature (not a full transaction β€” e.g. Sign In with BitBadges), skip BitBadgesSigningClient and call adapter.signArbitrary(...) directly.

Putting It Together

A minimal app/page.tsx that wires the above components:

Reference Implementation

The bitbadges-frontendarrow-up-right repo is a production Next.js app using these exact primitives. Good for seeing how wallet state is lifted into React context, how network switching is handled, and how transaction UX (approve / reject / pending) is wired.

Next Steps

  • Signing Client Reference β€” every BitBadgesSigningClient option, network preset, and error path.

  • Common Snippets β€” balance lookups, metadata, transfers with increments, approval inspection.

  • Sign In with BitBadges β€” OAuth-style authentication if you don't need transactions, just wallet identity.

  • SDK Types β€” NumberType conventions, BigIntify / Stringify, the 3D balance array.

  • Testnet Faucet β€” grab BADGE on testnet (bitbadges-2) to test the transfer flow.

Last updated