Need to generate Fibonacci numbers in your application? This guide covers everything you need to know about the Fibonacci sequence via API, including the formula, golden ratio connection, and implementation examples.
What is the Fibonacci Sequence?
The Fibonacci sequence is a series where each number is the sum of the two preceding ones. Starting from 0 and 1, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n > 1
The Golden Ratio Connection
The ratio of consecutive Fibonacci numbers approaches the golden ratio (phi): approximately 1.618033988749895. This ratio appears throughout nature, art, and architecture.
F(n)/F(n-1) approaches phi as n increases
phi = (1 + sqrt(5)) / 2 ≈ 1.618
Using the Fibonacci API
TinyFn provides a flexible endpoint for Fibonacci calculations:
GET https://api.tinyfn.io/v1/math/fibonacci
Headers: X-API-Key: your-api-key
{
"n": 10,
"value": 55,
"sequence": [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
}
Parameters
| Parameter | Type | Description |
|---|---|---|
n |
integer | Position in sequence (0-indexed) |
include_sequence |
boolean | Include all numbers up to n (default: true) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/fibonacci?n=10',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.value); // 55
console.log(data.sequence); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/fibonacci',
params={'n': 10},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['value']) # 55
cURL
curl "https://api.tinyfn.io/v1/math/fibonacci?n=10" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Design/Art: Golden ratio calculations for layouts
- Algorithm Practice: Classic computer science example
- Financial Analysis: Fibonacci retracements in trading
- Nature Simulations: Modeling natural patterns
- Education: Teaching sequences and recursion
Best Practices
- Handle large numbers: Fibonacci numbers grow quickly
- Cache results: Sequence values don't change
- Use iterative: For single values, avoid recursive overhead
- Consider precision: Large Fibonacci numbers may need big integers
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-math": {
"url": "https://api.tinyfn.io/mcp/math/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all math tools available via MCP in our Math MCP Tools for AI Agents guide.
Try the Fibonacci Sequence API
Get your free API key and start generating Fibonacci numbers in seconds.
Get Free API Key