{"id":26555257,"url":"https://github.com/luminati-io/node-js-user-agent","last_synced_at":"2026-05-18T03:11:50.790Z","repository":{"id":283784013,"uuid":"949364042","full_name":"luminati-io/node-js-user-agent","owner":"luminati-io","description":"Set, change, and rotate user agents in Node.js to evade anti-bot detection and improve web scraping success.","archived":false,"fork":false,"pushed_at":"2025-03-16T09:41:13.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-10T17:19:52.185Z","etag":null,"topics":["fetch-api","get-request","javascript","node-js","node-js-web-scraping","user-agent","user-agent-rotation","web-scraping"],"latest_commit_sha":null,"homepage":"https://brightdata.com/blog/web-data/node-js-user-agent","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luminati-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-16T09:38:24.000Z","updated_at":"2025-03-16T09:51:22.000Z","dependencies_parsed_at":"2025-03-22T07:02:08.697Z","dependency_job_id":"2d6c359b-98fd-4b26-aa7f-cb4b23c34303","html_url":"https://github.com/luminati-io/node-js-user-agent","commit_stats":null,"previous_names":["luminati-io/node-js-user-agent"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/luminati-io/node-js-user-agent","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luminati-io%2Fnode-js-user-agent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luminati-io%2Fnode-js-user-agent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luminati-io%2Fnode-js-user-agent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luminati-io%2Fnode-js-user-agent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luminati-io","download_url":"https://codeload.github.com/luminati-io/node-js-user-agent/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luminati-io%2Fnode-js-user-agent/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278698809,"owners_count":26030398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch-api","get-request","javascript","node-js","node-js-web-scraping","user-agent","user-agent-rotation","web-scraping"],"created_at":"2025-03-22T10:25:41.523Z","updated_at":"2025-10-06T23:46:00.388Z","avatar_url":"https://github.com/luminati-io.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Setting and Changing User Agent in Node.js\n\n[![Promo](https://github.com/luminati-io/LinkedIn-Scraper/raw/main/Proxies%20and%20scrapers%20GitHub%20bonus%20banner.png)](https://brightdata.com/) \n\nThis guide explains hot to set the `User-Agent` header with Node.js and implement user agent rotation to evade anti-bot detection while [scraping with node.js](https://brightdata.com/blog/how-tos/web-scraping-with-node-js):\n\n- [How to Change the Node.js User Agent Using the Fetch API](#how-to-change-the-nodejs-user-agent-using-the-fetch-api)\n  - [Set a User Agent Locally](#set-a-user-agent-locally)\n  - [Set a User Agent Globally](#set-a-user-agent-globally)\n- [Implement User Agent Rotation in Node.js](#implement-user-agent-rotation-in-nodejs)\n  - [Step 1: Retrieve a List of User Agents](#step-1-retrieve-a-list-of-user-agents)\n  - [Step 2: Randomly Pick a User Agent](#step-2-randomly-pick-a-user-agent)\n  - [Step 3: Make the HTTP Request with a Random User Agent](#step-3-make-the-http-request-with-a-random-user-agent)\n  - [Step 4: Put It All Together](#step-4-put-it-all-together)\n\n## Why Setting a User Agent Is So Important\n\nThe [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) header is a string that identifies the client making an HTTP request. It usually contains details about the browser, application, operating system, and system architecture.\n\nFor example, here is the user agent string set by Chrome when making a request:\n\n```\nMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\n```\n\nBelow is a breakdown of the components in this user agent string:\n\n* `Mozilla/5.0`: Originally used to denote compatibility with Mozilla browsers, this prefix is now added for compatibility purposes.\n* `Windows NT 10.0; Win64; x64:` Indicates the operating system (`Windows NT 10.0`), platform (`Win64`), and system architecture (`x64`).\n* `AppleWebKit/537.36`: Refers to the browser engine that Chrome uses.\n* (`KHTML, like Gecko`): Shows compatibility with the KHTML and Gecko layout engines.\n* `Chrome/127.0.0.0`: Specifies the browser name and version.\n* `Safari/537.36`: Indicates compatibility with Safari.\n\nThe `User-Agent` header helps determine whether a request comes from a trusted browser or automated software.\n\nWeb scraping bots often use default or non-browser user agents, making them easy targets for anti-bot systems. These systems analyze the `User-Agent` header to distinguish real users from bots. Learn more about [user agents for web scraping](https://brightdata.com/blog/how-tos/user-agents-for-web-scraping-101).\n\n## What Is the Node.js Default User Agent?\n\nSince version 18, Node.js includes [`fetch()`](https://nodejs.org/dist/latest/docs/api/globals.html) as a built-in implementation of the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). It is the recommended way to perform HTTP requests in Node.js without external dependencies. Learn more in our guide on [HTTP requests in Node.js with Fetch API](/blog/how-tos/fetch-api-nodejs).\n\nLike most HTTP clients, `fetch()` automatically sets a default `User-Agent` header. The same behavior occurs in the [Python `requests` library](/faqs/python-requests/what-is-python-requests).\n\nBy default, `fetch()` in Node.js sets the following `User-Agent` string:\n\n```\nnode\n```\n\nYou can check the default `User-Agent` set by `fetch()` by making a GET request to [`httpbin.io/user-agent`](https://httpbin.io/user-agent). This endpoint returns the `User-Agent` header of the incoming request, allowing you to identify the user agent used by an HTTP client.\n\nTo test this, create a Node.js script, define an [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) function, and use `fetch()` to make the request:\n\n```js\nasync function getFetchDefaultUserAgent() {\n\n// make an HTTP request to the HTTPBin endpoint\n\n// to get the user agent\n\nconst response = await fetch(\"https://httpbin.io/user-agent\");\n\n// read the default user agent from the response\n\n// and print it\n\nconst data = await response.json();\n\nconsole.log(data);\n\n}\n\ngetFetchDefaultUserAgent();\n```\n\nExecute the JavaScript code above, and you will receive the following string:\n\n```\n{ 'user-agent': 'node' }\n```\n\nBy default, `fetch()` in Node.js sets the `User-Agent` to `node`, which differs significantly from browser user agents. This can trigger [anti-bot systems](/webinar/bot-detection).\n\nAnti-bot solutions detect unusual user agents and flag such requests as bots, leading to blocks. Changing the default Node.js `User-Agent` helps avoid detection.\n\n## How to Change the Node.js User Agent Using the Fetch API\n\nThe Fetch API specification does not include a built-in method for changing the `User-Agent`. However, since it is just an HTTP header, you can customize its value using [`fetch()` header options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_headers).\n\n**Set a User Agent Locally**\n\n`fetch()` supports header customization via the `headers` option. Use it to set the `User-Agent` header when making a specific HTTP request as follows:\n\n```js\nconst response = await fetch(\"https://httpbin.io/user-agent\", {\n\nheaders: {\n\n\"User-Agent\":\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n},\n\n});\n```\n\nPut it all together and you will get:\n\n```js\nasync function getFetchUserAgent() {\n\n// make an HTTP request to HTTPBin\n\n// with a custom user agent\n\nconst response = await fetch(\"https://httpbin.io/user-agent\", {\n\nheaders: {\n\n\"User-Agent\":\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n},\n\n});\n\n// read the default user agent from the response\n\n// and print it\n\nconst data = await response.json();\n\nconsole.log(data);\n\n}\n\ngetFetchUserAgent();\n```\n\nLaunch the above script, and this time the result will be:\n\n```\n{\n\n'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'\n\n}\n```\n\n**Set a User Agent Globally**\n\nWhile setting the `User-Agent` per request is simple, it can lead to repetitive code. However, the `fetch()` API does not currently support global overrides for its default settings.\n\nTo work around this, you can create a wrapper function to customize `fetch()` with your desired configurations:\n\n```js\nfunction customFetch(url, options = {}) {\n\n// custom headers\n\nconst customHeaders = {\n\n\"User-Agent\":\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n...options.headers, // merge with any other headers passed in the options\n\n};\n\nconst mergedOptions = {\n\n...options,\n\nheaders: customHeaders,\n\n};\n\nreturn fetch(url, mergedOptions);\n\n}\n```\n\nYou can now make an HTTP request with a custom user agent by calling `customFetch()` instead of `fetch()`:\n\n```js\nconst response = await customFetch(\"https://httpbin.io/user-agent\");\n```\n\nThe complete Node.js script will be:\n\n```js\nfunction customFetch(url, options = {}) {\n\n// add a custom user agent header\n\nconst customHeaders = {\n\n\"User-Agent\":\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n...options.headers, // merge with any other headers passed in the options\n\n};\n\nconst mergedOptions = {\n\n...options,\n\nheaders: customHeaders,\n\n};\n\nreturn fetch(url, mergedOptions);\n\n}\n\nasync function getFetchUserAgent() {\n\n// make an HTTP request to HTTPBin\n\n// through the custom fetch wrapper\n\nconst response = await customFetch(\"https://httpbin.io/user-agent\");\n\n// read the default user agent from the response\n\n// and print it\n\nconst data = await response.json();\n\nconsole.log(data);\n\n}\n\ngetFetchUserAgent();\n```\n\nLaunch the Node.js script above, and it will print:\n\n```\n{\n\n'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'\n\n}\n```\n\n## Implement User Agent Rotation in Node.js\n\nSimply replacing the default `User-Agent` with a browser string may not bypass anti-bot detection. If multiple requests originate from the same IP with the same user agent, anti-scraping systems can still flag the activity as automated.\n\nTo reduce detection risk in Node.js, introduce variability in your requests. One effective method is **user agent rotation**, where the `User-Agent` header changes on each request. This makes your requests appear to come from different browsers, reducing the chance of being blocked.\n\nLet's implement user agent rotation in Node.js.\n\n### Step #1: Retrieve a List of User Agents\n\nVisit a site like [WhatIsMyBrowser.com](https://www.whatismybrowser.com/guides/the-latest-user-agent/) and populate a list of some valid user agent values:\n\n```js\nconst userAgents = [\n\n\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0\",\n\n\"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15\",\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/126.0.2592.113\",\n\n// other user agents...\n\n];\n```\n\n\u003e **Tip**:\n\u003e \n\u003e The more real-world user agent strings this array contains, the better to avoid anti-bot detection.\n\n### Step #2: Randomly Pick a User Agent\n\nCreate a function that randomly selects and returns a user agent string from the list:\n\n```js\nfunction getRandomUserAgent() {\n\nconst userAgents = [\n\n// user agents omitted for brevity...\n\n];\n\n// return a user agent randomly\n\n// extracted from the list\n\nreturn userAgents[Math.floor(Math.random() * userAgents.length)];\n\n}\n```\n\nLet’s break down what happens in this function:\n\n* [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) generates a random number between 0 and 1\n* This number is then multiplied by the length of the `userAgents` array.\n* [`Math.floor()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) rounds down the resulting number to the largest integer less than or equal to that number.\n* The resulting number from the previous operations corresponds to a randomly generated index that goes from 0 to `userAgents.length - 1`.\n* The index is then used to return a random user agent from the array of user agents.\n\nEvery time you call the `getRandomUserAgent()` function, you will likely get a different user agent.\n\n### Step #3: Make the HTTP Request with a Random User Agent\n\nTo implement user agent rotation in Node.js using `fetch()`, set the `User-Agent` header with the value from the `getRandomUserAgent()` function:\n\n```js\nconst response = await fetch(\"https://httpbin.io/user-agent\", {\n\nheaders: {\n\n\"User-Agent\": getRandomUserAgent(),\n\n},\n\n});\n```\n\nThe HTTP request performed via the Fetch API will now have a random user agent.\n\n### Step #4: Put It All Together\n\nAdd the snippets of the previous steps to a Node.js script, and then wrap the logic for making a `fetch()` request in an `async` function.\n\nHere is what your final Node.js user agent rotation script should look like:\n\n```js\nfunction getRandomUserAgent() {\n\nconst userAgents = [\n\n\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\",\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0\",\n\n\"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15\",\n\n\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/126.0.2592.113\",\n\n// other user agents...\n\n];\n\n// return a user agent randomly\n\n// extracted from the list\n\nreturn userAgents[Math.floor(Math.random() * userAgents.length)];\n\n}\n\nasync function getFetchUserAgent() {\n\n// make an HTTP request with a random user agent\n\nconst response = await fetch(\"https://httpbin.io/user-agent\", {\n\nheaders: {\n\n\"User-Agent\": getRandomUserAgent(),\n\n},\n\n});\n\n// read the default user agent from the response\n\n// and print it\n\nconst data = await response.json();\n\nconsole.log(data);\n\n}\n\ngetFetchUserAgent();\n```\n\nRun the script 3 or 4 times. Statistically, you should see different user agent responses as below:\n\n![different user agent responses](https://github.com/luminati-io/node-js-user-agent/blob/main/images/different-user-agent-responses-1024x298.png)\n\nThis demonstrates that the user agent rotation is working effectively.\n\nEt voilà! You are now skilled at setting user agents in Node.js using the Fetch API.\n\n## Conclusion\n\nImplementing user agent rotation in Node.js helps evade basic anti-scraping systems. Yet, more advanced systems can still detect and block your automated requests. To avoid IP bans, consider [Web Scraper API](https://brightdata.com/products/web-scraper) that effectively bypasses anti-scraping measures through features like IP and user agent rotation, making web scraping easier than ever.\n\nSign up now and start your free trial today!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluminati-io%2Fnode-js-user-agent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluminati-io%2Fnode-js-user-agent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluminati-io%2Fnode-js-user-agent/lists"}