Skip to content

feat(x402): FastAPI/Flask Python decorator + Express.js TypeScript middleware - #28

Open
jdjioe5-cpu wants to merge 1 commit into
nirium-protocol:mainfrom
jdjioe5-cpu:main
Open

feat(x402): FastAPI/Flask Python decorator + Express.js TypeScript middleware#28
jdjioe5-cpu wants to merge 1 commit into
nirium-protocol:mainfrom
jdjioe5-cpu:main

Conversation

@jdjioe5-cpu

Copy link
Copy Markdown

Two x402 packages targeting the GrantFox OSS Campaign

This PR adds two complementary x402 implementations for the Nirium SDK v0.7.0, one in Python (FastAPI / Flask) and one in TypeScript (Express.js). Both satisfy the acceptance criteria from the GrantFox bounty campaign (Org ID: b6550049-a239-4dc1-b95e-2fb28ded7ea6).

1. Python - nirium.fastapi_x402

from fastapi import FastAPI, Request
from nirium.fastapi_x402 import x402_required

app = FastAPI()

@app.get("/premium")
@x402_required(price="0.02", pay_to="GXXX_STELLAR_ADDRESS")
async def premium(request: Request):
    return {"data": "paid-only"}
  • One decorator works on FastAPI and Flask routes.
  • Inspects X-402-Signature header; returns HTTP 402 + structured body + WWW-Authenticate when missing or invalid.
  • Async verify via aiohttp with bounded timeout.
  • Graceful bounded fallback when settlement is unreachable.
  • 5/5 pytest cases pass.

2. TypeScript - @nirium/express-x402

import express from 'express';
import { nirium } from '@nirium/express-x402';

app.get('/premium',
  nirium.x402Serve({ price: '0.02', payTo: 'GXXX_STELLAR_ADDRESS' }),
  (req, res) => res.json({ ok: true, paid: req.x402Validation })
);
  • Exposes nirium.x402Serve matching the Python helper API.
  • Uses fetch + AbortController for bounded verify.
  • Returns HTTP 402 + structured body + headers when missing/invalid signature.
  • 5/5 jest cases pass.

Bounded by design

Both packages validate signatures only. They never open bank rails, never issue credits, never mutate external state, never sign or broadcast Stellar transactions.

Tests

# Python
cd packages/sdk-python
pip install -e .
pip install fastapi httpx pytest
python -m pytest tests/test_fastapi_x402.py -v

# TypeScript
cd packages/sdk/express-x402
npm install
npm test

…t middleware

Two deliverables targeting GrantFox bounty campaign (Org b6550049-a239-4dc1-b95e-2fb28ded7ea6):

1. nirium.fastapi_x402 (Python)
   - @x402_required(price='0.02', pay_to='GXXX...')
   - Works on FastAPI and Flask routes (same decorator)
   - Inspects X-402-Signature header; returns HTTP 402 with structured body + WWW-Authenticate
   - Async verify via aiohttp with bounded timeout + offline fallback
   - 5 pytest tests cover happy / missing / short / unreachable / undecorated paths

2. @nirium/express-x402 (TypeScript)
   - nirium.x402Serve({ price, payTo, ... }) Express middleware
   - Inspects X-402-Signature header; returns HTTP 402 with bounded reason
   - Uses fetch + AbortController for verify with timeout
   - Exposes validation at req.x402Validation for downstream handlers
   - 5 jest tests cover the same paths

Both packages are read-only: they validate signatures; they never open bank rails, never issue credits, never mutate external state.
@jdjioe5-cpu

Copy link
Copy Markdown
Author

Hi maintainer — ping for review on PR #28. Both bounty #9 (Express.js) and #10 (Python) deliverables are complete:

  • Python: nirium.fastapi_x402 (5/5 pytest pass) — FastAPI/Flask @x402_required decorator
  • TypeScript: @nirium/express-x402 (5/5 jest pass) — nirium.x402Serve middleware

PR is mergeable: clean. No conflicts with main. Ready for review whenever you have a moment. Both deliverables satisfy the bounty acceptance criteria:

#10 (FastAPI Python x402 Decorator):

  • Compatible with FastAPI & Flask (single decorator)
  • Inspects X-402-Signature header and validates settlement
  • Exposes clean 402 HTTP status codes
  • Includes unit tests

#9 (Express.js x402 Micropayment Middleware Helper):

  • Exposes nirium.x402Serve({ price: '0.02', payTo: 'STELLAR_ADDRESS' })
  • Returns HTTP 402 Payment Required when no valid signature is provided
  • Includes unit tests with Jest

Payout address: Base USDC (will DM on completion). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant