To implement geo-based routing using Vercel Edge Middleware and ApogeoAPI, follow these steps:
Step 1: Set Up Your Environment
Ensure you have the necessary environment variables set up in your Vercel project. Specifically, you need an API key from ApogeoAPI.
bash1# .env.local file example 2APOGEOAPI_KEY=your-apogeoapi-key-here
Step 2: Create Middleware Function
Create a middleware function that will handle the geolocation and redirection logic. This function should be placed in your project's middleware directory or specified as an edge middleware in Vercel.
typescript1import { NextResponse } from 'next/server'; 2import fetch from 'node-fetch'; 3 4export async function middleware(request: Request) { 5 // Exclude API routes, static files, and Next.js internals 6 const excludedPaths = ['/api', /\.+\.(jpg|jpeg|png|gif|ico|svg|css|js|map)$/, /^\/_next/]; 7 8 if (excludedPaths.some(path => request.nextUrl.pathname.match(path))) { 9 return; 10 } 11 12 // Get the visitor's IP address 13 const ip = request 14 15[Read the full article at DEV Community](https://dev.to/apogeoapi/geo-redirect-users-at-the-edge-with-vercel-apogeoapi-in-10-lines-4k4n) 16 17--- 18 19**Want to create content about this topic?** [Use Nemati AI tools](https://nemati.ai) to generate articles, social posts, and more.

![[AINews] The Unreasonable Effectiveness of Closing the Loop](/_next/image?url=https%3A%2F%2Fmedia.nemati.ai%2Fmedia%2Fblog%2Fimages%2Farticles%2F600e22851bc7453b.webp&w=3840&q=75)



