Skip to main content

Proxy Configuration

The L402 Proxy allows you to monetize any API - yours or third-party - by creating a payment-gated reverse proxy.

Overview

The proxy sits between clients and target APIs:

Client → L402 Proxy → Target API

└─ Requires Lightning payment

Use cases:

  • Monetize your own APIs without modifying them
  • Resell third-party APIs with a markup
  • Create premium access to public APIs
  • Rate-limit expensive APIs via micropayments

Creating a Proxy

Via API

curl -X POST https://api.lightningenable.com/api/proxy \
-H "X-API-Key: your-merchant-api-key" \
-H "Content-Type: application/json" \
-d '{
"name": "Premium Weather API",
"targetBaseUrl": "https://api.weather.com/v1",
"defaultPriceSats": 10,
"description": "Weather data with Lightning payments"
}'

Response:

{
"proxyId": "premium-weather-api-a1b2",
"name": "Premium Weather API",
"targetBaseUrl": "https://api.weather.com/v1",
"defaultPriceSats": 10,
"isActive": true,
"createdAt": "2024-12-29T12:00:00Z"
}

Proxy Endpoint

Your proxy is now available at:

https://api.lightningenable.com/l402/proxy/{proxyId}/{path}

For example:

https://api.lightningenable.com/l402/proxy/premium-weather-api-a1b2/forecast?city=nyc

Configuration Options

Full Configuration

{
"name": "Premium Weather API",
"description": "Weather data via Lightning payments",
"targetBaseUrl": "https://api.weather.com/v1",
"defaultPriceSats": 10,
"isActive": true,
"authHeader": "X-Api-Key",
"authValue": "your-weather-api-key",
"timeout": 30,
"allowedMethods": ["GET", "POST"],
"allowedPaths": ["/forecast/*", "/current/*"],
"blockedPaths": ["/admin/*"]
}

Configuration Reference

FieldTypeRequiredDescription
namestringYesDisplay name for the proxy
targetBaseUrlstringYesBase URL of target API
defaultPriceSatsintYesDefault price per request
descriptionstringNoProxy description
isActiveboolNoEnable/disable proxy (default: true)
authHeaderstringNoHeader name for target API auth
authValuestringNoHeader value for target API auth
timeoutintNoRequest timeout in seconds (default: 30)
allowedMethodsarrayNoAllowed HTTP methods
allowedPathsarrayNoPath whitelist (glob patterns)
blockedPathsarrayNoPath blacklist (glob patterns)

Endpoint-Specific Pricing

Set different prices for different endpoints:

curl -X POST https://api.lightningenable.com/api/proxy/{proxyId}/pricing \
-H "X-API-Key: your-merchant-api-key" \
-H "Content-Type: application/json" \
-d '{
"pathPattern": "/forecast/7day",
"priceSats": 50,
"description": "7-day forecast (premium)"
}'

Multiple Price Tiers

# Add pricing for different endpoints
curl -X POST .../pricing -d '{"pathPattern": "/current/*", "priceSats": 5}'
curl -X POST .../pricing -d '{"pathPattern": "/forecast/1day", "priceSats": 10}'
curl -X POST .../pricing -d '{"pathPattern": "/forecast/7day", "priceSats": 50}'
curl -X POST .../pricing -d '{"pathPattern": "/historical/*", "priceSats": 100}'

Price Matching Priority

Prices are matched in order of specificity:

  1. Exact path match
  2. Glob pattern match (most specific first)
  3. Default proxy price

Using the Proxy

Request Flow

  1. Client requests proxy endpoint (no auth):
curl https://api.lightningenable.com/l402/proxy/premium-weather-api-a1b2/forecast?city=nyc
  1. Proxy returns 402 with invoice:
{
"error": "Payment Required",
"l402": {
"macaroon": "AgEL...",
"invoice": "lnbc100n1p...",
"amount_sats": 10
}
}
  1. Client pays invoice, gets preimage

  2. Client retries with L402 credential:

