Route requests to different targets based on the authenticated callerv3.14+

Map an authenticated credential to a host:port target and override the backend per request, bypassing load balancing. This example works together with the OpenID Connect plugin, which validates a bearer token and extracts a token claim (such as client_id) as a virtual credential. Datakit then reads that credential and uses its value to select a host:port target.

Unlike routing to a named Upstream entity, writing to kong.service.target bypasses load balancing, health checks, and retries. Use this approach for stable single-host backends where you don’t need a pool.

This example contains the following nodes:

  1. GET_CREDENTIAL reads the kong.client.credential object that the OpenID Connect plugin populated.
  2. PICK_TARGET uses a jq map to look up the credential’s .id field and return an object containing both the host:port address and the scheme. Unknown callers fall through to a default backend.
  3. EXTRACT_TARGET extracts the .target field from the PICK_TARGET output.
  4. SET_TARGET writes the extracted host:port string to kong.service.target, overriding the backend for this request.
  5. EXTRACT_SCHEME extracts the .scheme field from the PICK_TARGET output.
  6. SET_SCHEME writes the extracted scheme to kong.service.request.scheme, ensuring Kong uses the correct protocol when connecting.

For a complete tutorial, see Route requests to different targets based on the authenticated caller.

Note: The OpenID Connect plugin has a higher static priority than Datakit, so it always runs first in the access phase. No explicit plugin ordering configuration is required.

Prerequisites

  • You have configured the OpenID Connect plugin with credential_claim pointing to a token claim (such as client_id) and consumer_optional: true.

Set up the plugin

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!