Need to calculate VAT or sales tax in your application? This guide covers everything you need to know about tax calculations via API, including adding and removing VAT, different tax rates, and implementation examples in multiple languages.
What is VAT?
VAT (Value Added Tax) is a consumption tax added to products and services at each stage of production or distribution. Similar taxes include sales tax (US), GST (Australia, Canada, India), and consumption tax (Japan).
VAT rates vary by country and product category. Common rates include 20% (UK), 19% (Germany), and varying rates in the US by state.
VAT Calculation Types
Add VAT (Price Exclusive)
Add VAT to a net price to get the gross price.
Gross Price = Net Price × (1 + VAT Rate)
VAT Amount = Net Price × VAT Rate
Remove VAT (Price Inclusive)
Extract VAT from a gross price to get the net price.
Net Price = Gross Price / (1 + VAT Rate)
VAT Amount = Gross Price - Net Price
Using the VAT Calculator API
TinyFn provides a flexible endpoint for VAT calculations:
GET https://api.tinyfn.io/v1/math/vat
Headers: X-API-Key: your-api-key
{
"net_price": 100,
"vat_rate": 0.20,
"vat_amount": 20,
"gross_price": 120,
"calculation_type": "add"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
amount |
number | Price amount (net or gross) |
vat_rate |
number | VAT rate (e.g., 0.20 for 20%) |
type |
string | "add" (to net) or "remove" (from gross) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/vat?amount=100&vat_rate=0.20&type=add',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.vat_amount); // 20
console.log(data.gross_price); // 120
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/vat',
params={'amount': 100, 'vat_rate': 0.20, 'type': 'add'},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['gross_price']) # 120
cURL
curl "https://api.tinyfn.io/v1/math/vat?amount=100&vat_rate=0.20&type=add" \
-H "X-API-Key: your-api-key"
Common Use Cases
- E-commerce: Calculate taxes at checkout
- Invoicing: Generate invoices with proper tax breakdowns
- Accounting Software: Process transactions with tax calculations
- International Sales: Apply correct VAT rates by country
- Price Display: Show prices with and without tax
Best Practices
- Store rates correctly: Use decimal format (0.20 not 20)
- Handle multiple rates: Different products may have different rates
- Round appropriately: Follow local tax rounding rules
- Show breakdowns: Display net, VAT, and gross separately
Try the VAT Calculator API
Get your free API key and start calculating VAT in seconds.
Get Free API Key