Back to Home

$ examples

// Real-world use cases and integration guides

EXAMPLE 2

Python Full-Stack

Build a FastAPI backend with automated Python client for scheduled tasks. Perfect for bots, cron jobs, and server-to-server authentication.

$ what-you-will-build

FastAPI Server

Modern async Python API with OpenKitx403 middleware

Automated Bot

Python script that authenticates and fetches data automatically

Keypair Auth

Server-side authentication using Solana keypairs

$ prerequisites

  • [✓] Python 3.11+ installed
  • [✓] Basic Python knowledge
  • [✓] pip or poetry installed
  • [✓] Solana CLI (for keypair generation)

$ step-by-step-guide

1

Project Setup

# Create project
mkdir solana-bot-api
cd solana-bot-api

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Create directories
mkdir server bot
2

Install Dependencies

# Server dependencies
pip install openkitx403 fastapi uvicorn python-dotenv

# Bot dependencies
pip install openkitx403-client solana base58
3

Generate Keypair

# Generate new Solana keypair
solana-keygen new --outfile ./bot/keypair.json

# Or use Python to generate
python -c "from solana.keypair import Keypair; import json; kp = Keypair.generate(); print(json.dumps(list(kp.secret_key)))"

⚠️ Important

Keep your keypair secure! Add keypair.json to .gitignore

4

Write the Code

from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from openkitx403 import (
    OpenKit403Middleware,
    require_openkitx403_user,
    OpenKit403User
)
import uvicorn

app = FastAPI(title="Solana Bot API")

# Add CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Add OpenKit403 middleware
app.add_middleware(
    OpenKit403Middleware,
    audience="http://localhost:8000",
    issuer="bot-api-v1",
    ttl_seconds=60,
    bind_method_path=True
)

# Public endpoint
@app.get("/")
async def root():
    return {"message": "Solana Bot API", "status": "running"}

# Protected endpoint - Data feed
@app.get("/api/data")
async def get_data(user: OpenKit403User = Depends(require_openkitx403_user)):
    return {
        "wallet": user.address,
        "data": [
            {"id": 1, "value": "Data point 1", "timestamp": "2025-11-05T10:00:00Z"},
            {"id": 2, "value": "Data point 2", "timestamp": "2025-11-05T10:05:00Z"},
            {"id": 3, "value": "Data point 3", "timestamp": "2025-11-05T10:10:00Z"},
        ],
        "count": 3
    }

# Protected endpoint - Submit result
@app.post("/api/submit")
async def submit_result(
    result: dict,
    user: OpenKit403User = Depends(require_openkitx403_user)
):
    return {
        "success": True,
        "message": f"Result submitted by {user.address}",
        "result": result
    }

# Protected endpoint - Bot status
@app.get("/api/bot/status")
async def bot_status(user: OpenKit403User = Depends(require_openkitx403_user)):
    return {
        "bot_wallet": user.address,
        "status": "active",
        "last_run": "2025-11-05T10:15:00Z",
        "tasks_completed": 42
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
5

Run the Application

# Terminal 1 - Start server
cd server
python server.py

# Terminal 2 - Run bot
cd bot
python bot.py

• Server starts on http://localhost:8000

• Bot runs continuously, fetching data every 30 seconds

• Check server logs to see authenticated requests

$ expected-output

[BOT OUTPUT]

🤖 Starting Solana Bot...

✅ Loaded keypair: 5Gv8q5t...

==================================================

⏰ Running at 10:15:30

📥 Fetching data...

✅ Received 3 data points

⚙️ Processing data...

📤 Submitting result...

✅ Result submitted successfully

📊 Checking status...

✅ Bot status: active

Tasks completed: 42

😴 Waiting 30 seconds before next run...

$ real-world-use-cases

Data Aggregation Bot

Fetch blockchain data, process it, and submit to your API

Trading Bot

Authenticate with DeFi APIs and execute trades automatically

Monitoring System

Check wallet balances and alert on threshold changes

Scheduled Tasks

Run cron jobs that require wallet authentication

Download Complete Code

Get the complete working example with additional features, tests, and documentation.