Blog
  • AI Gateway
  • AI Security
  • AIOps
  • API Security
  • API Gateway
    • API Management
    • API Development
    • API Design
    • Automation
    • Service Mesh
    • Insomnia
    • View All Blogs
  1. Home
  2. Blog
  3. Engineering
  4. How to Dynamically Route Requests With Kong Enterprise
Engineering
July 31, 2020
4 min read

How to Dynamically Route Requests With Kong Enterprise

Mos Amokhtari
Topics
Kong Gateway EnterpriseKong GatewayAPI Gateway
Share on Social

More on this topic

eBooks

API Infrastructure: ESB versus API Gateway

eBooks

5 Questions To Ask Your API Gateway Vendor

See Kong in action

Accelerate deployments, reduce vulnerabilities, and gain real-time visibility. 

Get a Demo

Having worked with many customers and prospects at Kong, one of the main requirements we often hear is how to handle dynamic routing based on the URL and headers. In this blog post, I will cover different use cases we come across for dynamic routing and how Kong can address them.

The default behavior of an API gateway is to route the incoming request to the appropriate upstream service.

If you are new to Kong, a Service object represents the upstream API or Service. The main attribute of a Service is the URL (where Kong should proxy traffic to), which can be set as a single string URL or by specifying the protocol, host, port and path properties.

Steps to Route Kong Enterprise Requests

To make requests to services via Kong, they first need to be exposed by routes. A single service can have many routes to allow different policies to be applied to different clients/consumers accessing the same Service. After configuring services and routes, clients make requests to routes, and the requests will be proxied to services.

One common use case for dynamic routing is to rewrite the requested URL to a different URL for the upstream. This is helpful in instances where the structure of the service's URL has changed, but we don't want to change or impact how the client calls the service. For this use case, Kong can make the change transparent to the client by enabling URL rewriting. My colleague, Peter Kim, wrote a great blog post on this subject that I invite you to read.

Let’s review another use case. Based on a specific header, route the request to a different upstream. This scenario is useful when a new version of an API is available.

Let's walk through an example using Kong Manager to set up the scenario. Note Kong can also be managed through the Admin API as well as declaratively.

Say our API will be represented by a service called httpbin, which forwards requests to an upstream with URL httpbin.org.

Launch Kong Manager, click on the Workspace you want to work with, select Services and click Add a Service.

Create myfirstRoute

Let's now create a route called "myfirstRoute" to access the service through Kong when a path of /requestByheader is provided. Meaning, when a request to Kong is made with path /requestByheader, Kong will forward the request to the httpbin service.

In the httpbin service, click Add route, name the route "myfirstRoute" and under Path(s), enter /reequestByheader.

Let's confirm the service and route are working correctly by using Kong Studio to send a request to Kong and the route we created. As expected, the request is forwarded to the upstream httpbin.org.

Enable Dynamic Routing

To enable the dynamic routing use case based on header values, let’s enable the route-by-header plugin on this route. For those new to Kong, a plugin allows Kong to apply policy to request and responses. For this scenario, let's configure the plugin to route to a different upstream, httpbin2, if a header with version "v2" is part of the request.

Let's use the Admin API to configure the plugin on the route we created. The following command configures the route-by-header plug-in on the myfirstRoute to route to the upstream httpbin2 when a header version:v2 is part of the request.

curl -i -X POST http://<kong_host>:8001/routes/myfirstRoute/plugins -H ‘Content-Type: application/json’ –data ‘{"name": "route-by-header", "config": {"rules":[{"condition": {"version":"v2"}, "upstream_name": "httpbin2"}]}}’

Sending the same request as before using Kong Studio, but this time with a header of version v2, sends the request to httpbin2 instead of httpbin.org.

Many customers use this capability to enable A/B testing or to slowly migrate users from one version to another.

Target Upstream Requests

Another use case is that you may want to provide the target upstream in your request (in the URI, query parameters or in a header). One possible use case is, for example, the same service is hosted on different instances but specific to an end customer. One of our telecommunications customers providing network managed services uses this scenario to give access to the right EMS (Element Management System) to their end customers. The service provided is the same but on a different EMS. The route-transformer-advanced plugin is the most appropriate to do so, as it allows it to transform the routing on the fly in Kong, changing the upstream server, port or path to hit.

Case A: Using a Header