curl https://api.lightningenable.com/l402/proxy/premium-weather-api-a1b2/forecast?city=nyc \
-H "Authorization: L402 AgEL...:abc123..."
  1. Proxy forwards request to target, returns response:
{
"city": "New York",
"forecast": [...]
}

Proxy Management

List Your Proxies

curl https://api.lightningenable.com/api/proxy \
-H "X-API-Key: your-merchant-api-key"

Get Proxy Details

curl https://api.lightningenable.com/api/proxy/{proxyId} \
-H "X-API-Key: your-merchant-api-key"

Update Proxy

curl -X PUT https://api.lightningenable.com/api/proxy/{proxyId} \
-H "X-API-Key: your-merchant-api-key" \
-d '{"defaultPriceSats": 20}'

Delete Proxy

curl -X DELETE https://api.lightningenable.com/api/proxy/{proxyId} \
-H "X-API-Key: your-merchant-api-key"

Target API Authentication

Header-Based Auth

{
"authHeader": "Authorization",
"authValue": "Bearer your-api-token"
}

API Key Header

{
"authHeader": "X-Api-Key",
"authValue": "your-api-key"
}

Query Parameter Auth

For APIs requiring query parameter auth, include in target URL:

{
"targetBaseUrl": "https://api.example.com/v1?apikey=your-key"
}
note

Auth credentials are stored encrypted and never exposed to clients.

Analytics

Get Proxy Analytics

curl https://api.lightningenable.com/api/proxy/{proxyId}/analytics \
-H "X-API-Key: your-merchant-api-key"

Response:

{
"proxyId": "premium-weather-api-a1b2",
"period": "last_30_days",
"totalRequests": 5234,
"totalSatsEarned": 52340,
"topPaths": [
{ "path": "/forecast/7day", "requests": 2341, "sats": 23410 },
{ "path": "/current/conditions", "requests": 1892, "sats": 9460 }
]
}

Testing Proxy

Test Target Reachability

curl -X POST https://api.lightningenable.com/api/proxy/{proxyId}/test \
-H "X-API-Key: your-merchant-api-key"

Response:

{
"success": true,
"responseTime": 234,
"statusCode": 200
}

Test with Specific Path

curl -X POST .../test \
-d '{"testPath": "/forecast?city=nyc"}'

Example Configurations

OpenAI API Proxy

{
"name": "GPT-4 Proxy",
"targetBaseUrl": "https://api.openai.com/v1",
"defaultPriceSats": 100,
"authHeader": "Authorization",
"authValue": "Bearer sk-...",
"allowedPaths": ["/chat/completions"],
"allowedMethods": ["POST"]
}

Endpoint pricing:

[
{ "pathPattern": "/chat/completions", "priceSats": 500 }
]

Weather API Proxy

{
"name": "Weather Data",
"targetBaseUrl": "https://api.weatherapi.com/v1",
"defaultPriceSats": 5,
"authHeader": "key",
"authValue": "your-weather-key"
}

Stock Data Proxy

{
"name": "Stock Market Data",
"targetBaseUrl": "https://api.polygon.io/v2",
"defaultPriceSats": 10,
"authHeader": "Authorization",
"authValue": "Bearer your-polygon-key",
"allowedPaths": ["/aggs/*", "/ticker/*"]
}

Error Handling

Proxy Errors

ErrorCauseSolution
proxy_not_foundInvalid proxyIdCheck proxy ID
proxy_inactiveProxy disabledReactivate proxy
target_unreachableTarget API downCheck target URL
target_timeoutTarget too slowIncrease timeout
path_not_allowedPath blockedCheck allowedPaths

Client Errors

ErrorCauseSolution
402Payment requiredPay invoice
401Invalid L402Check credential format
403Token expiredGet new token

Best Practices

Security

  • Store target API credentials securely
  • Use allowedPaths to restrict access
  • Block sensitive paths (/admin, /internal)
  • Monitor usage for abuse

Pricing

  • Research target API costs
  • Add reasonable markup (20-50%)
  • Consider volume discounts
  • Price based on value, not cost

Reliability

  • Set appropriate timeouts
  • Handle target API errors gracefully
  • Monitor target API availability
  • Have fallback targets if possible

Next Steps