Get client ip in a api route
Unanswered
American Fuzzy Lop posted this in #help-forum
American Fuzzy LopOP
import { fetchLicense } from '@/lib/license'
import { NextApiRequest } from 'next'
import {NextResponse } from 'next/server'
import requestIp from 'request-ip'
export async function GET(req: NextApiRequest) {
const ip = requestIp.getClientIp(req)
console.log("Request Made", ip)
if (!ip) return NextResponse.json(false)
const user = await fetchLicense(ip)
return NextResponse.json(user)
}
This will debug: Request Made null
And when I deploy it on vercel, it will give an error.
import { NextApiRequest } from 'next'
import {NextResponse } from 'next/server'
import requestIp from 'request-ip'
export async function GET(req: NextApiRequest) {
const ip = requestIp.getClientIp(req)
console.log("Request Made", ip)
if (!ip) return NextResponse.json(false)
const user = await fetchLicense(ip)
return NextResponse.json(user)
}
This will debug: Request Made null
And when I deploy it on vercel, it will give an error.
50 Replies
American Fuzzy LopOP
Failed to compile.
app/api/license/route.ts
Type error: Route "app/api/license/route.ts" has an invalid "GET" export:
Type "NextApiRequest" is not a valid type for the function's first argument.
Expected "Request | NextRequest", got "NextApiRequest".
Error: Command "npx prisma generate && next build" exited with 1So its basically not possible because I need to pass the NextApiRequest as a argument to the requestIp.getClientIp(...)
i think you can do it with NextApiRequest
just import it i think
or same as how you send back data like NextResponse
try NextRequest
American Fuzzy LopOP
Yeah I imported it ofc
Yeah but then the requestIp isnt working
welp i guess we use different ways
American Fuzzy LopOP
Can you maybe show me ur code?
i use pages/api and not route
American Fuzzy LopOP
Ahh yeah thats probably why
probly that is why it works
American Fuzzy LopOP
Indeed but its basically impossible for me to do this on next 13 ... with app router
cant you pass parameter of NextRequest in the function ?
American Fuzzy LopOP
Nah only NextApiRequest
What I can't use in the GET function
import { fetchLicense } from '@/lib/license'
import { NextApiRequest } from 'next'
import { NextResponse } from 'next/server'
import requestIp from 'request-ip'
export async function GET(req: NextApiRequest) {
// Check if you are behind a proxy or load balancer
const clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log("Request Made", clientIp);
if (!clientIp) {
return NextResponse.json(false);
}
const user = await fetchLicense(clientIp);
return NextResponse.json(user);
}
would you check this ?American Fuzzy LopOP
already done but its just showing the ip of cloudflare
Maybe thats the problem
some chat gpt response with the problem also remembered about getting remote address
@American Fuzzy Lop already done but its just showing the ip of cloudflare
if that is then it is behind proxy
American Fuzzy LopOP
But NextApiRequest will not work
@Ejayz || PiggyDev if that is then it is behind proxy
American Fuzzy LopOP
Yeah I think so
Should I disable that?
based on what chat gpt says
. if you want to see their IP yes disable it . Though it dpends on you
. if you want to see their IP yes disable it . Though it dpends on youis IP really important ?
American Fuzzy LopOP
Yeah its for a license system
Not like grabbing them for illegal reasons 🤣
welp find a way for the losses . That is for DNS record to be disabled
if you want its instruction
sometimes i have trust in gpt but sometimes i dont
lol
American Fuzzy LopOP
Hahaha thx for your time and effort 💯
How do I find the client IP address in NextJS?
To get the Client Ip address in NextJS, Please follow the below steps
NextJS provides NextRequest in Server Code, It is a wrapper of Native Request Object.
Import, NextRequest in API routes in the server side
import type { NextRequest } from 'next/server'
call NextRequest.ip property to get the IP Address of a Server request.
To get the Client Ip address in NextJS, Please follow the below steps
NextJS provides NextRequest in Server Code, It is a wrapper of Native Request Object.
Import, NextRequest in API routes in the server side
import type { NextRequest } from 'next/server'
call NextRequest.ip property to get the IP Address of a Server request.
another want probly want to try
NextRequest.iplike import it
American Fuzzy LopOP
Ah
@Ejayz || PiggyDev another want probly want to try NextRequest.ip
American Fuzzy LopOP
ip doesn't exist on NextRequest
welp i dont have other resorts probly wait for other people to help you with this .
American Fuzzy LopOP
Yeah no problem thx for the help!
@Ejayz || PiggyDev ohhhf
American Fuzzy LopOP
Works after disabling the proxy and waiting like 10 minutes
noice
just mark the answer with solutions