{
  "type": "module",
  "source": "doc/api/api-proxyagent.md",
  "modules": [
    {
      "textRaw": "Class: ProxyAgent",
      "name": "class:_proxyagent",
      "type": "module",
      "desc": "<p>Extends: <code>undici.Dispatcher</code></p>\n<p>A Proxy Agent class that implements the Agent API. It allows the connection through proxy in a simple way.</p>",
      "signatures": [
        {
          "textRaw": "`new ProxyAgent([options])`",
          "name": "ProxyAgent",
          "type": "ctor",
          "params": [
            {
              "name": "options",
              "optional": true
            }
          ],
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>options</strong> <code>ProxyAgentOptions</code> (required) - It extends the <code>Agent</code> options.</li>\n</ul>\n<p>Returns: <code>ProxyAgent</code></p>",
          "modules": [
            {
              "textRaw": "Parameter: `ProxyAgentOptions`",
              "name": "parameter:_`proxyagentoptions`",
              "type": "module",
              "desc": "<p>Extends: <a href=\"/docs/docs/api/Agent.html#parameter-agentoptions\"><code>AgentOptions</code></a></p>\n<blockquote>\n<p>It ommits <code>AgentOptions#connect</code>.</p>\n</blockquote>\n<blockquote>\n<p><strong>Note:</strong> When <code>AgentOptions#connections</code> is set, and different from <code>0</code>, the non-standard <a href=\"https://udger.com/resources/http-request-headers-detail?header=Proxy-Connection\"><code>proxy-connection</code> header</a> will be set to <code>keep-alive</code> in the request.</p>\n</blockquote>\n<ul>\n<li><strong>uri</strong> <code>string | URL</code> (required) - The URI of the proxy server.  This can be provided as a string, as an instance of the URL class, or as an object with a <code>uri</code> property of type string.\nIf the <code>uri</code> is provided as a string or <code>uri</code> is an object with an <code>uri</code> property of type string, then it will be parsed into a <code>URL</code> object according to the <a href=\"https://url.spec.whatwg.org\">WHATWG URL Specification</a>.\nFor detailed information on the parsing process and potential validation errors, please refer to the <a href=\"https://url.spec.whatwg.org/#writing\">\"Writing\" section</a> of the WHATWG URL Specification.</li>\n<li><strong>token</strong> <code>string</code> (optional) - It can be passed by a string of token for authentication.</li>\n<li><strong>auth</strong> <code>string</code> (<strong>deprecated</strong>) - Use token.</li>\n<li><strong>clientFactory</strong> <code>(origin: URL, opts: Object) => Dispatcher</code> (optional) - Default: <code>(origin, opts) => new Pool(origin, opts)</code></li>\n<li><strong>requestTls</strong> <code>BuildOptions</code> (optional) - Options object passed when creating the underlying socket via the connector builder for the request. It extends from <a href=\"/docs/docs/api/Client.html#parameter-connectoptions\"><code>Client#ConnectOptions</code></a>.</li>\n<li><strong>proxyTls</strong> <code>BuildOptions</code> (optional) - Options object passed when creating the underlying socket via the connector builder for the proxy server. It extends from <a href=\"/docs/docs/api/Client.html#parameter-connectoptions\"><code>Client#ConnectOptions</code></a>.</li>\n<li><strong>proxyTunnel</strong> <code>boolean</code> (optional) - For connections involving secure protocols, Undici will always establish a tunnel via the HTTP2  CONNECT extension. If proxyTunnel is set to true, this will occur for unsecured proxy/endpoint connections as well. Currently, there is no way to facilitate HTTP1 IP tunneling as described in <a href=\"https://www.rfc-editor.org/rfc/rfc9484.html#name-http-11-request\">https://www.rfc-editor.org/rfc/rfc9484.html#name-http-11-request</a>. If proxyTunnel is set to false (the default), ProxyAgent connections where both the Proxy and Endpoint are unsecured will issue all requests to the Proxy, and prefix the endpoint request path with the endpoint origin address.</li>\n</ul>\n<p>Examples:</p>\n<pre><code class=\"language-js\">import { ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n// or\nconst proxyAgent = new ProxyAgent(new URL('my.proxy.server'))\n// or\nconst proxyAgent = new ProxyAgent({ uri: 'my.proxy.server' })\n// or\nconst proxyAgent = new ProxyAgent({\n  uri: new URL('my.proxy.server'),\n  proxyTls: {\n    signal: AbortSignal.timeout(1000)\n  }\n})\n</code></pre>",
              "modules": [
                {
                  "textRaw": "Example - Basic ProxyAgent instantiation",
                  "name": "example_-_basic_proxyagent_instantiation",
                  "type": "module",
                  "desc": "<p>This will instantiate the ProxyAgent. It will not do anything until registered as the agent to use with requests.</p>\n<pre><code class=\"language-js\">import { ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n</code></pre>",
                  "displayName": "Example - Basic ProxyAgent instantiation"
                },
                {
                  "textRaw": "Example - Basic Proxy Request with global agent dispatcher",
                  "name": "example_-_basic_proxy_request_with_global_agent_dispatcher",
                  "type": "module",
                  "desc": "<pre><code class=\"language-js\">import { setGlobalDispatcher, request, ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\nsetGlobalDispatcher(proxyAgent)\n\nconst { statusCode, body } = await request('http://localhost:3000/foo')\n\nconsole.log('response received', statusCode) // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // data foo\n}\n</code></pre>",
                  "displayName": "Example - Basic Proxy Request with global agent dispatcher"
                },
                {
                  "textRaw": "Example - Basic Proxy Request with local agent dispatcher",
                  "name": "example_-_basic_proxy_request_with_local_agent_dispatcher",
                  "type": "module",
                  "desc": "<pre><code class=\"language-js\">import { ProxyAgent, request } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n\nconst {\n  statusCode,\n  body\n} = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })\n\nconsole.log('response received', statusCode) // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // data foo\n}\n</code></pre>",
                  "displayName": "Example - Basic Proxy Request with local agent dispatcher"
                },
                {
                  "textRaw": "Example - Basic Proxy Request with authentication",
                  "name": "example_-_basic_proxy_request_with_authentication",
                  "type": "module",
                  "desc": "<pre><code class=\"language-js\">import { setGlobalDispatcher, request, ProxyAgent } from 'undici';\n\nconst proxyAgent = new ProxyAgent({\n  uri: 'my.proxy.server',\n  // token: 'Bearer xxxx'\n  token: `Basic ${Buffer.from('username:password').toString('base64')}`\n});\nsetGlobalDispatcher(proxyAgent);\n\nconst { statusCode, body } = await request('http://localhost:3000/foo');\n\nconsole.log('response received', statusCode); // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')); // data foo\n}\n</code></pre>",
                  "displayName": "Example - Basic Proxy Request with authentication"
                }
              ],
              "displayName": "Parameter: `ProxyAgentOptions`"
            }
          ],
          "methods": [
            {
              "textRaw": "`ProxyAgent.close()`",
              "name": "close",
              "type": "method",
              "signatures": [
                {
                  "params": []
                }
              ],
              "desc": "<p>Closes the proxy agent and waits for registered pools and clients to also close before resolving.</p>\n<p>Returns: <code>Promise&#x3C;void></code></p>",
              "modules": [
                {
                  "textRaw": "Example - clean up after tests are complete",
                  "name": "example_-_clean_up_after_tests_are_complete",
                  "type": "module",
                  "desc": "<pre><code class=\"language-js\">import { ProxyAgent, setGlobalDispatcher } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\nsetGlobalDispatcher(proxyAgent)\n\nawait proxyAgent.close()\n</code></pre>",
                  "displayName": "Example - clean up after tests are complete"
                }
              ]
            },
            {
              "textRaw": "`ProxyAgent.dispatch(options, handlers)`",
              "name": "dispatch",
              "type": "method",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "handlers"
                    }
                  ]
                }
              ],
              "desc": "<p>Implements <a href=\"/docs/docs/api/Agent.html#parameter-agentdispatchoptions\"><code>Agent.dispatch(options, handlers)</code></a>.</p>"
            },
            {
              "textRaw": "`ProxyAgent.request(options[, callback])`",
              "name": "request",
              "type": "method",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"/docs/docs/api/Dispatcher.html#dispatcherrequestoptions-callback\"><code>Dispatcher.request(options [, callback])</code></a>.</p>",
              "modules": [
                {
                  "textRaw": "Example - ProxyAgent with Fetch",
                  "name": "example_-_proxyagent_with_fetch",
                  "type": "module",
                  "desc": "<p>This example demonstrates how to use <code>fetch</code> with a proxy via <code>ProxyAgent</code>. It is particularly useful for scenarios requiring proxy tunneling.</p>\n<pre><code class=\"language-javascript\">import { ProxyAgent, fetch } from 'undici';\n\n// Define the ProxyAgent\nconst proxyAgent = new ProxyAgent('http://localhost:8000');\n\n// Make a GET request through the proxy\nconst response = await fetch('http://localhost:3000/foo', {\n  dispatcher: proxyAgent,\n  method: 'GET',\n});\n\nconsole.log('Response status:', response.status);\nconsole.log('Response data:', await response.text());\n</code></pre>\n<hr>",
                  "displayName": "Example - ProxyAgent with Fetch"
                },
                {
                  "textRaw": "Example - ProxyAgent with a Custom Proxy Server",
                  "name": "example_-_proxyagent_with_a_custom_proxy_server",
                  "type": "module",
                  "desc": "<p>This example shows how to create a custom proxy server and use it with <code>ProxyAgent</code>.</p>\n<pre><code class=\"language-javascript\">import * as http from 'node:http';\nimport { createProxy } from 'proxy';\nimport { ProxyAgent, fetch } from 'undici';\n\n// Create a proxy server\nconst proxyServer = createProxy(http.createServer());\nproxyServer.listen(8000, () => {\n  console.log('Proxy server running on port 8000');\n});\n\n// Define and use the ProxyAgent\nconst proxyAgent = new ProxyAgent('http://localhost:8000');\n\nconst response = await fetch('http://example.com', {\n  dispatcher: proxyAgent,\n  method: 'GET',\n});\n\nconsole.log('Response status:', response.status);\nconsole.log('Response data:', await response.text());\n</code></pre>\n<hr>",
                  "displayName": "Example - ProxyAgent with a Custom Proxy Server"
                },
                {
                  "textRaw": "Example - ProxyAgent with HTTPS Tunneling",
                  "name": "example_-_proxyagent_with_https_tunneling",
                  "type": "module",
                  "desc": "<p>This example demonstrates how to perform HTTPS tunneling using a proxy.</p>\n<pre><code class=\"language-javascript\">import { ProxyAgent, fetch } from 'undici';\n\n// Define a ProxyAgent for HTTPS proxy\nconst proxyAgent = new ProxyAgent('https://secure.proxy.server');\n\n// Make a request to an HTTPS endpoint via the proxy\nconst response = await fetch('https://secure.endpoint.com/api/data', {\n  dispatcher: proxyAgent,\n  method: 'GET',\n});\n\nconsole.log('Response status:', response.status);\nconsole.log('Response data:', await response.json());\n</code></pre>",
                  "displayName": "Example - ProxyAgent with HTTPS Tunneling"
                },
                {
                  "textRaw": "Example - ProxyAgent as a Global Dispatcher",
                  "name": "example_-_proxyagent_as_a_global_dispatcher",
                  "type": "module",
                  "desc": "<p><code>ProxyAgent</code> can be configured as a global dispatcher, making it available for all requests without explicitly passing it. This simplifies code and is useful when a single proxy configuration applies to all requests.</p>\n<pre><code class=\"language-javascript\">import { ProxyAgent, setGlobalDispatcher, fetch } from 'undici';\n\n// Define and configure the ProxyAgent\nconst proxyAgent = new ProxyAgent('http://localhost:8000');\nsetGlobalDispatcher(proxyAgent);\n\n// Make requests without specifying the dispatcher\nconst response = await fetch('http://example.com');\nconsole.log('Response status:', response.status);\nconsole.log('Response data:', await response.text());\n</code></pre>",
                  "displayName": "Example - ProxyAgent as a Global Dispatcher"
                }
              ]
            }
          ]
        }
      ],
      "displayName": "Class: ProxyAgent"
    }
  ]
}