The ROI Calculator API provides a simple way to calculate key investment metrics based on a series of cash flows. It accepts an initial investment and subsequent yearly returns, along with a discount rate, to compute the Net Present Value (NPV), Internal Rate of Return (IRR), and Return on Investment (ROI). Additionally, it provides a qualitative rating of the investment.
The base URL for all API requests is:
www.promptbox.cn/api/roi.php
No authentication is required to use this API.
The API accepts POST requests with a JSON body.
/api/roi.php
| Header | Value | Required |
|---|---|---|
Content-Type |
application/json |
Yes |
The JSON body must contain the following parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
rate |
Number | Yes | The discount, interest, or inflation rate as a percentage. For example, for 5%, use 5. |
cashFlows |
Array of Numbers | Yes | An array of cash flows. The first element must be the initial investment (as a positive number). Subsequent elements represent the cash inflow (return) for each consecutive year. |
This example represents an initial investment of $500,000, followed by 8 years of $20,000 returns, and a final year return of $600,000 (representing the sale of the asset). The discount rate is 5%.
{
"rate": 5,
"cashFlows": [500000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 600000]
}
The API returns a JSON object. The HTTP status code indicates success or failure.
If the request is valid and the calculation is successful, the API will return an object containing the calculated metrics.
| Field | Type | Description |
|---|---|---|
NPV |
Number | The Net Present Value, rounded to two decimal places. This represents the total value of all future cash flows in today's dollars. |
IRR_percent |
Number | The Internal Rate of Return as a percentage, rounded to two decimal places. This is the discount rate at which the NPV becomes zero. |
ROI_percent |
Number | The real Return on Investment, calculated as (NPV / Initial Investment) * 100. |
Rating |
Object | A qualitative assessment of the investment. Contains rating and reason fields. |
↳ rating |
String | A categorical rating (e.g., "Good Investment", "Terrible Investment"). |
↳ reason |
String | An explanation for the given rating, providing context on the metrics. |
{
"NPV": 16203.49,
"IRR_percent": 5.36,
"ROI_percent": 3.24,
"Rating": {
"rating": "Poor Investment",
"reason": "A barely positive real return of about 0.36% over inflation. However, NPV is just 3.2% of your investment, which is relatively small."
}
}
If the request fails due to invalid input or the wrong HTTP method, the API will return an appropriate HTTP status code and a JSON object containing an error message.
POST (e.g., GET).Request is missing the cashFlows parameter.
{
"error": "Missing, invalid, or empty array parameter: 'cashFlows'."
}
A GET request was sent to the endpoint.
{
"error": "Only POST method is allowed."
}
Here is an example of how to call the API from the command line using cURL.
curl -X POST 'https://www.promptbox.cn/api/roi.php' \
-H 'Content-Type: application/json' \
-d '{
"rate": 5,
"cashFlows": [500000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 600000]
}'