Examples
Code examples for common use cases
Practical examples for integrating the CPF API into your applications.
JavaScript / TypeScript
Calculate Monthly Contribution
async function (: number, ?: number) {
const = await (
"https://simplycpf.com/api/cpf/calculate",
{
: "POST",
: { "Content-Type": "application/json" },
: .({ , }),
}
);
if (!.) {
const = await .();
throw new (.error);
}
return .();
}
// Usage
const = await (5000, 30);
.(`Take-home pay: $${.afterCpfContribution}`);Batch Processing
async function (: number[]) {
const = .(() => ({ }));
const = await (
"https://simplycpf.com/api/cpf/calculate/batch",
{
: "POST",
: { "Content-Type": "application/json" },
: .({ }),
}
);
return .();
}
// Calculate for multiple income levels
const = await ([4000, 5000, 6000, 7000, 8000]);Get Age-Appropriate Rates
async function (: number) {
const = await (
`https://simplycpf.com/api/cpf/age-group/find?age=${}`
);
return .();
}
const = await (45);
.(`Employee rate: ${.contributionRate.employee * 100}%`);Python
Basic Calculation
import requests
def calculate_cpf(income: float, age: int = None) -> dict:
payload = {"income": income}
if age:
payload["age"] = age
response = requests.post(
"https://simplycpf.com/api/cpf/calculate",
json=payload
)
response.raise_for_status()
return response.json()
# Usage
result = calculate_cpf(5000, age=30)
print(f"Employee contribution: ${result['contribution']['employee']}")
print(f"Employer contribution: ${result['contribution']['employer']}")Multi-Year Projection
import requests
def project_cpf(income: float, age: int, years: int) -> dict:
response = requests.post(
"https://simplycpf.com/api/cpf/projection",
json={"income": income, "age": age, "years": years}
)
response.raise_for_status()
return response.json()
# Project 10 years of contributions
projection = project_cpf(6000, 30, 10)
for year_data in projection["projections"]:
print(f"Year {year_data['year']}: "
f"Cumulative total: ${year_data['cumulative']['totalContribution']:,.2f}")Compare Investment Returns
import requests
def compare_investments(principal: float, years: int, scenarios: list) -> dict:
response = requests.post(
"https://simplycpf.com/api/cpf/investment-comparison",
json={
"principal": principal,
"years": years,
"scenarios": scenarios
}
)
response.raise_for_status()
return response.json()
# Compare different investment options
result = compare_investments(
principal=100000,
years=20,
scenarios=[
{"name": "CPF OA (2.5%)", "rate": 2.5},
{"name": "CPF SA (4.0%)", "rate": 4.0},
{"name": "Index Fund (7%)", "rate": 7.0}
]
)
for r in result["results"]:
print(f"{r['name']}: ${r['finalValue']:,.2f} (+{r['growthPercentage']:.1f}%)")React Hook
import { , } from "react";
interface CpfResult {
: {
: number;
: number;
: number;
};
: {
: number;
: number;
: number;
};
: number;
}
export function () {
const [, ] = <CpfResult | null>(null);
const [, ] = (false);
const [, ] = <string | null>(null);
const = (async (: number, ?: number) => {
(true);
(null);
try {
const = await (
"https://simplycpf.com/api/cpf/calculate",
{
: "POST",
: { "Content-Type": "application/json" },
: .({ , }),
}
);
if (!.) {
const = await .();
throw new (.error);
}
const = await .();
();
} catch () {
( instanceof ? . : "An error occurred");
} finally {
(false);
}
}, []);
return { , , , };
}