Fibonacci Sequence API: The Complete Guide

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
Fun Fact: Fibonacci numbers appear in nature: the spiral arrangement of leaves, flower petals, pinecones, and sunflower seeds.

Using the Fibonacci API

TinyFn provides a flexible endpoint for Fibonacci calculations:

API Request
GET https://api.tinyfn.io/v1/math/fibonacci
Headers: X-API-Key: your-api-key
Response
{
  "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

  1. Handle large numbers: Fibonacci numbers grow quickly
  2. Cache results: Sequence values don't change
  3. Use iterative: For single values, avoid recursive overhead
  4. 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

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key