Need to calculate profit margins in your application? This guide covers everything you need to know about margin calculations via API, including gross margin, net margin, and implementation examples in multiple languages.
What is Profit Margin?
Profit margin is a financial metric that shows the percentage of revenue that becomes profit after accounting for costs. It's calculated as (Revenue - Cost) / Revenue, expressed as a percentage.
A higher profit margin indicates a more profitable business. Margin calculations are essential for pricing strategies, financial analysis, and business planning.
Types of Profit Margins
Gross Margin
Revenue minus cost of goods sold (COGS), divided by revenue. Shows profitability before operating expenses.
Gross Margin = (Revenue - COGS) / Revenue × 100
Net Margin
Net income divided by revenue. Shows profitability after all expenses, taxes, and costs.
Margin vs Markup
Don't confuse margin with markup. Margin is profit as a percentage of selling price; markup is profit as a percentage of cost.
Using the Margin Calculator API
TinyFn provides a simple endpoint for margin calculations:
GET https://api.tinyfn.io/v1/math/margin
Headers: X-API-Key: your-api-key
{
"revenue": 100,
"cost": 60,
"profit": 40,
"margin_percentage": 40,
"markup_percentage": 66.67
}
Parameters
| Parameter | Type | Description |
|---|---|---|
revenue |
number | Selling price or revenue |
cost |
number | Cost of goods or services |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/margin?revenue=100&cost=60',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.margin_percentage); // 40
console.log(data.markup_percentage); // 66.67
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/margin',
params={'revenue': 100, 'cost': 60},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['margin_percentage']) # 40
cURL
curl "https://api.tinyfn.io/v1/math/margin?revenue=100&cost=60" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Pricing Strategy: Set prices to achieve target margins
- Financial Dashboards: Display profitability metrics
- Product Analysis: Compare margins across product lines
- E-commerce: Calculate profit on each sale
- Business Planning: Project profitability scenarios
Best Practices
- Include all costs: Factor in all relevant costs for accurate margins
- Track over time: Monitor margin trends to identify issues
- Benchmark: Compare margins against industry standards
- Separate by product: Calculate margins per product for insights
Try the Margin Calculator API
Get your free API key and start calculating profit margins in seconds.
Get Free API Key