Next.js get client ipv4 address

This is my fly.toml config

app = "****"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
# [build.args]
# NEXT_PUBLIC_EXAMPLE="value here"

[env]
PORT = "8080"

[deploy]
release_command = "npx prisma migrate deploy"

[experimental]
allowed_public_ports = []
auto_rollback = true

[[services]]
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 2500
soft_limit = 2000
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"

And this is next.js api route

// pages/api/get-ip.ts
export default function handler(req, res) {
    const forwarded = req.headers["x-forwarded-for"];
    const ip =
      (typeof forwarded === "string"
        ? forwarded.split(/, /)[0]
        : req.socket.remoteAddress) || "";

    console.log(ip);
}

I want to get ipv4 address instead of ipv6

To get the client’s ip address you need to use Fly-Client-IP instead of X-Forwarded-For

I just tried fly-client-ip and it’s still getting ipv6