Math expression evaluator API
Evaluate math expressions safely: units (5 cm to inch), matrices, statistics, variables, big numbers or exact fractions, with formatted result and LaTeX.
Send an expression in expr (GET or POST) or a batch in expressions (up to 100, sharing variables: a = 5, then a * 2). The evaluator is mathjs with a restricted function set: arithmetic, powers and roots, logarithms, rounding, trigonometry, gcd / lcm, factorial and combinations, statistics (mean, median, std, variance, quantile…), matrices (det, inv, transpose, dot, cross), comparisons and logic, complex numbers, constants and units with conversion (100 km/h to m/s, 5 cm + 2 inch to mm, 10 kg * 9.81 m/s^2 to N). You can define functions (f(x) = x^2 + 1) and pass variables in scope (numbers, arrays or strings such as "42 km").
number=bignumber computes with 64 significant digits (precision 16–256), number=fraction gives exact fractions (1/3 + 1/6 → 1/2). Each result comes as a formatted string (digits significant digits, 15 by default), a JSON value, its type and the LaTeX of the expression for display.
Safety: import, createUnit, evaluate, parse, simplify, derivative and other meta functions are disabled, property access to constructors is blocked by mathjs, expressions are limited to 2,000 characters, matrices and ranges to 100,000 elements, factorial to 100,000 and exact powers to exponent 10,000; a batch stops after 2 seconds.
Parameters
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
expr | query|body | string | no | One expression (max 2,000 characters). | |
expressions | body | array | no | Batch of expressions evaluated in order with shared variables (max 100). | |
scope | body | object | no | Variables: {"price": 120000, "d": "42 km"}. | |
number | query|body | string | no | number | number (double), bignumber or fraction. |
precision | query|body | number | no | 64 | Significant digits of bignumber mode (16–256). |
digits | query|body | number | no | 15 | Significant digits of the formatted result. |
Example
curl "https://apibox.io.vn/v1/math-eval?expr=5%20cm%20%2B%202%20inch%20to%20mm" \
-H "Authorization: Bearer YOUR_API_KEY"const res = await fetch('https://apibox.io.vn/v1/math-eval?expr=5%20cm%20%2B%202%20inch%20to%20mm', {
method: 'GET',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
},
});
const data = await res.json();
console.log(data);import requests
res = requests.get(
"https://apibox.io.vn/v1/math-eval?expr=5%20cm%20%2B%202%20inch%20to%20mm",
headers={"Authorization": "Bearer YOUR_API_KEY"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/math-eval?expr=5%20cm%20%2B%202%20inch%20to%20mm');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY'],
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Response
{
"expression": "5 cm + 2 inch to mm",
"result": "100.8 mm",
"value": {
"value": 100.8,
"unit": "mm"
},
"type": "Unit",
"tex": "5~\\mathrm{cm}+2~\\mathrm{inch}\\rightarrow\\mathrm{mm}",
"number": "number",
"ms": 6
}
Use cases
- Let users type price or discount formulas in your app
- Convert units in a chat bot or a spreadsheet import
- Compute exact fractions or 64-digit results
- Render formulas as LaTeX next to their result
FAQ
Why does 0.1 + 0.2 show 0.3?
The formatted result uses 15 significant digits; value keeps the raw double (0.30000000000000004). Use number=bignumber for exact decimal arithmetic.
Can expressions run code?
No: only math functions are available, meta functions such as import or evaluate are disabled and JavaScript properties cannot be reached.
Which units are supported?
All built-in mathjs units: length, mass, time, speed, area, volume, temperature, force, energy, power, pressure, data (bit, byte) and SI prefixes. Currencies are not included.
Related APIs
UUID Generator
Generate up to 1,000 UUID v4, time-ordered UUID v7, ULID, Nano ID or random hex IDs per request. Secure randomness, optional uppercase.
⏱Cron Expression Parser
Validate a cron expression and get its next run times in any timezone, with a plain-English description. Supports 5, 6 and 7-field cron syntax.
🕒Time & Timezone Converter
Get the current time in any timezone, convert between timezones and Unix timestamps (seconds or ms), with UTC offset and weekday for up to 30 zones.