Subnet Calculator API: The Complete Guide to Network Calculations

Need to calculate subnet ranges and network information? This guide covers everything you need to know about subnet calculations via API, including CIDR notation, network ranges, and implementation examples.

What is a Subnet?

A subnet (subnetwork) is a logical subdivision of an IP network. Subnetting allows you to divide a large network into smaller, manageable segments for better organization, security, and performance.

For example, 192.168.1.0/24 defines a network containing 256 addresses (192.168.1.0 to 192.168.1.255).

CIDR Notation

CIDR (Classless Inter-Domain Routing) notation represents IP ranges:

Format

IP/prefix - e.g., 192.168.1.0/24. The prefix indicates how many bits define the network portion.

Common Prefixes

/8 = 16.7M addresses, /16 = 65,536 addresses, /24 = 256 addresses, /32 = 1 address.

Subnet Mask

/24 = 255.255.255.0, /16 = 255.255.0.0, /8 = 255.0.0.0. The mask identifies network vs host bits.

Tip: For most home/small office networks, /24 provides 254 usable host addresses (256 - 2 for network and broadcast).

Using the Subnet Calculator API

TinyFn provides a simple endpoint to calculate subnet information:

API Request
GET https://api.tinyfn.io/v1/network/subnet?cidr=192.168.1.0/24
Headers: X-API-Key: your-api-key
Response
{
  "cidr": "192.168.1.0/24",
  "networkAddress": "192.168.1.0",
  "broadcastAddress": "192.168.1.255",
  "subnetMask": "255.255.255.0",
  "wildcardMask": "0.0.0.255",
  "firstHost": "192.168.1.1",
  "lastHost": "192.168.1.254",
  "totalHosts": 256,
  "usableHosts": 254,
  "prefix": 24,
  "ipVersion": 4
}

Parameters

Parameter Type Description
cidr string CIDR notation (e.g., 192.168.1.0/24)
ip string Alternative: IP address
mask string Alternative: subnet mask (use with ip)

Code Examples

JavaScript / Node.js

// Calculate subnet from CIDR
const response = await fetch(
  'https://api.tinyfn.io/v1/network/subnet?cidr=192.168.1.0/24',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`Network: ${data.networkAddress}`);    // 192.168.1.0
console.log(`Broadcast: ${data.broadcastAddress}`);// 192.168.1.255
console.log(`Usable hosts: ${data.usableHosts}`);  // 254
console.log(`First host: ${data.firstHost}`);      // 192.168.1.1
console.log(`Last host: ${data.lastHost}`);        // 192.168.1.254

Python

import requests

# Calculate subnet
response = requests.get(
    'https://api.tinyfn.io/v1/network/subnet',
    params={'cidr': '10.0.0.0/16'},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"Network: {data['networkAddress']}")      # 10.0.0.0
print(f"Usable hosts: {data['usableHosts']}")    # 65534
print(f"Subnet mask: {data['subnetMask']}")      # 255.255.0.0

# Check if IP is in subnet
ip_to_check = '10.0.50.100'
# Compare with firstHost/lastHost range

cURL

# Calculate subnet from CIDR
curl "https://api.tinyfn.io/v1/network/subnet?cidr=192.168.1.0/24" \
  -H "X-API-Key: your-api-key"

# Calculate from IP and mask
curl "https://api.tinyfn.io/v1/network/subnet?ip=192.168.1.100&mask=255.255.255.0" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Network Planning: Calculate address ranges for new networks
  • Firewall Rules: Determine network boundaries for ACLs
  • IP Allocation: Find available address ranges
  • Documentation: Generate network documentation automatically
  • Capacity Planning: Calculate how many hosts a subnet can support

Best Practices

  1. Plan for growth: Choose subnet sizes that allow for expansion
  2. Document CIDR ranges: Keep clear records of allocated subnets
  3. Use consistent sizing: Standardize subnet sizes where possible
  4. Reserve addresses: Keep some addresses for network infrastructure

Try the Subnet Calculator API

Get your free API key and start calculating subnets in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key