Need to validate URLs before processing them? This guide covers everything about URL validation via API, including format validation, protocol checking, and implementation examples.
Why Validate URLs?
Invalid URLs can cause application errors, security vulnerabilities, and poor user experiences. Validation helps ensure:
- URLs are syntactically correct
- Protocols are appropriate (http/https)
- Domains are valid
- No malicious content in URLs
Types of Validation
Syntax Validation
Checks if the URL follows the correct format: scheme://host/path?query#fragment
Protocol Validation
Verifies the scheme is allowed (http, https, ftp, etc.)
Domain Validation
Ensures the domain/host is properly formatted and can be resolved
Safety Validation
Checks for known malicious or phishing URLs
Using the URL Validation API
TinyFn provides a comprehensive endpoint for URL validation:
POST https://api.tinyfn.io/v1/url/validate
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"url": "https://example.com/path?query=value"
}
{
"valid": true,
"url": "https://example.com/path?query=value",
"components": {
"scheme": "https",
"host": "example.com",
"path": "/path",
"query": "query=value",
"fragment": null
},
"checks": {
"syntax": true,
"protocol_allowed": true,
"domain_valid": true,
"has_valid_tld": true
}
}
Parameters
| Parameter | Type | Description |
|---|---|---|
url |
string | URL to validate (required) |
allowed_protocols |
array | List of allowed protocols (default: ["http", "https"]) |
require_tld |
boolean | Require a valid TLD (default: true) |
check_dns |
boolean | Verify domain resolves (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/url/validate',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com/path'
})
}
);
const { valid, checks } = await response.json();
if (valid) {
console.log('URL is valid!');
} else {
console.log('Invalid URL:', checks);
}
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/url/validate',
headers={'X-API-Key': 'your-api-key'},
json={'url': 'https://example.com/path'}
)
result = response.json()
if result['valid']:
print('URL is valid!')
else:
print('Invalid URL:', result['checks'])
cURL
curl -X POST "https://api.tinyfn.io/v1/url/validate" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/path"}'
Common Use Cases
- Form Validation: Validate user-submitted URLs in forms
- Data Import: Validate URLs during bulk data imports
- Link Checking: Verify links before displaying or following
- API Security: Validate callback URLs and webhooks
- Content Moderation: Check URLs in user-generated content
Best Practices
- Validate early: Check URLs at the point of entry
- Be restrictive: Only allow necessary protocols
- Consider context: Different validation rules for different use cases
- Provide feedback: Tell users why a URL is invalid
Try the URL Validation API
Get your free API key and start validating URLs in seconds.
Get Free API Key