URL Validation API: The Complete Guide

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

Note: The API can perform quick syntax checks or comprehensive validation including DNS resolution and HTTP checks.

Using the URL Validation API

TinyFn provides a comprehensive endpoint for URL validation:

API Request
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"
}
Response
{
  "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

  1. Validate early: Check URLs at the point of entry
  2. Be restrictive: Only allow necessary protocols
  3. Consider context: Different validation rules for different use cases
  4. 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

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key