{"id":19589783,"url":"https://github.com/supercharge/request-ip","last_synced_at":"2025-04-09T11:10:57.430Z","repository":{"id":43356494,"uuid":"284726047","full_name":"supercharge/request-ip","owner":"supercharge","description":"Retrieve a request’s IP address in Node.js","archived":false,"fork":false,"pushed_at":"2022-10-31T11:30:38.000Z","size":49,"stargazers_count":64,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T02:22:39.305Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/supercharge.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-03T14:44:12.000Z","updated_at":"2025-03-24T01:47:19.000Z","dependencies_parsed_at":"2023-01-20T02:53:09.783Z","dependency_job_id":null,"html_url":"https://github.com/supercharge/request-ip","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supercharge%2Frequest-ip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supercharge%2Frequest-ip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supercharge%2Frequest-ip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supercharge%2Frequest-ip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/supercharge","download_url":"https://codeload.github.com/supercharge/request-ip/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248027407,"owners_count":21035594,"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","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":[],"created_at":"2024-11-11T08:20:58.366Z","updated_at":"2025-04-09T11:10:57.410Z","avatar_url":"https://github.com/supercharge.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\r\n  \u003ca href=\"https://superchargejs.com\"\u003e\r\n    \u003cimg width=\"471\" style=\"max-width:100%;\" src=\"https://superchargejs.com/images/supercharge-text.svg\" /\u003e\r\n  \u003c/a\u003e\r\n  \u003cbr/\u003e\r\n  \u003cbr/\u003e\r\n  \u003cp\u003e\r\n    \u003ch3\u003eRequest IP\u003c/h3\u003e\r\n  \u003c/p\u003e\r\n  \u003cp\u003e\r\n    Retrieve a request’s IP address\r\n  \u003c/p\u003e\r\n  \u003cbr/\u003e\r\n  \u003cp\u003e\r\n    \u003ca href=\"#installation\"\u003e\u003cstrong\u003eInstallation\u003c/strong\u003e\u003c/a\u003e ·\r\n    \u003ca href=\"#usage\"\u003e\u003cstrong\u003eUsage\u003c/strong\u003e\u003c/a\u003e ·\r\n    \u003ca href=\"#detecting-the-ip-address\"\u003e\u003cstrong\u003eDetecting the IP Address\u003c/strong\u003e\u003c/a\u003e\r\n  \u003c/p\u003e\r\n  \u003cbr/\u003e\r\n  \u003cbr/\u003e\r\n  \u003cp\u003e\r\n    \u003ca href=\"https://www.npmjs.com/package/@supercharge/request-ip\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@supercharge/request-ip.svg\" alt=\"Latest Version\"\u003e\u003c/a\u003e\r\n    \u003ca href=\"https://www.npmjs.com/package/@supercharge/request-ip\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@supercharge/request-ip.svg\" alt=\"Monthly downloads\"\u003e\u003c/a\u003e\r\n  \u003c/p\u003e\r\n  \u003cp\u003e\r\n    \u003cem\u003eFollow \u003ca href=\"http://twitter.com/marcuspoehls\"\u003e@marcuspoehls\u003c/a\u003e and \u003ca href=\"http://twitter.com/superchargejs\"\u003e@superchargejs\u003c/a\u003e for updates!\u003c/em\u003e\r\n  \u003c/p\u003e\r\n\u003c/div\u003e\r\n\r\n---\r\n\r\n## Introduction\r\nThe `@supercharge/request-ip` package provides a function to retrieve a request’s IP address.\r\n\r\n\r\n## Installation\r\n\r\n```\r\nnpm i @supercharge/request-ip\r\n```\r\n\r\n\r\n## Usage\r\n\r\n```js\r\nconst RequestIp = require('@supercharge/request-ip')\r\n\r\nconst ip = RequestIp.getClientIp(request)\r\n\r\n// for example '213.211.254.97' as an IP v4 address\r\n// or '2001:0db8:85a3:0000:0000:8a2e:0370:7334' as an IP v6 address\r\n// or 'undefined' if no IP address is available on the given request\r\n```\r\n\r\nDepending on your used web framework, you may use it in a middleware or route handler:\r\n\r\n\r\n**simple Express example**\r\n\r\n```js\r\nconst { getClientIp } = require('@supercharge/request-ip')\r\n\r\nconst expressMiddleware = function (req, res, next) {\r\n  req.ip = getClientIp(req)\r\n\r\n  next()\r\n}\r\n```\r\n\r\n**simple hapi route handler example:**\r\n\r\n```js\r\nconst Hapi = require('@hapi/hapi')\r\nconst { getClientIp } = require('@supercharge/request-ip')\r\n\r\nconst server = new Hapi.Server({\r\n  host: 'localhost'\r\n})\r\n\r\nserver.route({\r\n  method: 'GET',\r\n  path: '/login',\r\n  handler: (request, h) =\u003e {\r\n    const ip = getClientIp(request)\r\n\r\n    return h.response(ip)\r\n  }\r\n})\r\n```\r\n\r\n\r\n## Detecting the IP Address\r\nThe client’s IP address may be stored in different locations of the request instance varying between services.\r\n\r\nHere’s the order of locations in which the packages searches for the requesting IP address:\r\n\r\n1. Checks HTTP request headers\r\n    1. `x-forwarded-for`: this header may contain multiple IP address for (client/proxies/hops). This package extracts and returns the first IP address.\r\n    2. `x-forwarded`, `forwarded`, `forwarded-for` as variants from `x-forwarded-for` possibly configured by proxies\r\n    3. `x-client-ip` possibly configured by nginx\r\n    4. `x-real-ip` possibly configured in nginx\r\n    5. `cf-connecting-ip` from Cloudflare\r\n    6. `fastly-client-ip` from Fastly and Firebase\r\n    7. `true-client-ip` from Akamai and Cloudflare\r\n    8. `x-cluster-client-ip` from Rackspace\r\n2. Checks the HTTP connection in `request.connection` and `request.connection.socket`\r\n3. Checks the HTTP socket in `request.socket`\r\n4. Checks the HTTP info in `request.info`\r\n5. Checks the raw HTTP request instance in `request.raw`\r\n6. Checks the request context used by AWS API Gateway/Lambda in `request.requestContext`\r\n\r\n\r\n## Credits\r\nA huge thank you to [Petar Bojinov](https://github.com/pbojinov) for his [request-ip](https://github.com/pbojinov/request-ip) package. I was using Petar’s package for two years in my [hapi-rate-limitor](https://github.com/futurestudio/hapi-rate-limitor) plugin. It seems Petar is busy with other work and I felt the need to create my own package providing the functionality to retrieve a request’s IP address.\r\n\r\n\r\n## Contributing\r\nDo you miss a way to find the request’s IP? We very much appreciate your contribution! Please send in a pull request 😊\r\n\r\n1.  Create a fork\r\n2.  Create your feature branch: `git checkout -b my-feature`\r\n3.  Commit your changes: `git commit -am 'Add some feature'`\r\n4.  Push to the branch: `git push origin my-new-feature`\r\n5.  Submit a pull request 🚀\r\n\r\n\r\n## License\r\nMIT © [Supercharge](https://superchargejs.com)\r\n\r\n---\r\n\r\n\u003e [superchargejs.com](https://superchargejs.com) \u0026nbsp;\u0026middot;\u0026nbsp;\r\n\u003e GitHub [@supercharge](https://github.com/supercharge/) \u0026nbsp;\u0026middot;\u0026nbsp;\r\n\u003e Twitter [@superchargejs](https://twitter.com/superchargejs)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupercharge%2Frequest-ip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsupercharge%2Frequest-ip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupercharge%2Frequest-ip/lists"}