Undici maintains a small set of global state that controls the default behavior of top-level functions likeDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
request, stream, and fetch. You can replace the global dispatcher to apply connection pooling, proxy routing, retry logic, or caching to every request made through undici without changing individual call sites.
The global dispatcher is stored under
Symbol.for('undici.globalDispatcher.2'). A legacy mirror is kept at Symbol.for('undici.globalDispatcher.1') via Dispatcher1Wrapper for compatibility with Node.js built-in fetch, which uses the older handler contract.setGlobalDispatcher(dispatcher)
Replaces the global dispatcher used by request, stream, pipeline,
connect, upgrade, and fetch when no per-call dispatcher option is
provided.
The dispatcher must be an object with a dispatch method — any Dispatcher
subclass qualifies (Agent, Pool, Client, ProxyAgent, etc.).
When set, undici also writes a Dispatcher1Wrapper-wrapped copy of the
dispatcher to Symbol.for('undici.globalDispatcher.1') so that Node.js
built-in fetch continues to work via the legacy handler contract.
Parameters
Any
Dispatcher instance. Must expose a dispatch method. Passing a value
without dispatch throws an InvalidArgumentError.Common patterns
- Agent with interceptors
- ProxyAgent
- EnvHttpProxyAgent
Caching + retry interceptors
Compatibility with Node.js built-in fetch
Undici stores the active dispatcher under two symbols to ensure compatibility:| Symbol | Consumer |
|---|---|
Symbol.for('undici.globalDispatcher.2') | undici request, stream, fetch, etc. |
Symbol.for('undici.globalDispatcher.1') | Node.js built-in fetch (legacy handler contract) |
setGlobalDispatcher writes to both automatically. If you need to expose a
custom dispatcher only to legacy consumers, wrap it manually:
Exposing a custom dispatcher to legacy consumers
getGlobalDispatcher()
Returns the current global dispatcher. The default value is a shared Agent
instance created at module load time.
Inspecting the global dispatcher
Return value
The active global dispatcher. Returns an
Agent instance unless overridden
with setGlobalDispatcher.setGlobalOrigin(origin)
Sets the base URL resolved against when fetch receives a relative URL path.
Once set, you can call fetch('/api/ping') instead of specifying the full URL.
Only http: and https: origins are accepted. Passing any other protocol
throws a TypeError.
Pass undefined to clear the global origin and revert to requiring absolute
URLs.
Parameters
The base origin to set. Must be an
http: or https: URL. Only the origin
portion (protocol + host + port) is used — any path, query, or fragment is
discarded.Pass undefined to reset the global origin. After resetting, relative URLs
passed to fetch throw a TypeError.Example — resetting the origin
Resetting the global origin
getGlobalOrigin()
Returns the currently configured global origin, or undefined if none has
been set.
Reading the global origin
Return value
The global origin as a
URL object, or undefined if not set.install()
Installs undici’s web API implementations onto globalThis, making them
available globally without explicit imports. This is useful for polyfilling
environments, ensuring consistent fetch behavior across Node.js versions, or
satisfying third-party libraries that expect standard browser globals.
After calling install(), all listed globals come from undici’s
implementation. They form a matched set, so FormData and fetch are
guaranteed to be compatible.
Installed globals
install() assigns the following properties on globalThis:
| Global | Type | Description |
|---|---|---|
fetch | function | WHATWG Fetch function |
Headers | class | HTTP headers container |
Response | class | HTTP response representation |
Request | class | HTTP request representation |
FormData | class | Multipart form data builder |
WebSocket | class | WebSocket client |
CloseEvent | class | WebSocket close event |
ErrorEvent | class | WebSocket error event |
MessageEvent | class | WebSocket/EventSource message event |
EventSource | class | Server-sent events client |
When to use install()
Polyfilling older Node.js versions
Polyfilling older Node.js versions
Node.js added built-in
fetch in v18 (unflagged in v21). If you need
undici’s implementation on older runtimes, or you want undici’s latest
features regardless of the Node.js version:Conditional install
Consistent FormData + fetch pairing
Consistent FormData + fetch pairing
The safest pattern when using Alternatively, import both directly from
FormData with fetch is to ensure both
come from the same implementation. install() guarantees this:After install(), FormData and fetch are always paired
undici without calling install():Direct import — no globals needed
Test environments
Test environments
When you need deterministic fetch behavior in tests regardless of the
Node.js version or environment:
Test setup file
Third-party library compatibility
Third-party library compatibility
Some libraries (e.g. SDKs that target both browser and Node.js) rely on
globally available
fetch, Headers, Request, and Response. Calling
install() satisfies those expectations without patching the libraries.