How to Monetize Your Python API in 10 Lines
You built a Python function that does something valuable. Maybe it summarizes documents, scores leads, classifies images, or generates embeddings. It works. People want it. Now you want to monetize your Python function without spending three weeks integrating Stripe, building a usage dashboard, and writing billing logic that has nothing to do with your actual product. You want to add a price tag to your code the same way you add a type hint: one line, no drama.
That is exactly what nano-empire-tollbooth does. One decorator. One price. Ten lines of code. Your function starts earning.
The Problem with Traditional API Billing When You Monetize a Python Function
The typical path to monetize a Python function looks something like this: sign up for a payment provider, generate API keys, build a webhook endpoint, store customer subscription state in a database, write middleware that checks entitlements on every request, handle edge cases around failed payments, implement usage metering, and eventually expose all of this behind some kind of API gateway. You are now maintaining two products: the thing you actually built, and the billing system that lets people pay for it.
This is why most side-project APIs stay free forever. The activation energy required to go from "working endpoint" to "paid endpoint" is absurdly high relative to the value of the code itself. A function that took thirty minutes to write demands thirty hours of payment plumbing.
Managed API platforms solve part of this, but they force you onto their infrastructure, their pricing model, and their vendor lock-in. You lose control of your deployment and your margins. For a solo developer or a small team shipping a Python API, the overhead does not justify the return.
What if billing were a decorator?
Enter @monetize -- One Decorator to Monetize Your Python Function
The nano-empire-tollbooth package provides a single decorator called @monetize. You attach it to any Python function, set a price in USD, and the tollbooth handles the rest: usage tracking, free-tier gating, receipt generation, and upgrade prompts.
Install it from PyPI:
terminalpip install nano-empire-tollbooth
Here is the complete working example. Ten lines, including imports:
pythonfrom nano_empire_tollbooth import monetize
@monetize(price_usd=0.01)
def summarize(text: str) -> str:
"""Summarize any document. $0.01 per call."""
sentences = text.split(". ")
if len(sentences) <= 3:
return text
return ". ".join(sentences[:3]) + "."
result = summarize("Long document text goes here. It has many sentences. ...")
print(result)
That is it. The @monetize decorator wraps your function with a tollbooth. Every call is metered. The first 100 calls run in paper mode, which means full functionality with no payment required. After 100 calls, the tollbooth prompts the caller to upgrade. No database, no webhook, no API gateway. Your function is your product.
The decorator works with both sync and async functions. If your function is a coroutine, the tollbooth wraps it correctly:
python@monetize(price_usd=0.05)
async def classify_image(image_url: str) -> dict:
"""Classify an image. $0.05 per call."""
result = await run_classifier(image_url)
return {"label": result.label, "confidence": result.score}
The price, the metering, and the upgrade path are all defined at the point of the function itself. Your billing logic lives next to your business logic, not in a separate system.
How the Free Tier Works: 100 Calls, Paper Mode, Then Upgrade Prompt
When someone first calls your monetized function, the tollbooth starts in paper mode. Paper mode simulates the full payment flow -- usage is tracked, receipts are generated, toll records are written to a local ledger -- but no actual money changes hands. This serves two purposes.
First, it gives callers a genuine free trial. They can integrate your function into their workflow, test it at scale, and confirm it works before paying anything. The 100-call limit is generous enough for real evaluation but tight enough that production usage requires an upgrade.
Second, paper mode lets you, the developer, verify everything works before accepting real payments. You can inspect the ledger, review toll records, and validate that your pricing makes sense. The Tollbooth Simulator lets you test payment flows in a sandbox without touching Stripe at all.
After 100 calls, the tollbooth returns an upgrade prompt instead of executing the function. The prompt includes a direct link to your Stripe checkout page, which you configure when you upgrade to Tollbooth Pro. The caller does not need to create an account, manage API keys, or navigate a billing portal. They click, pay, and resume calling.
Usage state is stored locally by default. For production deployments, you can point the ledger at any persistent path:
pythonfrom nano_empire_tollbooth import monetize, TollboothConfig
config = TollboothConfig(
ledger_path="/var/data/tollbooth/ledger.json",
mode="paper" # or "live" after upgrading
)
@monetize(price_usd=0.02, config=config)
def score_lead(company: str) -> dict:
"""Score a sales lead. $0.02 per call."""
return run_scoring_model(company)
Going Live with Tollbooth Pro ($19/mo)
Paper mode is free, forever. You can run a monetized function with 100 free calls at no cost. When you are ready to accept real payments, upgrade to Tollbooth Pro for $19/month.
Tollbooth Pro unlocks:
- Live mode. Real Stripe charges on every call. Your function earns money the moment you flip the switch.
- Unlimited metered calls. No cap on paper-mode or live calls. The 100-call free tier applies to your callers, not to you.
- Receipt verification. Every transaction produces a cryptographically signed receipt that callers can verify independently. This matters for AI agents that need proof of work.
- Usage dashboard. View call volume, revenue, and caller activity from the Nano Empire dashboard.
- Priority support. Direct access to the engineering team for integration help.
Switching from paper mode to live mode is a one-line config change:
pythonconfig = TollboothConfig(mode="live")
Or set it via environment variable:
terminalexport TOLLBOOTH_MODE=live
There is no redeployment ceremony. Your function signature stays the same. Your callers do not need to change their integration. The only difference is that money starts flowing.
Why AI Agents Need This to Monetize Python Functions
The use case that makes @monetize inevitable is the rise of autonomous AI agents. Agents built on LangChain, CrewAI, AutoGen, and similar frameworks need to call external tools and APIs to complete tasks. These agents operate programmatically -- they discover tools, negotiate prices, pay for access, and verify results without any human in the loop.
Traditional billing systems were designed for humans. They assume someone will log into a dashboard, enter a credit card, and manage a subscription. AI agents cannot do this. They need a payment protocol that works at machine speed: discover the price, pay per call, receive a receipt, move on.
The @monetize decorator speaks this language natively. When an agent calls your function, the tollbooth returns structured pricing information in the response headers. The agent can inspect the price, decide whether the call is worth it, pay via the x402 protocol (HTTP 402 Payment Required), and receive a signed receipt proving the transaction occurred. No OAuth flow. No dashboard. No subscription management. Just function call, payment, result.
This is why the agent payment layer is becoming critical infrastructure. As more agents go into production, every valuable Python function becomes a potential revenue source. The developer who adds @monetize to their code today is positioned to capture revenue from an agent economy that barely exists yet but is growing exponentially.
Consider a concrete scenario. You build a function that extracts structured data from PDFs. A recruitment agent needs this capability to process resumes. With @monetize, your function shows up in the agent's tool registry with a clear price. The agent calls it 10,000 times per day at $0.02 per call. That is $200/day, $6,000/month, from a single function you wrote in an afternoon. No sales calls. No enterprise contracts. Just a decorator and a Stripe integration that runs itself.
Getting Started Today
The path from free function to paid function is short:
- Install the package:
pip install nano-empire-tollbooth - Add the decorator:
@monetize(price_usd=0.01)above your function - Test in paper mode: Run your function and verify the toll records look correct
- Try the simulator: Use the Tollbooth Simulator to test payment flows
- Upgrade when ready: Get Tollbooth Pro and flip to live mode
Your Python function already does something valuable. The only question is whether you are going to keep giving it away for free.
Ready to monetize your Python function?
Install from PyPI and start earning in under five minutes. Free tier included -- no credit card required to start.