1. Examples
  2. Geolocation Routing

Geolocation Routing

Route requests to regional backends based on user location for data residency and latency optimization.

Deploy to Zuplo
Deploy to Zuplo

Prerequisite: You need a Zuplo account to run this example. Sign up for free

Route API requests to different backends based on the user's geographic location. Zuplo detects the user's country at the edge and this example maps it to regional backends.

This pattern is useful for:

  • Data residency compliance: Keep EU data in Europe, US data in America
  • Latency optimization: Route users to the nearest regional backend
  • Regional feature rollouts: Test features in specific regions before global release

Prerequisites

  • Node.js 18+ installed
  • A Zuplo account (only required for deployment). You can sign up for free.

Working with this Example

Locally

Get a local copy using the Zuplo CLI:

Terminalbash
npx create-zuplo-api@latest --example geolocation-routing
cd geolocation-routing
npm install
cp env.example .env

Start the development server:

Terminalbash
npm run dev

The server starts on http://localhost:9000.

Deploy to Zuplo

Click the Deploy to Zuplo button on this page to deploy directly to your Zuplo account.

How It Works

The geolocation-routing policy runs before each request:

  1. Reads the user's country from context.incomingRequestProperties.country (detected from IP at the edge)
  2. Maps the country to a region using ROUTING_CONFIG (americas, europe, apac, or global)
  3. Sets context.custom.backendUrl to the regional backend URL
  4. Adds X-Routed-Region and X-Detected-Country headers to the request

The urlForwardHandler then forwards the request to ${context.custom.backendUrl}.

Region Mapping

RegionCountriesEnvironment Variable
AmericasUS, CA, MXAPI_URL_AMERICAS
EuropeGB, FR, DE, IT, ES, NL, + 20 moreAPI_URL_EUROPE
APACJP, KR, AU, NZ, SG, IN, + 7 moreAPI_URL_APAC
GlobalAll others (fallback)API_URL_DEFAULT

All regions default to https://echo.zuplo.io — an echo service that returns your request details as JSON, making it easy to verify routing is working.

Project Structure

text
├── config/
│   ├── routes.oas.json       # Wildcard route using urlForwardHandler
│   └── policies.json         # Geolocation routing policy config
├── modules/
│   └── geolocation-routing.ts  # Country-to-region mapping and routing logic
└── zuplo.jsonc               # Zuplo project metadata

API Endpoints

MethodPathDescription
GET/v1/{path}Forward GET request to regional backend
POST/v1/{path}Forward POST request to regional backend

Testing

Note: Real geolocation detection only works when deployed to Zuplo's edge. Locally, use the X-Test-Country header to simulate different locations.

Use the -i flag with curl to see response headers:

Terminalbash
# Simulate a request from Germany (routes to Europe)
curl -i http://localhost:9000/v1/anything -H "X-Test-Country: DE"

You'll see headers showing the routing decision:

text
HTTP/1.1 200 OK
X-Routed-Region: europe
X-Detected-Country: DE

Test different regions

Terminalbash
# Route to Americas (United States)
curl -i http://localhost:9000/v1/anything -H "X-Test-Country: US"

# Route to APAC (Japan)
curl -i http://localhost:9000/v1/anything -H "X-Test-Country: JP"

# Route to global fallback (unknown country)
curl -i http://localhost:9000/v1/anything -H "X-Test-Country: XX"

The response body from echo.zuplo.io shows your forwarded request details as JSON.

Configuration

To route to real regional backends, set these environment variables:

VariableDescriptionDefault
API_URL_AMERICASBackend for Americashttps://echo.zuplo.io
API_URL_EUROPEBackend for Europehttps://echo.zuplo.io
API_URL_APACBackend for Asia-Pacifichttps://echo.zuplo.io
API_URL_DEFAULTFallback for other regionshttps://echo.zuplo.io

Local development: Copy env.example to .env and update the URLs (cp env.example .env).

Deployed projects: Set in the Zuplo Portal under Settings → Environment Variables.

Extending This Example

  • Add regions: Add new entries to ROUTING_CONFIG and getBackendUrl() in modules/geolocation-routing.ts
  • Add authentication: Include api-key-auth in the policy chain before geolocation-routing
  • Add rate limiting: Apply different rate limits per region using context.custom.region
  • Combine with A/B testing: Route a percentage of traffic to alternate backends per region

Troubleshooting

IssueCauseSolution
Always routes to globalCountry not in ROUTING_CONFIGAdd country code to the appropriate region
X-Test-Country header not workingHeader not being sentVerify with curl -v that header appears in request
Wrong region in logsCountry mapped incorrectlyCheck ROUTING_CONFIG in geolocation-routing.ts
Backend URL undefinedEnvironment variable not setDefaults to echo.zuplo.io; set env var for real backend

Learn More

  • Geolocation Routing Guide
  • Custom Code Inbound Policy
  • ZuploContext Documentation
  • Environment Variables

Quick Links

View on GitHubDocumentation

Run Locally

Clone and run this example:

npx create-zuplo-api --example geolocation-routing

On This Page

Related Examples

Explore more examples in this category

Canary Routing

Routing

Gradually roll out new backend versions by routing a percentage of traffic to canary before full deployment.

View Example

Environment-Based Routing

Routing

Route requests to sandbox or production backends based on API key metadata, like Stripe.

View Example
Check all of our Examples

Scale your APIs with
confidence.

Start for free or book a demo with our team.
Book a demoStart for Free
SOC 2 TYPE 2High Performer Spring 2025Momentum Leader Spring 2025Best Estimated ROI Spring 2025Easiest To Use Spring 2025Fastest Implementation Spring 2025

Get Updates From Zuplo

Zuplo logo
© 2026 zuplo. All rights reserved.
Products & Features
API ManagementAI GatewayMCP ServersMCP GatewayDeveloper PortalRate LimitingOpenAPI NativeGitOpsProgrammableAPI Key ManagementMulti-cloudAPI GovernanceMonetizationSelf-Serve DevX
Developers
DocumentationBlogLearning CenterCommunityChangelogIntegrations
Product
PricingSupportSign InCustomer Stories
Company
About UsMedia KitCareersStatusTrust & Compliance
Privacy PolicySecurity PoliciesTerms of ServiceTrust & Compliance
Docs
Pricing
Sign Up
Login
ContactBook a demoFAQ
Zuplo logo
DocsPricingSign Up
Login