MegaHydronics
MegaHydronics9mo 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 })
}
})
9 Replies
Convex Bot
Convex Bot9mo 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
MegaHydronicsOP9mo ago
#ask-ai referred me to this post https://discord.com/channels/1019350475847499849/1281699105101451284 which answered my question.
ballingt
ballingt9mo ago
What's your goal with this? Currently the incoming IP address is not exposed even in an httpAction.
MegaHydronics
MegaHydronicsOP9mo 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
ballingt9mo 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
MegaHydronicsOP9mo ago
For my use case, ctx.meta.geo would be great.
Rane
Rane8mo 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
timmm6mo ago
@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
d3m1r
d3m1r3mo ago
@ballingt I would use it as well, also to incorporate more robust security checks, like automatic malicious query detection, and IP based restriction of functionality. I feel like this is a must-have, a lot of use cases, and in many cases IP address is required by law or for mere functionality (e.g., Stripe).

Did you find this page helpful?