MegaHydronics
MegaHydronics4mo ago

Accessing client IP address in a query/mutation

Good day. Any help would be appreciated, as I am fairly new to Convex. Is there a way to expose HTTP headers, such as for obtaining the IP address in the example below? Or is an httpAction required for this use case?
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
8 Replies
Convex Bot
Convex Bot4mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
MegaHydronics
MegaHydronicsOP4mo ago
#ask-ai referred me to this post https://discord.com/channels/1019350475847499849/1281699105101451284 which answered my question.
ballingt
ballingt4mo ago
What's your goal with this? Currently the incoming IP address is not exposed even in an httpAction.
MegaHydronics
MegaHydronicsOP4mo ago
It's for simple analytics of users' geographical distribution.
const response = await fetch(`https://ipinfo.io/${ip}/json/?token=${process.env.ipinfoToken}`)
const response = await fetch(`https://ipinfo.io/${ip}/json/?token=${process.env.ipinfoToken}`)
ballingt
ballingt4mo ago
If Convex were to expose as an extra header or ctx.meta.geo property would you want the geographical location directly, or still the IP address?
MegaHydronics
MegaHydronicsOP4mo ago
For my use case, ctx.meta.geo would be great.
Rane
Rane4mo ago
For my use case, I’m looking to implement an IP-based rate limiter in Convex. This would require reading the client’s IP address within an action. Is there currently any way to access the IP address server-side in Convex without having to rely on the client to send it?
timmm | DexFi Gia Dev
@ballingt I would definitely use this too. To rate limit on IP address I have to use a http endpoint. I think for me I'd probably want the raw details from the http request (presumably) that happens when initating the web socket

Did you find this page helpful?