| Name | openlibx402-fastapi JSON |
| Version |
0.1.1
JSON |
| download |
| home_page | None |
| Summary | FastAPI middleware and decorators for X402 payment protocol |
| upload_time | 2025-10-28 16:38:32 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.8 |
| license | MIT License
Copyright (c) 2025 OpenLibx402 Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. |
| keywords |
fastapi
middleware
openlibx402
payments
x402
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# openlibx402-fastapi
FastAPI middleware and decorators for adding X402 payment requirements to API endpoints.
## Overview
The `openlibx402-fastapi` package makes it easy to add payment requirements to your FastAPI endpoints. Simply decorate endpoints with payment requirements, and the middleware handles payment verification automatically.
## Features
- Easy-to-use decorators for payment-required endpoints
- Dependency injection support for accessing payment details
- Automatic 402 Payment Required response generation
- Configurable payment amounts and descriptions
- Seamless integration with existing FastAPI applications
## Installation
```bash
pip install openlibx402-fastapi
```
## Quick Start
### 1. Initialize X402 Configuration
```python
from fastapi import FastAPI
from openlibx402_fastapi import X402Config, init_x402
import os
# Initialize X402 configuration
config = X402Config(
payment_address=os.getenv("PAYMENT_WALLET_ADDRESS"),
token_mint=os.getenv("USDC_MINT_ADDRESS"),
network="solana-devnet",
rpc_url=os.getenv("SOLANA_RPC_URL", "https://api.devnet.solana.com"),
)
init_x402(config)
app = FastAPI()
```
### 2. Add Payment Requirements with Decorator
```python
from fastapi import Request
from openlibx402_fastapi import payment_required
@app.get("/premium-data")
@payment_required(
amount="0.10",
description="Access to premium market data"
)
async def get_premium_data(request: Request):
"""Endpoint requires 0.10 USDC payment"""
return {
"data": "This is premium content",
"market_data": {"price": 100.50, "volume": 1_000_000}
}
```
### 3. Access Payment Details with Dependency Injection
```python
from fastapi import Depends
from openlibx402_fastapi import verify_payment_factory
from openlibx402_core import PaymentAuthorization
@app.get("/expensive-data")
async def get_expensive_data(
payment: PaymentAuthorization = Depends(
verify_payment_factory(
amount="1.00",
description="Access to expensive AI model inference"
)
)
):
"""Access payment details in your endpoint"""
return {
"data": "AI-generated content",
"payment_id": payment.payment_id,
"amount_paid": payment.actual_amount,
"transaction_hash": payment.transaction_hash,
}
```
## Usage Patterns
### Pattern 1: Simple Decorator (Recommended)
Best for endpoints that don't need to access payment details:
```python
@app.get("/data")
@payment_required(amount="0.05", description="Data access")
async def get_data(request: Request):
return {"data": "content"}
```
### Pattern 2: Dependency Injection
Best when you need to access payment details (transaction hash, amount, etc.):
```python
@app.get("/data")
async def get_data(
payment: PaymentAuthorization = Depends(
verify_payment_factory(amount="0.05", description="Data access")
)
):
return {
"data": "content",
"payment_id": payment.payment_id,
"tx_hash": payment.transaction_hash,
}
```
### Pattern 3: Dynamic Pricing
Adjust payment requirements based on parameters:
```python
@app.get("/tiered-data/{tier}")
@payment_required(amount="0.05", description="Tiered data access")
async def get_tiered_data(request: Request, tier: str):
"""Payment required regardless of tier"""
tier_data = {
"basic": {"quality": "720p"},
"premium": {"quality": "4K"},
}
return {"tier": tier, "data": tier_data.get(tier)}
```
## Complete Example
```python
from fastapi import FastAPI, Depends, Request
from openlibx402_fastapi import (
payment_required,
verify_payment_factory,
X402Config,
init_x402
)
from openlibx402_core import PaymentAuthorization
import os
# Initialize X402
config = X402Config(
payment_address=os.getenv("PAYMENT_WALLET_ADDRESS"),
token_mint=os.getenv("USDC_MINT_ADDRESS"),
network="solana-devnet",
)
init_x402(config)
app = FastAPI()
@app.get("/")
async def root():
"""Public endpoint - no payment required"""
return {"message": "Welcome to the API"}
@app.get("/premium")
@payment_required(amount="0.10", description="Premium data")
async def premium(request: Request):
"""Protected endpoint - payment required"""
return {"data": "premium content"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
```
## Configuration
### X402Config Options
- `payment_address`: Your Solana wallet address for receiving payments
- `token_mint`: Token mint address (e.g., USDC on Solana)
- `network`: Network name (e.g., "solana-devnet", "solana-mainnet")
- `rpc_url`: Solana RPC endpoint URL
## Documentation
For complete API reference and guides, see:
- [Documentation](https://openlibx402.github.io/docs)
- [GitHub Repository](https://github.com/openlibx402/openlibx402)
- [Full Example](https://github.com/openlibx402/openlibx402/tree/main/examples/python/fastapi-server)
## Testing
```bash
pytest tests/
```
## License
MIT License - See [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "openlibx402-fastapi",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "fastapi, middleware, openlibx402, payments, x402",
"author": null,
"author_email": "OpenLibx402 Contributors <hello@openlibx402.org>",
"download_url": "https://files.pythonhosted.org/packages/49/0e/35f27ae1feccd357b7e062c11e1922ad0554853f44c605e4c4b48cef89cc/openlibx402_fastapi-0.1.1.tar.gz",
"platform": null,
"description": "# openlibx402-fastapi\n\nFastAPI middleware and decorators for adding X402 payment requirements to API endpoints.\n\n## Overview\n\nThe `openlibx402-fastapi` package makes it easy to add payment requirements to your FastAPI endpoints. Simply decorate endpoints with payment requirements, and the middleware handles payment verification automatically.\n\n## Features\n\n- Easy-to-use decorators for payment-required endpoints\n- Dependency injection support for accessing payment details\n- Automatic 402 Payment Required response generation\n- Configurable payment amounts and descriptions\n- Seamless integration with existing FastAPI applications\n\n## Installation\n\n```bash\npip install openlibx402-fastapi\n```\n\n## Quick Start\n\n### 1. Initialize X402 Configuration\n\n```python\nfrom fastapi import FastAPI\nfrom openlibx402_fastapi import X402Config, init_x402\nimport os\n\n# Initialize X402 configuration\nconfig = X402Config(\n payment_address=os.getenv(\"PAYMENT_WALLET_ADDRESS\"),\n token_mint=os.getenv(\"USDC_MINT_ADDRESS\"),\n network=\"solana-devnet\",\n rpc_url=os.getenv(\"SOLANA_RPC_URL\", \"https://api.devnet.solana.com\"),\n)\ninit_x402(config)\n\napp = FastAPI()\n```\n\n### 2. Add Payment Requirements with Decorator\n\n```python\nfrom fastapi import Request\nfrom openlibx402_fastapi import payment_required\n\n@app.get(\"/premium-data\")\n@payment_required(\n amount=\"0.10\",\n description=\"Access to premium market data\"\n)\nasync def get_premium_data(request: Request):\n \"\"\"Endpoint requires 0.10 USDC payment\"\"\"\n return {\n \"data\": \"This is premium content\",\n \"market_data\": {\"price\": 100.50, \"volume\": 1_000_000}\n }\n```\n\n### 3. Access Payment Details with Dependency Injection\n\n```python\nfrom fastapi import Depends\nfrom openlibx402_fastapi import verify_payment_factory\nfrom openlibx402_core import PaymentAuthorization\n\n@app.get(\"/expensive-data\")\nasync def get_expensive_data(\n payment: PaymentAuthorization = Depends(\n verify_payment_factory(\n amount=\"1.00\",\n description=\"Access to expensive AI model inference\"\n )\n )\n):\n \"\"\"Access payment details in your endpoint\"\"\"\n return {\n \"data\": \"AI-generated content\",\n \"payment_id\": payment.payment_id,\n \"amount_paid\": payment.actual_amount,\n \"transaction_hash\": payment.transaction_hash,\n }\n```\n\n## Usage Patterns\n\n### Pattern 1: Simple Decorator (Recommended)\n\nBest for endpoints that don't need to access payment details:\n\n```python\n@app.get(\"/data\")\n@payment_required(amount=\"0.05\", description=\"Data access\")\nasync def get_data(request: Request):\n return {\"data\": \"content\"}\n```\n\n### Pattern 2: Dependency Injection\n\nBest when you need to access payment details (transaction hash, amount, etc.):\n\n```python\n@app.get(\"/data\")\nasync def get_data(\n payment: PaymentAuthorization = Depends(\n verify_payment_factory(amount=\"0.05\", description=\"Data access\")\n )\n):\n return {\n \"data\": \"content\",\n \"payment_id\": payment.payment_id,\n \"tx_hash\": payment.transaction_hash,\n }\n```\n\n### Pattern 3: Dynamic Pricing\n\nAdjust payment requirements based on parameters:\n\n```python\n@app.get(\"/tiered-data/{tier}\")\n@payment_required(amount=\"0.05\", description=\"Tiered data access\")\nasync def get_tiered_data(request: Request, tier: str):\n \"\"\"Payment required regardless of tier\"\"\"\n tier_data = {\n \"basic\": {\"quality\": \"720p\"},\n \"premium\": {\"quality\": \"4K\"},\n }\n return {\"tier\": tier, \"data\": tier_data.get(tier)}\n```\n\n## Complete Example\n\n```python\nfrom fastapi import FastAPI, Depends, Request\nfrom openlibx402_fastapi import (\n payment_required,\n verify_payment_factory,\n X402Config,\n init_x402\n)\nfrom openlibx402_core import PaymentAuthorization\nimport os\n\n# Initialize X402\nconfig = X402Config(\n payment_address=os.getenv(\"PAYMENT_WALLET_ADDRESS\"),\n token_mint=os.getenv(\"USDC_MINT_ADDRESS\"),\n network=\"solana-devnet\",\n)\ninit_x402(config)\n\napp = FastAPI()\n\n@app.get(\"/\")\nasync def root():\n \"\"\"Public endpoint - no payment required\"\"\"\n return {\"message\": \"Welcome to the API\"}\n\n@app.get(\"/premium\")\n@payment_required(amount=\"0.10\", description=\"Premium data\")\nasync def premium(request: Request):\n \"\"\"Protected endpoint - payment required\"\"\"\n return {\"data\": \"premium content\"}\n\nif __name__ == \"__main__\":\n import uvicorn\n uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\n## Configuration\n\n### X402Config Options\n\n- `payment_address`: Your Solana wallet address for receiving payments\n- `token_mint`: Token mint address (e.g., USDC on Solana)\n- `network`: Network name (e.g., \"solana-devnet\", \"solana-mainnet\")\n- `rpc_url`: Solana RPC endpoint URL\n\n## Documentation\n\nFor complete API reference and guides, see:\n- [Documentation](https://openlibx402.github.io/docs)\n- [GitHub Repository](https://github.com/openlibx402/openlibx402)\n- [Full Example](https://github.com/openlibx402/openlibx402/tree/main/examples/python/fastapi-server)\n\n## Testing\n\n```bash\npytest tests/\n```\n\n## License\n\nMIT License - See [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2025 OpenLibx402 Contributors\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.",
"summary": "FastAPI middleware and decorators for X402 payment protocol",
"version": "0.1.1",
"project_urls": {
"Documentation": "https://openlibx402.github.io/docs",
"Homepage": "https://openlib.xyz",
"Repository": "https://github.com/openlibx402/openlibx402"
},
"split_keywords": [
"fastapi",
" middleware",
" openlibx402",
" payments",
" x402"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "a6bc24039f70b5af075023edb3c280a1185b919ebd1328ee4c69bb1f17844221",
"md5": "b44edd0fb04bcc484fc65591053e4dc9",
"sha256": "f2ae6ef20e6a7bf4cb90e0c7c0378e84f25545dfe416b3886acfc199e68802b0"
},
"downloads": -1,
"filename": "openlibx402_fastapi-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b44edd0fb04bcc484fc65591053e4dc9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 10379,
"upload_time": "2025-10-28T16:38:31",
"upload_time_iso_8601": "2025-10-28T16:38:31.975199Z",
"url": "https://files.pythonhosted.org/packages/a6/bc/24039f70b5af075023edb3c280a1185b919ebd1328ee4c69bb1f17844221/openlibx402_fastapi-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "490e35f27ae1feccd357b7e062c11e1922ad0554853f44c605e4c4b48cef89cc",
"md5": "8de8b744115c8b98486412178eaa2168",
"sha256": "fd6d60d635ecf733a82a63e99edf738c03590416baf4882b43723042d8954ffd"
},
"downloads": -1,
"filename": "openlibx402_fastapi-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "8de8b744115c8b98486412178eaa2168",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7419,
"upload_time": "2025-10-28T16:38:32",
"upload_time_iso_8601": "2025-10-28T16:38:32.832341Z",
"url": "https://files.pythonhosted.org/packages/49/0e/35f27ae1feccd357b7e062c11e1922ad0554853f44c605e4c4b48cef89cc/openlibx402_fastapi-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-28 16:38:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "openlibx402",
"github_project": "openlibx402",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "openlibx402-fastapi"
}