Toolbox-XToolbox-X

Generate Query Parameters

Generates a URL-encoded query string from an object, flattening nested fields and supporting arrays, booleans, and various primitive types.

generateQueryParams

The generateQueryParams function generates a URL-encoded query string from a parameter object. It automatically flattens nested objects, handles arrays by repeating keys, and filters out nullish or empty string values.

Function Signature

function generateQueryParams<T extends QueryObject>(params?: T): QueryString

Parameters

  • params (QueryObject, optional): The object containing query keys and values.

Return Value

  • QueryString: A URL-encoded string (e.g. "?key1=value1&key2=42"). If the input is empty or contains only nullish values, it returns an empty string "".

Example Usage

playground.ts

Notes

  • Nested fields are extracted to top-level keys. If nested keys collide, the last processed key overrides previous ones.
  • Array values generate repeated keys (e.g., ?key=a&key=b) rather than array bracket notations (?key[]=a).

Aliases

The following aliases can be used for the generateQueryParams function:

  • createQueryParams
  • formatQueryParams

Types

type Primitive = string | number | boolean | null | undefined;
type QueryObjectValue = Primitive | Primitive[] | QueryObject;
type QueryObject = { [key: string]: QueryObjectValue };
type QueryString = string;

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page