@tpmjs/tools-url-parse
Parse a URL into its components: protocol, hostname, port, pathname, search parameters, hash, and origin. Returns search parameters as a key-value object for easy access.
Test @tpmjs/tools-url-parse (urlParseTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-url-parsepnpm add @tpmjs/tools-url-parseyarn add @tpmjs/tools-url-parsebun add @tpmjs/tools-url-parsedeno add npm:@tpmjs/tools-url-parseimport { urlParseTool } from '@tpmjs/tools-url-parse';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { urlParseTool } from '@tpmjs/tools-url-parse';
const result = await generateText({
model: openai('gpt-4o'),
tools: { urlParseTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
urlstringThe URL string to parse (e.g., "https://example.com/path?key=value#section")
Schema extracted: 1/1/2026, 8:17:47 AM
Parse URLs into components using the Web URL API.
npm install @tpmjs/tools-url-parse # or pnpm add @tpmjs/tools-url-parse # or yarn add @tpmjs/tools-url-parse
import { urlParseTool } from '@tpmjs/tools-url-parse'; import { generateText } from 'ai'; const result = await generateText({ model: yourModel, tools: { urlParse: urlParseTool, }, prompt: 'Parse this URL and tell me the domain', });
import { urlParseTool } from '@tpmjs/tools-url-parse'; const result = await urlParseTool.execute({ url: 'https://example.com:8080/path/to/page?key=value&foo=bar#section', }); console.log(result); // { // protocol: 'https:', // hostname: 'example.com', // port: '8080', // pathname: '/path/to/page', // search: '?key=value&foo=bar', // hash: '#section', // origin: 'https://example.com:8080', // searchParams: { key: 'value', foo: 'bar' } // }
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | The URL string to parse |
{ protocol: string; // e.g., 'https:' hostname: string; // e.g., 'example.com' port: string; // e.g., '8080' (empty if default) pathname: string; // e.g., '/path/to/page' search: string; // e.g., '?key=value&foo=bar' hash: string; // e.g., '#section' origin: string; // e.g., 'https://example.com:8080' searchParams: Record<string, string>; // e.g., { key: 'value', foo: 'bar' } }
const result = await urlParseTool.execute({ url: 'https://www.example.com/page', }); console.log(result.protocol); // 'https:' console.log(result.hostname); // 'www.example.com' console.log(result.pathname); // '/page'
const result = await urlParseTool.execute({ url: 'http://localhost:3000/api/users', }); console.log(result.hostname); // 'localhost' console.log(result.port); // '3000' console.log(result.pathname); // '/api/users'
const result = await urlParseTool.execute({ url: 'https://search.example.com/?q=hello&lang=en&limit=10', }); console.log(result.search); // '?q=hello&lang=en&limit=10' console.log(result.searchParams); // { q: 'hello', lang: 'en', limit: '10' }
const result = await urlParseTool.execute({ url: 'https://docs.example.com/guide#installation', }); console.log(result.hash); // '#installation'
const result = await urlParseTool.execute({ url: 'https://user:pass@api.example.com:8443/v1/users?active=true&limit=50#results', }); console.log(result); // { // protocol: 'https:', // hostname: 'api.example.com', // port: '8443', // pathname: '/v1/users', // search: '?active=true&limit=50', // hash: '#results', // origin: 'https://api.example.com:8443', // searchParams: { active: 'true', limit: '50' } // }
// Note: Relative URLs need a base URL const result = await urlParseTool.execute({ url: new URL('/api/data', 'https://example.com').href, }); console.log(result.pathname); // '/api/data' console.log(result.hostname); // 'example.com'
const result = await urlParseTool.execute({ url: 'https://subdomain.example.com/path', }); console.log(result.hostname); // 'subdomain.example.com' console.log(result.origin); // 'https://subdomain.example.com'
const result = await urlParseTool.execute({ url: 'https://secure.example.com', }); const isSecure = result.protocol === 'https:'; console.log(isSecure); // true
const result = await urlParseTool.execute({ url: 'file:///Users/name/document.pdf', }); console.log(result.protocol); // 'file:' console.log(result.pathname); // '/Users/name/document.pdf'
const result = await urlParseTool.execute({ url: 'ftp://ftp.example.com/files/archive.zip', }); console.log(result.protocol); // 'ftp:' console.log(result.hostname); // 'ftp.example.com' console.log(result.pathname); // '/files/archive.zip'
const result = await urlParseTool.execute({ url: 'https://shop.example.com/products?category=electronics&sort=price', }); console.log(result.searchParams.category); // 'electronics' console.log(result.searchParams.sort); // 'price'
const result = await urlParseTool.execute({ url: 'https://example.com/page', }); console.log(result.port); // '' (empty string, default HTTPS port 443)
const result = await urlParseTool.execute({ url: 'https://example.com/search?q=hello%20world&tags=foo%2Cbar', }); console.log(result.searchParams.q); // 'hello world' (decoded) console.log(result.searchParams.tags); // 'foo,bar' (decoded)
const result = await urlParseTool.execute({ url: 'data:text/plain;base64,SGVsbG8gV29ybGQ=', }); console.log(result.protocol); // 'data:' console.log(result.pathname); // 'text/plain;base64,SGVsbG8gV29ybGQ='
const result = await urlParseTool.execute({ url: 'wss://api.example.com/socket', }); console.log(result.protocol); // 'wss:' console.log(result.hostname); // 'api.example.com'
| Component | Description | Example |
|---|---|---|
protocol | URL scheme with colon | https:, http:, ftp: |
hostname | Domain name or IP address | example.com, 192.168.1.1 |
port | Port number (empty if default) | 8080, 3000, '' |
pathname | Path part of URL | /path/to/page, / |
search | Query string with ? | ?key=value&foo=bar |
hash | Fragment identifier with # | #section, #top |
origin | Protocol + hostname + port | https://example.com:8080 |
searchParams | Query params as object | { key: 'value' } |
const result = await urlParseTool.execute({ url: userInputUrl }); if (result.protocol !== 'https:') { console.warn('URL is not using HTTPS'); }
const result = await urlParseTool.execute({ url: 'https://api.example.com/v2/users/123', }); const apiBase = result.origin; // 'https://api.example.com' const endpoint = result.pathname; // '/v2/users/123'
const result = await urlParseTool.execute({ url: 'https://example.com/items?page=2&limit=20', }); const page = parseInt(result.searchParams.page); // 2 const limit = parseInt(result.searchParams.limit); // 20
const result = await urlParseTool.execute({ url: originalUrl }); // Modify components const newUrl = `${result.protocol}//${result.hostname}/new-path${result.search}`;
const url1 = await urlParseTool.execute({ url: 'https://example.com/page1' }); const url2 = await urlParseTool.execute({ url: 'https://example.com/page2' }); const sameOrigin = url1.origin === url2.origin; // true
try { const result = await urlParseTool.execute({ url: 'not a valid url', }); } catch (error) { console.error(error.message); // "Invalid URL: Invalid URL" }
try { const result = await urlParseTool.execute({ url: '', }); } catch (error) { console.error(error.message); // "URL input cannot be empty" }
try { const result = await urlParseTool.execute({ url: null, }); } catch (error) { console.error(error.message); // "URL input must be a string" }
https: for security-sensitive operationsport, search, or hash are emptysearchParams are automatically URL-decodednew URL(relative, base))username:password) in URLs are not returnedjavascript: and data: protocols@tpmjs/tools-url-risk-heuristic for URL safety checksMIT
Downloads/month
99
Quality Score