On the previous same route (myfirstRoute), instead of enabling the route-by-header plugin, we enable the plugins route-transformer-advanced.

For now on, for this path (/requestByheader), Kong expects a header called "mytarget" with the target host value.

Case B: Using a Part of the URI (Capture URI String)

First, we need to modify the path in the route. Paths accept regex (as explained in more details here) to allow capturing groups.

Let’s create another route with a path "/requestByUri/(?<target>\w+)"

Apply the route-transformer-advanced plugins using the capture URI method.

And voilà …

One comment here: Some may object that the upstream target may be unknown from the client/consumer side and therefore this won’t be of any use. In that particular case, I recommend that you read my next post on the usage of the serverless function plugin.

Developer agility meets compliance and security. Discover how Kong can help you become an API-first company.

Get a DemoStart for Free
Topics
Kong Gateway EnterpriseKong GatewayAPI Gateway
Share on Social
Mos Amokhtari

Recommended posts

Kong Gateway Enterprise 3.8.x.x EOL

Kong Logo
Product ReleasesSeptember 23, 2025

As of September 2025, Kong Gateway Enterprise 3.8 will enter its End Of Life (EOL) phase and will no longer be fully supported by Kong. Following this, Kong Gateway Enterprise 3.8 will enter a 12-month sunset support period, focused on helping cus

Andrew Jessup

Unlocking API Analytics for Product Managers

Kong Logo
EngineeringSeptember 9, 2025

Meet Emily. She’s an API product manager at ACME, Inc., an ecommerce company that runs on dozens of APIs. One morning, her team lead asks a simple question: “Who’s our top API consumer, and which of your APIs are causing the most issues right now?”

Christian Heidenreich

Announcing terraform-provider-konnect v3

Kong Logo
Product ReleasesAugust 22, 2025

It’s been almost a year since we released our  Konnect Terraform provider . In that time we’ve seen over 300,000 installs, have 1.7 times as many resources available, and have expanded the provider to include data sources to enable federated managem

Michael Heap

How to Build a Multi-LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 31, 2025

In the last two parts of this series, we discussed How to Strengthen a ReAct AI Agent with Kong AI Gateway and How to Build a Single-LLM AI Agent with Kong AI Gateway and LangGraph . In this third and final part, we're going to evolve the AI Agen

Claudio Acquaviva

How to Build a Single LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 24, 2025

In my previous post, we discussed how we can implement a basic AI Agent with Kong AI Gateway. In part two of this series, we're going to review LangGraph fundamentals, rewrite the AI Agent and explore how Kong AI Gateway can be used to protect an LLM

Claudio Acquaviva

How to Strengthen a ReAct AI Agent with Kong AI Gateway

Kong Logo
EngineeringJuly 15, 2025

This is part one of a series exploring how Kong AI Gateway can be used in an AI Agent development with LangGraph. The series comprises three parts: Basic ReAct AI Agent with Kong AI Gateway Single LLM ReAct AI Agent with Kong AI Gateway and LangGr

Claudio Acquaviva

Kong Gateway Enterprise 3.11 Makes APIs & Event Streams More Powerful

Kong Logo
Product ReleasesJuly 9, 2025

Update Includes Data Orchestration, CyberArk Support, Solace Integration, and Kafka Schema Validation We’re excited to bring you Kong Gateway Enterprise 3.11 with compelling new features to make your APIs and event streams even more powerful, includ

Anthony Gatti

Ready to see Kong in action?

Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.

Get a Demo
Powering the API world

Increase developer productivity, security, and performance at scale with the unified platform for API management, AI gateways, service mesh, and ingress controller.

Sign up for Kong newsletter

Platform
Kong KonnectKong GatewayKong AI GatewayKong InsomniaDeveloper PortalGateway ManagerCloud GatewayGet a Demo
Explore More
Open Banking API SolutionsAPI Governance SolutionsIstio API Gateway IntegrationKubernetes API ManagementAPI Gateway: Build vs BuyKong vs PostmanKong vs MuleSoftKong vs Apigee
Documentation
Kong Konnect DocsKong Gateway DocsKong Mesh DocsKong AI GatewayKong Insomnia DocsKong Plugin Hub
Open Source
Kong GatewayKumaInsomniaKong Community
Company
About KongCustomersCareersPressEventsContactPricing
  • Terms•
  • Privacy•
  • Trust and Compliance•
  • © Kong Inc. 2025