Credit Rate Limiter
The Credit rate limiter provides a uniquely flexible consumption model where the backend service itself controls credit deduction and allocation through response headers. Unlike other rate limiters that operate purely at the proxy level, the Credit system enables sophisticated business logic to be implemented in your backend service.
How Credit Rate Limiting Works
The Credit rate limiter operates through a unique server-controlled model:
- an ApiCharge Subscriptions is granted a specific number of total credits
- Each API call initially consumes 1 credit by default (unless your backend service specifies otherwise)
- Your backend service returns an
apicharge_credits
header indicating how many credits to actually deduct or add - ApiCharge adjusts the credit balance based on your service's response header
- This enables dynamic credit control based on your business logic
This approach provides unprecedented flexibility in monetization strategies, allowing your backend service to implement complex pricing algorithms, promotional logic, and reward systems.
Configuration
To use the Credit rate limiter, add it to your route quote in appsettings.json
:
{
"ApiChargeProxy": {
"Routes": {
"premium-api": {
"ClusterId": "myCluster",
"Match": {
"Path": "/api/{**catch-all}"
},
"Quote": {
"ServerFundsRecipient": "YOUR_STELLAR_ADDRESS",
"PricingStrategy": {
"$type": "Basic",
"Duration": 3600,
"MicroUnitPrice": 1000000
},
"RateLimiterStrategies": [
{
"$type": "Credit",
"Credits": 100
}
]
}
}
}
}
}
Configuration Properties
Property | Type | Description |
---|---|---|
Credits |
Integer | The total number of credits assigned to the subscription |
Server-Controlled Credit Adjustment
The key feature of the Credit rate limiter is that your backend service controls credit consumption through the apicharge_credits
response header:
HTTP/1.1 200 OK
Content-Type: application/json
apicharge_credits: -5
{
"result": "Premium processing completed"
}
Credit Adjustment Values
- Negative values: Deduct credits (e.g.,
-5
removes 5 credits) - Positive values: Add credits (e.g.,
+10
rewards 10 credits) - Zero or no header: Uses default 1 credit deduction
Advanced Use Cases
Progressive Pricing Models
Implement sophisticated pricing where costs change based on usage patterns:
- First 10 calls free: Return
apicharge_credits: 0
- Standard rate: Return
apicharge_credits: -1
- Heavy usage premium: Return
apicharge_credits: -2
Reward-Based Systems
Reward users with credits for specific behaviors:
- Watching advertisements: Return
apicharge_credits: +5
- Completing surveys: Return
apicharge_credits: +10
- User referrals: Return
apicharge_credits: +25
Feature-Based Pricing
Charge different amounts based on features used:
- Basic operations: Return
apicharge_credits: -1
- Advanced processing: Return
apicharge_credits: -5
- Premium features: Return
apicharge_credits: -10
- Enterprise functionality: Return
apicharge_credits: -20
Promotional Campaigns
Implement time-based promotions and special offers:
- Happy hours: Free processing during specific times
- Weekend bonuses: Reward extra credits on weekends
- Holiday specials: Reduced credit costs during promotional periods
- New user incentives: Free credits for first-time users
Business Model Examples
Freemium API Service
- Free tier operations: Return
apicharge_credits: 0
- Premium features: Return
apicharge_credits: -10
- Loyalty rewards: Return
apicharge_credits: +2
for long-term users
Gaming API with Achievements
- Regular API calls: Standard deduction
apicharge_credits: -1
- Achievement unlocks: Reward credits
apicharge_credits: +50
- Premium content access: Higher cost
apicharge_credits: -25
Educational Platform
- Lesson completion: Reward
apicharge_credits: +5
- Quiz submission: Free
apicharge_credits: 0
- Certificate generation: Premium
apicharge_credits: -20
Combined with Other Rate Limiters
Credits work excellently with other rate limiters for comprehensive service tiers:
{
"RateLimiterStrategies": [
{
"$type": "Credit",
"Credits": 1000
},
{
"$type": "CallCount",
"Count": 500
},
{
"$type": "StreamRateDownload",
"BytesPerSecond": 1048576,
"WindowDurationInSeconds": 3600
}
]
}
Implementation Best Practices
Backend Service Guidelines
- Always validate: Ensure credit adjustments are appropriate for the operation performed
- Log adjustments: Track all credit modifications for audit and analytics
- Handle edge cases: Consider what happens with failed operations, partial results, etc.
- Security: Don't trust client input when determining credit costs
- Consistency: Maintain predictable credit costs for similar operations
Credit Strategy Design
- Resource correlation: Align credit costs with actual resource consumption
- User experience: Balance monetization with user satisfaction
- Clear documentation: Inform users about credit costs for different operations
- Gradual changes: Avoid sudden dramatic shifts in credit pricing
Error Handling
When credits are exhausted, ApiCharge returns a 429 status:
HTTP/1.1 429 Too Many Requests
Content-Type: application/problem+json
{
"type": "https://apicharge.error/EX017",
"title": "Insufficient Credits",
"status": 429,
"detail": "Your subscription has insufficient credits for this operation.",
"instance": "/apicharge/errors/12345"
}
Monitoring and Analytics
Track credit usage patterns to optimize your pricing strategy:
- Credit consumption rates: Monitor how quickly users exhaust credits
- Operation popularity: See which features justify higher credit costs
- Promotional effectiveness: Measure impact of credit rewards and discounts
- User behavior: Understand how credit pricing affects usage patterns
- Revenue optimization: Analyze credit pricing impact on overall monetization
Next Steps
Explore how Credits can be combined with other rate limiter types:
- Call Count - Control the total number of API calls
- Data Limits - Control data transfer amounts
- Stream Rate - Control bandwidth for streaming connections
- Rate Limiters Overview - Learn how to combine different rate limiters