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, GenericCosmosAdapter, MsgCreateCollection } from 'bitbadgesjs-sdk';

const adapter = await GenericCosmosAdapter.fromMnemonic(
  process.env.BOT_MNEMONIC!,
  'bitbadges-2'
);

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), a typical workflow looks like this:

Tool Call Sequence

  1. Get instructions for what you want to build:

  2. Build the transaction:

  3. Validate the transaction is well-formed:

  4. Simulate to check gas and catch errors:

  5. Sign and broadcast:

Query Sequence (No Signing)

Tips for AI Agents

  • Always start on testnet. The sign_and_broadcast MCP tool defaults to testnet for safety. Only switch to mainnet when 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. For MCP tools, sign_and_broadcast handles this internally.

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

Last updated