AnvilJSON utilities

Anvil › Docs › /v1/percent

Percentage calculator API: of, is, change, apply, reverse

Five everyday percentage questions in one endpoint. Ask what 20% of 50 is, what percent 15 is of 60, how much 80→100 changed, what 200 becomes after +15%, or what the original was before a markup.

In one line: GET /v1/percent?mode=of&percent=20&of=50 returns JSON. No key, CORS on, 60 requests a minute.
GET

/v1/percent

Percentage calculator: of, is, change, apply, reverse. Change the values and press Send request to call the live API.

Parameters

NameTypeWhat it doesDefault
mode requiredstringof | is | change | apply | reverse—
percentstringPercent value (modes of / apply / reverse); trailing % ok—
ofstringWhole amount (mode=of)—
partstringPart amount (mode=is)—
wholestringWhole amount (mode=is)—
fromstringStart value (mode=change)—
tostringEnd value (mode=change)—
valuestringStarting value (mode=apply)—
resultstringValue after the change (mode=reverse)—

Example

Request:

GET https://api.oddlathe.com/v1/percent?mode=of&percent=20&of=50

Response (generated by the real code when these docs were last built; random or time-based values will differ for you):

{
 "mode": "of",
 "percent": 20,
 "of": 50,
 "result": 10,
 "formula": "20% of 50 = 10"
}

Code examples

curl
curl 'https://api.oddlathe.com/v1/percent?mode=of&percent=20&of=50'
JavaScript (browser or Node 18+)
const params = new URLSearchParams({
  mode: "of",
  percent: "20",
  of: "50"
});
const res = await fetch(`https://api.oddlathe.com/v1/percent?${params}`);
if (!res.ok) throw new Error((await res.json()).error.message);
const data = await res.json();
console.log(data);
Python (standard library only)
import json, urllib.parse, urllib.request

params = urllib.parse.urlencode({"mode": "of", "percent": "20", "of": "50"})
url = "https://api.oddlathe.com/v1/percent?" + params
# Send a User-Agent: the edge network refuses Python's default one.
req = urllib.request.Request(url, headers={"User-Agent": "my-script/1.0"})
with urllib.request.urlopen(req, timeout=10) as r:
    data = json.load(r)
print(data)

When it's useful

  • Checkout / invoice scripts that need markups and discounts without a spreadsheet
  • Normalising "was £80, now £100" into a signed percent change for reports
  • Recovering a pre-tax or pre-markup price from the final figure
  • Teaching the difference between "20% of" and "percent change"

Good to know

  • mode is required: of, is, change, apply or reverse.
  • mode=of needs percent and of (the whole). Returns result.
  • mode=is needs part and whole. Returns percent. whole must not be zero.
  • mode=change needs from and to. Returns percent_change, absolute_change and direction. Percent change is (to − from) ÷ from × 100, so 80→100 is +25%. from must not be zero.
  • mode=apply needs value and percent (positive increases, negative decreases). Returns result.
  • mode=reverse needs result (the value after the change) and percent. Returns original. A −100% change cannot be reversed.
  • A trailing % on number params is accepted and stripped. Pure maths on the query string; nothing is stored.
  • Browser UI: Tooltern percentage calculator (same maths, nothing leaves your device).

Five percentage questions: what is X% of Y, X is what % of Y, percent change from A to B, apply ±% to a value, or recover the original before a % change. Pure maths; nothing is stored.

Errors

Errors are JSON like {"error": {"code": "…", "message": "…"}}. The ones this endpoint can return:

  • missing_param (400): a required parameter is missing or empty
  • bad_param (400): a value is the wrong type or out of range (the message says which)
  • rate_limited (429): over 60 requests a minute; wait for Retry-After seconds

Questions

Why is 80→100 a 25% increase, not 20%?

Percent change is relative to the starting value: (100 − 80) ÷ 80 = 0.25. The 20-point gap is 20% of 100, which answers a different question.

Can I undo a −100% discount?

No. Multiplying by zero leaves no information about the original, so reverse returns bad_param.

Is anything stored?

Never. Pure maths on the query string.

Related endpoints

Prefer a page to an API?

Published · OpenAPI 3.1 spec · Limits and privacy