Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added an example for detecting user location in edge functions

Lakshan Pereraac0ea037f8ae
diff --git a/examples/edge-functions/supabase/functions/location/index.ts b/examples/edge-functions/supabase/functions/location/index.ts
new file mode 100644
index 00000000000..438a33418a2
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/location/index.ts
@@ -0,0 +1,21 @@
+// Follow this setup guide to integrate the Deno language server with your editor:
+// https://deno.land/manual/getting_started/setup_your_environment
+// This enables autocomplete, go to definition, etc.
+
+import { serve } from "https://deno.land/[email protected]/http/server.ts"
+
+function ips(req: Request) {
+ return req.headers.get("x-forwarded-for")?.split(/\s*,\s*/);
+}
+
+serve(async (req) => {
+ const clientIps = ips(req) || [''];
+ const res = await fetch(`https://ipinfo.io/${clientIps[0]}?token=XXXX`, {
+ headers: { 'Content-Type': 'application/json'}});
+ const { city, country } = await res.json();
+
+ return new Response(
+ JSON.stringify(`You're accessing from ${city}, ${country}`),
+ { headers: { "Content-Type": "application/json" } },
+ )
+})