next-request-ip

A Next.js-friendly fork of request-ip, optimized for working with the Web API Headers to retrieve client IP addresses.
Installation
npm install next-request-ip
yarn add next-request-ip
Usage
App Router (Next.js 13+)
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 (
<div>
<p>Your IP: {clientIp}</p>
</div>
);
}
API Routes
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 });
}
CommonJS (Node)
const { getClientIp } = require('next-request-ip');
const headers = new Headers({ 'x-client-ip': '192.0.2.1' });
console.log(getClientIp(headers));
API
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-IPv4
Supports 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 standards
Compatibility
- 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