URL Parser API: The Complete Guide to URL Component Extraction

Need to extract components from URLs? This guide covers everything you need to know about URL parsing via API, including all URL components, query parameter handling, and implementation examples.

What is URL Parsing?

URL parsing is the process of breaking down a URL into its constituent components: protocol, host, port, path, query string, and fragment. This allows you to work with individual parts without complex string manipulation.

For example, https://example.com:8080/path?key=value#section contains protocol, host, port, path, query, and fragment components.

URL Components

A complete URL can contain many parts:

Protocol/Scheme

http, https, ftp, etc. Defines how to access the resource.

Host and Port

Domain name or IP address, optionally with port number. example.com:8080.

Path

The resource location on the server. /users/profile/123.

Query String

Key-value parameters after ?. page=1&sort=name.

Fragment/Hash

Client-side anchor after #. #section-2.

Note: The API automatically decodes URL-encoded characters and parses nested query parameters.

Using the URL Parser API

TinyFn provides a simple endpoint to parse URLs:

API Request
GET https://api.tinyfn.io/v1/parse/url?url=https://example.com:8080/path/to/resource?page=1&sort=name#section
Headers: X-API-Key: your-api-key
Response
{
  "url": "https://example.com:8080/path/to/resource?page=1&sort=name#section",
  "protocol": "https:",
  "host": "example.com:8080",
  "hostname": "example.com",
  "port": "8080",
  "pathname": "/path/to/resource",
  "search": "?page=1&sort=name",
  "hash": "#section",
  "query": {
    "page": "1",
    "sort": "name"
  },
  "origin": "https://example.com:8080"
}

Parameters

Parameter Type Description
url string The URL to parse (URL-encoded)
decodeQuery boolean Decode query parameter values (default: true)

Code Examples

JavaScript / Node.js

const url = 'https://shop.example.com/products?category=electronics&page=2&sort=price';

const response = await fetch(
  `https://api.tinyfn.io/v1/parse/url?url=${encodeURIComponent(url)}`,
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const parsed = await response.json();
console.log(parsed.hostname);      // shop.example.com
console.log(parsed.pathname);      // /products
console.log(parsed.query.category); // electronics
console.log(parsed.query.page);     // 2

// Check if HTTPS
if (parsed.protocol === 'https:') {
  console.log('Secure connection');
}

Python

import requests
from urllib.parse import quote

url = 'https://api.example.com/v1/users?id=123&fields=name,email'

response = requests.get(
    'https://api.tinyfn.io/v1/parse/url',
    params={'url': url},
    headers={'X-API-Key': 'your-api-key'}
)
parsed = response.json()
print(parsed['hostname'])   # api.example.com
print(parsed['pathname'])   # /v1/users
print(parsed['query']['id'])     # 123
print(parsed['query']['fields']) # name,email

cURL

# Parse a URL (note: URL must be URL-encoded)
curl "https://api.tinyfn.io/v1/parse/url?url=https%3A%2F%2Fexample.com%2Fpath%3Fkey%3Dvalue" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Analytics: Extract UTM parameters from marketing URLs
  • Routing: Parse pathname for custom routing logic
  • Validation: Verify URLs before redirects or fetch operations
  • Link Analysis: Extract domains from user-submitted links
  • API Clients: Build URLs from parsed components

Best Practices

  1. Always URL-encode: Encode URLs before passing as query parameters
  2. Validate protocols: Check for http/https to prevent security issues
  3. Handle missing components: Not all URLs have ports, queries, or fragments
  4. Decode before comparison: Ensure consistent handling of encoded characters

Try the URL Parser API

Get your free API key and start parsing 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