The Profit Margin Calculator API allows you to compute the missing value among Cost, Price, and Margin (%), provided the other two are given. It is available at:
POST https://www.promptbox.cn/api/margin.php
The API accepts a POST request with a JSON body containing exactly 2 of the following 3 fields.
The missing one will be calculated.
| Field | Type | Description |
|---|---|---|
cost |
number | Production or purchase cost. |
price |
number | Selling price. |
margin |
number | Profit margin in percent (e.g., 25 = 25%). |
Note: Exactly 2 fields must be provided. The API will calculate the 3rd.
{
"cost": 75,
"price": 100
}
The API responds with a JSON object containing all 3 fields, including the calculated one.
| Field | Type | Description |
|---|---|---|
cost |
number | Cost (supplied or computed). |
price |
number | Price (supplied or computed). |
margin |
number | Margin percentage (supplied or computed, rounded to 2 decimals). |
{
"cost": 75,
"price": 100,
"margin": 25.00
}
If the request is invalid, the API will return an error JSON object with an explanatory message.
| Status Code | Description | Example |
|---|---|---|
| 400 | Invalid input (e.g., not exactly 2 fields, negative values, or invalid margin). | {
"error": "Please provide exactly two fields to calculate the third."
} |
| 405 | Invalid HTTP method (only POST is allowed). | {
"error": "Only POST method is allowed."
} |
cost = price * (1 - margin/100)price = cost / (1 - margin/100)margin = ((price - cost) / price) * 100