IP in Network Check API: The Complete Guide

Need to check if an IP address falls within a specific network range? This guide covers everything about IP-in-network testing via API, including the matching logic, multiple network checks, and implementation examples.

How Network Matching Works

To check if an IP belongs to a network, we compare the network portion of both addresses. The CIDR prefix length tells us how many bits to compare.

For example, in 192.168.1.0/24, the first 24 bits (192.168.1) must match. So 192.168.1.50 is in the network, but 192.168.2.50 is not.

The Algorithm

The check involves bitwise operations:

1. Convert IP to integer
2. Convert network address to integer
3. Create netmask from prefix length
4. Check: (IP & netmask) == (network & netmask)
Example: Is 192.168.1.100 in 192.168.1.0/24?
(3232235876 & 0xFFFFFF00) == (3232235776 & 0xFFFFFF00)
3232235776 == 3232235776 -> Yes!

Using the IP in Network API

TinyFn provides a simple endpoint for network membership checks:

API Request
POST https://api.tinyfn.io/v1/ip/in-network
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "ip": "192.168.1.100",
  "network": "192.168.1.0/24"
}
Response
{
  "ip": "192.168.1.100",
  "network": "192.168.1.0/24",
  "in_network": true,
  "network_address": "192.168.1.0",
  "broadcast_address": "192.168.1.255"
}

Check Multiple Networks

POST https://api.tinyfn.io/v1/ip/in-network
{
  "ip": "10.0.5.25",
  "networks": [
    "10.0.0.0/8",
    "192.168.0.0/16",
    "172.16.0.0/12"
  ]
}

Parameters

Parameter Type Description
ip string IP address to check (required)
network string Single network in CIDR notation
networks array Multiple networks to check against

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/ip/in-network',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      ip: '192.168.1.100',
      network: '192.168.1.0/24'
    })
  }
);
const { in_network } = await response.json();
console.log(in_network ? 'IP is in network' : 'IP is not in network');

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/ip/in-network',
    headers={'X-API-Key': 'your-api-key'},
    json={
        'ip': '192.168.1.100',
        'network': '192.168.1.0/24'
    }
)
result = response.json()
print('In network' if result['in_network'] else 'Not in network')

cURL

curl -X POST "https://api.tinyfn.io/v1/ip/in-network" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"ip": "192.168.1.100", "network": "192.168.1.0/24"}'

Common Use Cases

  • Access Control: Restrict access to specific IP ranges
  • Rate Limiting: Apply different limits based on IP origin
  • Geo-restrictions: Check if IPs are from certain regions
  • Network Segmentation: Verify traffic is from expected networks
  • Cloud Detection: Identify if requests come from cloud provider ranges

Best Practices

  1. Batch checks: Use the multiple networks option for efficiency
  2. Cache results: Cache network membership for frequently checked IPs
  3. Order matters: Check most likely networks first for early exits
  4. Handle edge cases: Consider broadcast and network addresses

Try the IP in Network API

Get your free API key and start checking network membership in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key