Profit Margin Calculator API

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

Request Format

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.

Example Request (calculate margin)

{
  "cost": 75,
  "price": 100
}

Response Format

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).

Example Successful Response

{
  "cost": 75,
  "price": 100,
  "margin": 25.00
}

Error Responses

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."
}

Calculation Logic

Usage Notes