{
  "about": "algosdk (JavaScript) recipes, each run against TestNet.",
  "capturedOn": "2026-12-03",
  "sdkVersion": "3.9.0",
  "recipes": [
    {
      "slug": "how-to-check-if-an-account-opted-in-to-an-asset-algosdk",
      "question": "How do I check if an Algorand account has opted in to an asset (algosdk JavaScript)?",
      "answer": "Call algod.accountAssetInformation(address, assetId). If the account holds the asset it returns the holding, including the amount in base units.",
      "code": "async () => {\n    const info = await algod.accountAssetInformation(addr, USDC).do();\n    return { optedIn: true, amount: String(info.assetHolding.amount) };\n  }",
      "output": {
        "optedIn": true,
        "amount": "1825001"
      },
      "notes": [
        "The result is an object with assetHolding; the amount is a bigint in the asset's base units.",
        "An account that has opted in with a zero balance returns a holding with amount 0, so test for the call succeeding, not for amount > 0."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "algosdk-account-asset-info-not-found-means-not-opted-in",
      "question": "What does algosdk return for an account that has not opted in to an asset?",
      "answer": "The same call throws an error with HTTP status 404 and the message \"account asset info not found\". Catch it and treat 404 as \"not opted in\".",
      "code": "async () => {\n    try { await algod.accountAssetInformation(unfunded, USDC).do(); return { optedIn: true }; }\n    catch (e) { return { optedIn: false, status: e.status, message: String(e.response?.body?.message || e.message) }; }\n  }",
      "output": {
        "optedIn": false,
        "status": 404,
        "message": "Network request error. Received status 404 (Not Found): account asset info not found"
      },
      "notes": [
        "Tested with a freshly generated address that had never been funded. I did not separately test an account that is funded but has not opted in.",
        "The status was available as e.status (404) and e.message held the text \"Network request error. Received status 404 (Not Found): account asset info not found\". In this run e.response.body.message was empty, so read e.message."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "how-to-read-a-usdc-balance-on-algorand-in-dollars",
      "question": "How do I read a USDC balance on Algorand and show it in dollars?",
      "answer": "USDC on Algorand has 6 decimals, so divide the base-unit amount by 1,000,000. Keep the base units as a BigInt and only convert for display.",
      "code": "async () => {\n    const { assetHolding } = await algod.accountAssetInformation(addr, USDC).do();\n    const base = BigInt(assetHolding.amount);\n    const dollars = Number(base / 10000n) / 100; // 6 decimals: 1 USDC = 1_000_000 base units\n    return { baseUnits: String(base), usdc: dollars };\n  }",
      "output": {
        "baseUnits": "1825001",
        "usdc": 1.82
      },
      "notes": [
        "Here 1825001 base units is 1.825001 USDC. The code shown floors to cents (1.82); it does not round.",
        "Never do money arithmetic on the converted float; compare and add base-unit BigInts."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "how-to-read-the-algorand-minimum-balance-and-spendable-amount",
      "question": "How do I find how much ALGO an Algorand account can actually spend?",
      "answer": "Read accountInformation(address) and subtract minBalance from amount. The minimum balance stays locked in the account.",
      "code": "async () => {\n    const a = await algod.accountInformation(addr).do();\n    return { balance: String(a.amount), minBalance: String(a.minBalance), spendable: String(a.amount - a.minBalance) };\n  }",
      "output": {
        "balance": "1300000",
        "minBalance": "200000",
        "spendable": "1100000"
      },
      "notes": [
        "In this run the account held 1,300,000 microAlgos with a minimum of 200,000 (0.1 base plus 0.1 for one asset opt-in), so 1,100,000 was spendable before fees.",
        "Both fields are in microAlgos (1 ALGO = 1,000,000). See measured minimum-balance costs."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "how-to-get-the-algorand-minimum-fee-and-latest-round",
      "question": "How do I get the current Algorand network fee and latest round with algosdk?",
      "answer": "Call algod.getTransactionParams() for the fee fields and algod.status() for the latest round.",
      "code": "async () => {\n    const s = await algod.status().do();\n    const p = await algod.getTransactionParams().do();\n    return { lastRoundIsPositive: s.lastRound > 0n, minFee: String(p.minFee), flatFee: p.flatFee };\n  }",
      "output": {
        "lastRoundIsPositive": true,
        "minFee": "1000",
        "flatFee": false
      },
      "notes": [
        "The minimum fee was 1000 microAlgos (0.001 ALGO) and flatFee was false, which the SDK documents as \"fee computed from the transaction size, never below the minimum\" (documented behaviour, not separately tested here).",
        "Fetch params right before building a transaction: they carry the validity window."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "how-to-read-an-algorand-asset-name-and-decimals",
      "question": "How do I read an Algorand asset's name, unit name and decimals?",
      "answer": "Call algod.getAssetByID(assetId) and read params.",
      "code": "async () => {\n    const a = await algod.getAssetByID(USDC).do();\n    return { name: a.params.name, unitName: a.params.unitName, decimals: a.params.decimals };\n  }",
      "output": {
        "name": "USDC",
        "unitName": "USDC",
        "decimals": 6
      },
      "notes": [
        "TestNet USDC is asset 10458941 with 6 decimals. Mainnet USDC is 31566704; always check the id belongs to the network of the node you call."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    },
    {
      "slug": "how-to-validate-an-algorand-address-in-javascript",
      "question": "How do I validate an Algorand address in JavaScript?",
      "answer": "algosdk.isValidAddress(address) checks the length, the base32 encoding and the built-in checksum.",
      "code": "async () => {\n    return { real: algosdk.isValidAddress(addr), typo: algosdk.isValidAddress(addr.slice(0, 57) + 'A'), lowercase: algosdk.isValidAddress(addr.toLowerCase()) };\n  }",
      "output": {
        "real": true,
        "typo": false,
        "lowercase": false
      },
      "notes": [
        "In this run a valid address returned true; the same address with its last character replaced returned false (the checksum caught it); the lowercased address returned false.",
        "Validate user input before building a transaction: a bad address otherwise surfaces later as a less clear error."
      ],
      "source": "run against TestNet with algosdk 3.9.0 on 2026-12-03"
    }
  ]
}