satsgate-proxy
Charge AI agents in Lightning and USDC on the same 402. Payments go straight to your wallet. 500 free unlocks a month.
A reverse proxy a merchant runs beside an existing HTTP API. The buyer hits the proxy. The proxy sells the request through Satsgate and forwards only a paid request to the origin. The merchant does not write paywall code.
The proxy is version 0.15.0. It works with core 0.20.3 or later and with gate 0.15.2 or later. Default mode: paywall talks to the Satsgate core paywall (challenge, verify, status, charge, and x402 verify/settle). mode: receipt does not call core and does not use an API key. It checks Satsgate-Receipt with the site secret and forwards a successful check to the origin. The proxy does not sell prepaid balance and does not call the facilitator. Top up on the core with POST /v1/topup/lightning or POST /v1/topup/usdc. On api.satsgate.org a USDC top-up is Base mainnet USDC (a self-hosted core needs SATSGATE_X402_CDP=1; see self-hosting). The hosted gate is a separate program and only sells receipts. Buyer USDC on a proxied paywall route goes to the merchant's verified payTo. In receipt mode the buyer already paid the gate.
Install
pip install satsgate-proxy
or
uvx --env-file .env satsgate-proxy --config satsgate-proxy.yaml
or
docker pull ghcr.io/mike-io-hash/satsgate-proxy:0.15.0
satsgate init --docker writes a compose file for that image. The minor tag is ghcr.io/mike-io-hash/satsgate-proxy:0.15.
USDC payments: Base network only
USDC payments use Base only. Ethereum mainnet, Polygon, Solana, Arbitrum, and any other chain are not accepted.
Merchants: payTo must be an address you control on Base. Prove it with the same EVM challenge the hosted gate uses. The USDC is paid to that address on the one network the route is in. Try on Base Sepolia with your agent first, then switch the site to live. Buyers: USDC sent on another network will not be accepted. A balance on Ethereum, Polygon, Solana, or Arbitrum does not pay this proxy.
Satsgate never custodies, receives, forwards, or refunds buyer funds. The USDC moves from the buyer to the merchant's payTo. The proxy does not hold a buyer wallet and does not run its own facilitator.
Run it
-
On the core, with the account key (sg_...), set the Lightning Address that should be paid. Then mint a restricted key. The proxy refuses any key that does not start with sgr_.
Resources are route:<name> or site. A prefix of route: covers every route-scoped route in the example config. Omit resource_prefix if you also use scope: site.
curl -s -X POST https://api.satsgate.org/v1/api-keys \
-H 'Content-Type: application/json' \
-H 'X-Api-Key: sg_...ACCOUNT_KEY...' \
-d '{"label":"shop-proxy","resource_prefix":"route:"}'
-
Generate one origin secret and give it to both the proxy and the origin. openssl rand -hex 32 is a good value. Shorter than 16 characters refuses to start. Shorter than 32 logs a warning.
-
Run satsgate init to write satsgate-proxy.yaml. Set upstream.base_url to the origin (no path, no userinfo). Put the sgr_ key and the origin secret in the environment, not in the file. Upstream credentials, if you inject any, are also environment variables and are never logged.
-
Add an origin check from snippets/ (FastAPI, Express, or nginx). Reject a missing or bad X-Satsgate-Signature. A client who can reach the origin directly must not skip the paywall. Stock Caddy does not verify HMAC; snippets/Caddyfile only shows a network gate in front of that check.
-
Start the proxy and do not publish the origin port.
export SATSGATE_API_KEY='sgr_...'
export SATSGATE_ORIGIN_SECRET="$(openssl rand -hex 32)"
uvx --env-file .env satsgate-proxy --config satsgate-proxy.yaml
Buyers use the proxy's published port. GET /healthz is the proxy ({"ok":true,"component":"satsgate-proxy","version":"0.15.0"}). It is not forwarded.
Startup logs key fp and 12 hex characters. That is the start of SHA-256 of the API key. It is not a prefix of the key, and the key itself is not written to the log.
The image listens as uid 65532.
Receipt mode
Set mode: receipt and a receipt block. There is no core block and no sgr_ key. The buyer pays https://gate.satsgate.org/<site><path>, then calls this proxy with Satsgate-Receipt. A 2xx from the origin commits one use. The origin still sees the HMAC in X-Satsgate-Signature.
mode: receipt
core:
base_url: https://api.satsgate.org
upstream:
base_url: https://origin.example
origin:
secret_env: SATSGATE_ORIGIN_SECRET
receipt:
site: my-shop
gate_base: https://gate.satsgate.org
store: /data/receipts.sqlite3
routes:
- path: /blog/post
price_sats: 21
core.base_url is still required by the config parser. Receipt mode does not call it and does not read SATSGATE_API_KEY. price_sats is stored on the route and is not charged here. The buyer already paid the gate.
proxy/snippets/fastapi_receipt.py is the five-line SDK wrapper. proxy/snippets/express_receipt.js checks the same test vectors in tests/fixtures/receipt_vectors.json with Node's crypto module and no npm dependency.
What a buyer sees
An unpaid request gets 402 and:
WWW-Authenticate: L402 macaroon="<standard base64>", invoice="<bolt11>"
macaroon comes before invoice, which is what lnget and Alby fetch402 parse. The JSON body has amount_sats, payment_hash, invoice, invoice_expires_at, max_uses, access_seconds (null when that side is unset), and a hint for the no-preimage path.
A paid retry is Authorization: L402 <macaroon>:<64-hex-preimage> (LSAT is accepted too). The proxy calls POST /v1/paywall/verify with consume=true.
Authorization: L402 <macaroon> with no preimage calls GET /v1/paywall/status/{payment_hash}?consume=true. That is the phone-wallet path: the wallet pays the invoice and does not return a preimage. While the invoice is unpaid the proxy answers 402 payment_pending with Retry-After and does not mint a second invoice. Pending does not consume a use.
A token for another route, a used-up bundle, or an expired pass becomes a fresh 402, not 401, so a standard client can pay again. The body includes reason (token_used_up, token_expired, resource mismatch, and the other core reject strings).
Core 429 (buyer_rate_limited, too_many_open_challenges_for_buyer, rate_limited, too_many_open_challenges) is passed through with Retry-After. A revoked key, a missing payee, an empty prepaid service balance (and no free unlocks left), or a resource outside the key prefix is 502 paywall_unavailable for the buyer. The core error code is logged on the proxy only.
Routes
First match wins. Anything that is not a free path or a route is 404. A path that matches with the wrong method is 405 and does not mint an invoice. OPTIONS is not special: list it, or put the path in free_paths, if a browser must call it.
A request path with a . or .. segment, also when percent-encoded (such as %2e%2e), a backslash, or an encoded slash or backslash (%2F, %5C) is 404 as well. It is checked before free paths and routes, so such a path never reaches the origin. This check was added in 0.15.0.
access |
Core policy |
Meaning |
per_request |
max_uses: 1 |
One payment, one request |
uses:N |
max_uses: N |
A bundle of N requests (N is 1 to 100000) |
pass:<seconds> |
access_seconds |
A time pass starting at settlement (1 to 604800 seconds) |
scope: route binds the macaroon to route:<name>. A token for one route does not open another. scope: site binds every site-scoped route to the resource site. Those routes must share price_sats and access, because one macaroon has one amount. Startup fails if they differ.
free_paths are forwarded with no challenge. Globs: * is one segment, a trailing /** is that prefix and anything under it (including the prefix). path_regex is a full match. A route has one of path or path_regex.
description is sent as the challenge memo, trimmed to 250 characters.
MCP servers
Added in 0.15.0. Earlier versions do not read MCP, so one price covers the whole route.
routes:
- name: tools
path: /mcp
methods: [POST, DELETE]
x402: true # optional, as today; applies to every paid tool
description: Market data tools # optional; memo fallback
mcp:
signal: in_band # optional; in_band (default) or http_402
default: free # required: free, or {price_sats, access[, description]}
tools: # optional
quote:
price_sats: 21
access: per_request
description: One market quote
deep_search:
price_sats: 100
access: uses:10
echo: free
An MCP route speaks Streamable HTTP. The proxy reads the JSON-RPC body, decides whether the call is free or priced, and only then talks to the core. signal is in_band, the default, or http_402. Any other value refuses to start.
default is required. It is the string free, or a price mapping with price_sats, access, and an optional description. Each key under tools is either free or the same kind of mapping. A missing default, a bad tool name, a route-level price_sats or access, a glob path, path_regex, a method other than POST and DELETE, or mode: receipt refuses to start.
What is free: every MCP message that is not a priced tools/call. That includes initialize, tools/list, ping, resources, prompts, a tool marked free, an unlisted tool when default is free, notifications, client responses, and DELETE. A batch is free when every tools/call in it is free. A batch that contains a priced tool is rejected and no invoice is minted.
What is charged: one tools/call whose name is a priced tool, or an unlisted name when default is a price. The resource is route:<route>:<tool>. A restricted key with resource_prefix: route: covers every one of those resources. The memo is the tool description, then the route description.
The proxy checks the request before it mints an invoice. A failed check is HTTP 400, Content-Type: application/json, Cache-Control: no-store, and a JSON-RPC error. data.error is the code word:
| Code word |
Meaning |
invalid_json |
The body is empty, not UTF-8, or not strict JSON. Duplicate keys are rejected. |
unsupported_content_type |
Content-Type is not application/json. |
accept_json_and_sse_required |
Accept does not list both application/json and text/event-stream. A wildcard does not count. |
invalid_message |
The JSON-RPC shape is wrong, a tools/call has no id, or a batch is not allowed for this protocol version. |
invalid_method |
The method is a disguised form of tools/call or of a method this proxy does not forward. |
invalid_tool_call |
A tools/call has no tool name. |
invalid_tool_name |
The name is a look-alike of a priced tool, or it cannot be used with a priced default. |
tasks_not_supported |
params.task is set. |
batch_with_paid_tool |
A batch contains a priced tools/call. |
duplicate_routing_header |
MCP-Protocol-Version, Mcp-Method, or Mcp-Name is repeated. |
header_mismatch |
Mcp-Method or Mcp-Name does not match the body, or a 2026-07-28 request is missing one of them. |
unsupported_protocol_version |
MCP-Protocol-Version is not one of the versions below. |
missing_request_envelope |
A 2026-07-28 request is missing params._meta. |
delete_body_not_allowed |
DELETE included a body. |
GET on an MCP route is 405 from the proxy, with Allow: DELETE, POST. DELETE with an empty body is free. subscriptions/listen and tasks/get, tasks/result, tasks/list, and tasks/cancel are not forwarded. A request gets HTTP 200 and JSON-RPC -32601. A notification gets HTTP 202 and an empty body.
On a free forward and on DELETE, the origin receives the original body bytes and these headers. A message that contains params._meta["x402/payment"] is the exception: that key is removed, _meta stays, and the message is sent as compact JSON.