Need to calculate discounts in your application? This guide covers everything you need to know about discount calculations via API, including percentage discounts, fixed discounts, and implementation examples in multiple languages.
What is a Discount Calculator?
A discount calculator determines the final price after applying a discount to an original price. It can handle percentage discounts (e.g., 20% off) or fixed amount discounts (e.g., $10 off), and calculate the total savings.
Discount calculators are essential for e-commerce platforms, retail applications, and any system that handles pricing and promotions.
Types of Discounts
The API supports multiple discount types:
Percentage Discount
A percentage off the original price. For example, 25% off $100 = $75 final price, $25 savings.
Fixed Amount Discount
A fixed dollar amount off the price. For example, $15 off $100 = $85 final price.
Stacked Discounts
Multiple discounts applied sequentially. The API can handle complex discount scenarios.
Using the Discount Calculator API
TinyFn provides a flexible endpoint for discount calculations:
GET https://api.tinyfn.io/v1/math/discount
Headers: X-API-Key: your-api-key
{
"original_price": 100,
"discount_type": "percentage",
"discount_value": 25,
"discount_amount": 25,
"final_price": 75,
"savings_percentage": 25
}
Parameters
| Parameter | Type | Description |
|---|---|---|
original_price |
number | Original price before discount |
discount_type |
string | "percentage" or "fixed" |
discount_value |
number | Discount amount (percentage or fixed) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/discount?original_price=100&discount_type=percentage&discount_value=25',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.final_price); // 75
console.log(data.discount_amount); // 25
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/discount',
params={
'original_price': 100,
'discount_type': 'percentage',
'discount_value': 25
},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['final_price']) # 75
cURL
curl "https://api.tinyfn.io/v1/math/discount?original_price=100&discount_type=percentage&discount_value=25" \
-H "X-API-Key: your-api-key"
Common Use Cases
- E-commerce Checkout: Apply promo codes and calculate final prices
- Sale Events: Display original and sale prices
- Coupon Systems: Validate and apply coupon discounts
- Price Comparison: Show savings compared to competitors
- Bulk Pricing: Calculate volume discounts
Best Practices
- Show savings: Always display how much the customer saves
- Validate limits: Ensure discounts don't exceed maximum allowed
- Handle precision: Use proper rounding for currency
- Display clearly: Show original price, discount, and final price
Try the Discount Calculator API
Get your free API key and start calculating discounts in seconds.
Get Free API Key