> ## Documentation Index
> Fetch the complete documentation index at: https://blackswan-23965643.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# BlackSwan JavaScript SDK Examples and Integrations

> Step-by-step BlackSwan SDK examples for React, Next.js, Node.js, and Express. Includes credit dashboard, trust score, and APR display code.

The examples below show you how to integrate the BlackSwan JavaScript SDK into the most common application frameworks. Each example is self-contained — pick the one that matches your stack and adapt it to your use case.

<Tabs>
  <Tab title="React">
    Render a borrower's credit profile inside a React component. The component initializes the client outside the render cycle so it isn't recreated on every re-render, then fetches the dashboard on mount whenever the `wallet` prop changes.

    ```tsx theme={null}
    import { useState, useEffect } from "react";
    import { BlackSwanClient } from "blackswan-sdk";

    const client = new BlackSwanClient({
      network: "amoy",
      rpcUrl: process.env.NEXT_PUBLIC_RPC_URL
    });

    function CreditCard({ wallet }: { wallet: string }) {
      const [credit, setCredit] = useState<any>(null);

      useEffect(() => {
        client.getCreditDashboard(wallet).then(setCredit);
      }, [wallet]);

      if (!credit) return <p>Loading...</p>;

      return (
        <div>
          <p>Trust Ratio: {credit.trustRatio}</p>
          <p>Tier: {credit.trustTierScore}</p>
          <p>APR: {credit.currentApr / 1000}%</p>
        </div>
      );
    }
    ```
  </Tab>

  <Tab title="Next.js">
    Share a single `BlackSwanClient` instance across your Next.js application by exporting it from a library file, then call it from an App Router route handler to expose a credit API endpoint.

    ```ts lib/blackswan.ts theme={null}
    import { BlackSwanClient } from "blackswan-sdk";

    export const blackswan = new BlackSwanClient({
      network: "amoy",
      rpcUrl: process.env.NEXT_PUBLIC_RPC_URL!
    });
    ```

    ```ts app/api/credit/[wallet]/route.ts theme={null}
    import { NextRequest } from "next/server";
    import { blackswan } from "@/lib/blackswan";

    export async function GET(
      req: NextRequest,
      { params }: { params: { wallet: string } }
    ) {
      const credit = await blackswan.getCreditDashboard(params.wallet);
      return Response.json(credit);
    }
    ```
  </Tab>

  <Tab title="Node.js">
    Run a standalone Node.js script to fetch and log a wallet's full credit dashboard. This is useful for scripts, cron jobs, or CLI tooling.

    ```ts node.ts theme={null}
    import { BlackSwanClient } from "blackswan-sdk";

    const client = new BlackSwanClient({
      network: "amoy",
      rpcUrl: "https://polygon-amoy.g.alchemy.com/v2/your-key"
    });

    async function main() {
      const wallet = "0x4EEA76237a91880B1c8B7a1c740610fFC0306EE4";
      const dashboard = await client.getCreditDashboard(wallet);
      console.log(`Trust Ratio: ${dashboard.trustRatio}`);
      console.log(`Tier: ${dashboard.trustTierScore}`);
      console.log(`APR: ${dashboard.currentApr / 1000}%`);
    }

    main();
    ```
  </Tab>

  <Tab title="Express.js">
    Expose a REST endpoint in an Express application that returns a wallet's credit dashboard as JSON. The route handler uses a `try/catch` block so RPC errors return a structured error response rather than crashing the server.

    ```ts theme={null}
    import express from "express";
    import { BlackSwanClient } from "blackswan-sdk";

    const app = express();
    const client = new BlackSwanClient({
      network: "amoy",
      rpcUrl: process.env.RPC_URL!
    });

    app.get("/credit/:wallet", async (req, res) => {
      try {
        const credit = await client.getCreditDashboard(req.params.wallet);
        res.json(credit);
      } catch (err: any) {
        res.status(500).json({ error: err.message });
      }
    });

    app.listen(3000);
    ```
  </Tab>
</Tabs>
