Need to validate geographic coordinates in your application? This guide covers everything about coordinate validation via API, including validation rules, common issues, and implementation examples in multiple languages.
Why Validate Coordinates?
Invalid coordinates can cause serious issues in your applications: mapping errors, crashed GPS systems, and incorrect geospatial calculations. Validation ensures your data is accurate before processing.
Common coordinate issues include:
- Out-of-range values (latitude > 90 or longitude > 180)
- Swapped latitude/longitude values
- Invalid formats or typos
- Coordinates in water or uninhabited areas (contextual validation)
Validation Rules
Geographic coordinates must follow these rules:
Latitude
- Range: -90 to +90 degrees
- -90° = South Pole
- +90° = North Pole
- 0° = Equator
Longitude
- Range: -180 to +180 degrees
- -180°/+180° = International Date Line
- 0° = Prime Meridian (Greenwich)
Using the Coordinate Validation API
TinyFn provides a comprehensive endpoint to validate coordinates:
POST https://api.tinyfn.io/v1/geo/validate
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"lat": 40.7128,
"lng": -74.0060
}
{
"valid": true,
"normalized": {
"lat": 40.7128,
"lng": -74.006
},
"location_type": "land",
"warnings": []
}
Parameters
| Parameter | Type | Description |
|---|---|---|
lat |
number | Latitude value to validate |
lng |
number | Longitude value to validate |
check_swap |
boolean | Check if lat/lng might be swapped (default: true) |
check_location |
boolean | Check if location is on land/water (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/validate',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
lat: 40.7128,
lng: -74.0060
})
}
);
const result = await response.json();
if (result.valid) {
console.log('Coordinates are valid!');
} else {
console.log('Invalid:', result.errors);
}
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/geo/validate',
headers={'X-API-Key': 'your-api-key'},
json={
'lat': 40.7128,
'lng': -74.0060
}
)
result = response.json()
if result['valid']:
print('Coordinates are valid!')
else:
print('Invalid:', result['errors'])
cURL
curl -X POST "https://api.tinyfn.io/v1/geo/validate" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"lat": 40.7128, "lng": -74.0060}'
Common Use Cases
- Form Validation: Validate user-entered coordinates before saving
- Data Import: Clean and validate bulk coordinate imports
- API Input Validation: Validate coordinates in your own API endpoints
- Data Quality: Audit existing coordinate data for errors
- ETL Pipelines: Validate coordinates during data transformation
Best Practices
- Validate early: Check coordinates at the point of entry
- Use swap detection: Catch common lat/lng swap errors
- Normalize precision: Round to appropriate decimal places
- Handle edge cases: Test with coordinates at poles and date line
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-geo": {
"url": "https://api.tinyfn.io/mcp/geo/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all geolocation tools available via MCP in our Geolocation MCP Tools for AI Agents guide.
Try the Coordinate Validation API
Get your free API key and start validating coordinates in seconds.
Get Free API Key