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)
(3232235876 & 0xFFFFFF00) == (3232235776 & 0xFFFFFF00)
3232235776 == 3232235776 -> Yes!
Using the IP in Network API
TinyFn provides a simple endpoint for network membership checks:
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"
}
{
"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
- Batch checks: Use the multiple networks option for efficiency
- Cache results: Cache network membership for frequently checked IPs
- Order matters: Check most likely networks first for early exits
- 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