Bot Examples

Copy-paste examples for common bot and AI agent patterns. All examples use testnet by default.

Example 1: Mint a Fungible Token

Create a new fungible token collection with server-side signing.

import { BitBadgesSigningClient, GenericEvmAdapter, NETWORK_CONFIGS, MsgCreateCollection } from 'bitbadgesjs-sdk';

const adapter = await GenericEvmAdapter.fromMnemonic(
  process.env.BOT_MNEMONIC!,
  NETWORK_CONFIGS['testnet'].evmRpcUrl
);

const client = new BitBadgesSigningClient({
  adapter,
  network: 'testnet'
});

const result = await client.signAndBroadcast([
  MsgCreateCollection.create({
    creator: client.address,
    // ... collection configuration
    // See SDK docs for full MsgCreateCollection fields
  })
]);

if (result.success) {
  console.log('Collection created! TX:', result.txHash);
} else {
  console.error('Failed:', result.error);
}

Example 2: Check Balance & Conditional Transfer

Check a user's token balance and transfer tokens based on a condition.

Example 3: Gate Access (Verify Ownership)

Verify that a user owns a specific badge before granting access.

Example 4: Subscribe to Events & Auto-React

Listen for transfers on a specific collection and take action.

Example 5: MCP Agent Workflow

When using the BitBadges MCP tools (e.g., in Claude Desktop or Claude Code), there are two build approaches:

Use per-field tools to build the collection incrementally, then verify:

One-Shot Build

For simple cases, use build_token to generate everything in one call:

Query Sequence (No Signing)

Note: The MCP server builds and validates transactions but does not sign or broadcast. Use the BitBadges SDK signing client or the BitBadges frontend to sign and submit.

Tips for AI Agents

  • Always start on testnet. Use the testnet API and signing client until you're confident in your workflow.

  • Simulate before broadcasting. Use simulate_transaction or the signing client's simulate: true option to catch errors before spending gas.

  • Use the faucet for testnet tokens. See Testnet Faucet API β€” one request per address, no API key needed.

  • Handle errors gracefully. Check result.success and result.error after every broadcast.

  • Sequence management. The signing client handles nonce/sequence automatically with retries.

  • Store credentials securely. Use environment variables (BITBADGES_MNEMONIC, BITBADGES_API_KEY) rather than hardcoding secrets.

Last updated