We've updated — New tools, dark mode, and an improved experience. 🎉

How to Encode URLs for API Requests

2026-03-15

URL encoding (percent encoding) converts special characters into a format that can be safely transmitted in URLs. When building API requests, query parameters with spaces, ampersands, or Unicode characters must be encoded to avoid breaking the URL structure.

How URL Encoding Works

Special characters are replaced with a percent sign followed by their hexadecimal ASCII value:

  • space%20 (or + in form data)
  • &%26
  • =%3D
  • /%2F

In JavaScript

// Encode a query parameter value
const query = encodeURIComponent('hello world & more');
// Result: hello%20world%20%26%20more

// Build full URL
const url = `https://api.example.com/search?q=${query}`;

// Encode a full URI (preserves :, /, ?)
const encoded = encodeURI('https://example.com/path with spaces');

Common Mistakes

  • Double encoding: Encoding an already-encoded string creates %2520 instead of %20. Always encode raw values, not already-encoded URLs.
  • Using encodeURI for parameters: encodeURI() preserves & and =. Use encodeURIComponent() for query parameter values.
  • Forgetting to decode: Server-side code should decode URL-encoded values before processing.

Encode URLs Online

Use our URL Encoder to encode strings for URLs, or URL Decoder to decode percent-encoded strings. For analyzing URL structure, try URL Parser which breaks URLs into protocol, host, path, query, and fragment. Check our URL Tools category for more utilities.

← Back to Blog