> ## 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 Python SDK Flask and FastAPI Integration

> Learn how to integrate the BlackSwan Python SDK into Flask and FastAPI backends to serve credit dashboard and eligibility data via REST APIs.

The BlackSwan Python SDK lets you query on-chain credit data from any Python backend. This tutorial shows how to expose credit dashboard and eligibility endpoints using both Flask and FastAPI.

## Install

```bash theme={null}
pip install blackswan-sdk-py
```

## Client Setup

```python theme={null}
from blackswan import BlackSwanClient

client = BlackSwanClient(
    network="amoy",
    rpc_url="https://polygon-amoy.g.alchemy.com/v2/your-api-key"
)
```

## Flask Backend

The Flask example below exposes two endpoints: one that returns the full credit dashboard for a wallet, and one that checks basic eligibility based on credit history and reputation:

```python theme={null}
from flask import Flask, jsonify
from blackswan import BlackSwanClient

client = BlackSwanClient(
    network="amoy",
    rpc_url="https://polygon-amoy.g.alchemy.com/v2/your-api-key"
)

app = Flask(__name__)

@app.route("/api/credit/<wallet>")
def get_credit(wallet):
    dashboard = client.get_credit_dashboard(wallet)
    # APR is returned in basis points — divide by 1000 to get a percentage.
    # For example, a raw value of 142 = 0.142%.
    return jsonify({
        "trust_ratio": dashboard.trust_ratio,
        "tier": dashboard.trust_tier,
        "apr": dashboard.current_apr / 1000,
        "successful_loans": dashboard.successful_loans
    })

@app.route("/api/eligible/<wallet>")
def check_eligible(wallet):
    history = client.get_credit_history(wallet)
    reputation = client.get_reputation(wallet)

    # Consider a borrower eligible if they have a positive reputation score
    # and no defaults on their credit history.
    eligible = reputation > 0 and history.defaults == 0

    return jsonify({
        "eligible": eligible,
        "reputation": reputation,
        "defaults": history.defaults
    })

if __name__ == "__main__":
    app.run(debug=True)
```

## FastAPI Backend

FastAPI's async support pairs naturally with the SDK. The example below mirrors the Flask routes with typed Pydantic-style response models:

```python theme={null}
from fastapi import FastAPI, HTTPException
from blackswan import BlackSwanClient

client = BlackSwanClient(
    network="amoy",
    rpc_url="https://polygon-amoy.g.alchemy.com/v2/your-api-key"
)

app = FastAPI()

@app.get("/credit/{wallet}")
async def credit(wallet: str):
    try:
        dashboard = client.get_credit_dashboard(wallet)
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))

    # APR is returned in basis points — divide by 1000 to get a percentage.
    return {
        "trust_ratio": dashboard.trust_ratio,
        "tier": dashboard.trust_tier,
        "apr": dashboard.current_apr / 1000
    }

@app.get("/eligible/{wallet}")
async def eligible(wallet: str):
    try:
        history = client.get_credit_history(wallet)
        reputation = client.get_reputation(wallet)
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))

    return {
        "eligible": reputation > 0 and history.defaults == 0,
        "reputation": reputation,
        "defaults": history.defaults
    }
```

## Understanding APR Values

APR values from the BlackSwan SDK are always integers expressed in basis points. To convert to a human-readable percentage, divide by `1000`. A raw value of `142` equals `0.142%`.
