A Next.js-friendly fork of request-ip, optimized for Headers to retrieve client IP addresses.
npm install next-request-ip!CI  
A Next.js-friendly fork of request-ip, optimized for working with the Web API Headers to retrieve client IP addresses.
``bash`
npm install next-request-ipor
yarn add next-request-ip
`typescript
import { getClientIp } from "next-request-ip";
import { headers } from "next/headers";
export default async function Page() {
const headersList = await headers();
const clientIp = getClientIp(headersList);
return (
Your IP: {clientIp}
$3
`typescript
import { NextResponse, type NextRequest } from "next/server";
import { getClientIp } from "next-request-ip";export async function GET(request: NextRequest) {
const ip = getClientIp(request.headers);
return NextResponse.json({ ip });
}
`$3
`js
// If your environment uses CommonJS you can require the package:
const { getClientIp } = require('next-request-ip');// Example usage with a standard Fetch-like Request
// (Pass a Headers-compatible object to getClientIp)
const headers = new Headers({ 'x-client-ip': '192.0.2.1' });
console.log(getClientIp(headers)); // '192.0.2.1'
`
API
$3
Returns the client IP address from the request headers, or
null if not found.Checks the following headers in order of priority:
Checks the following headers in order of priority (proxy/load-balancer headers first):
-
x-forwarded-for
- x-original-forwarded-for
- forwarded
- forwarded-for
- x-real-ip
- x-client-ip
- x-envoy-external-address (Envoy)
- x-envoy-client-address (Envoy)
- x-forwarded
- x-cluster-client-ip
- cf-connecting-ip
- do-connecting-ip
- fastly-client-ip
- true-client-ip
- x-appengine-user-ip
- Cf-Pseudo-IPv4
- x-forwarded-for
- x-original-forwarded-for
- forwarded
- forwarded-for
- x-real-ip
- x-client-ip
- x-envoy-external-address (Envoy)
- x-envoy-client-address (Envoy)
- x-forwarded
- x-cluster-client-ip
- cf-connecting-ip
- do-connecting-ip
- x-appengine-user-ip
- fastly-client-ip
- true-client-ip
- Cf-Pseudo-IPv4Supports both IPv4 and IPv6 addresses.
Differences from request-ip
- Designed specifically for Next.js and the Web API
Headers object
- No dependencies on Node.js-specific request objects
- Optimized for modern web standardsCompatibility
- Minimum Node.js version: >= 20 (see
package.json engines)
License
MIT License - see LICENSE file for details.
Credits
This library is a fork of request-ip by Petar Bojinov.
Original copyright notice:
`
Copyright (c) 2014 Petar Bojinov
``