This example demonstrates how to use Kong's Request Callout plugin to enrich API requests with data from multiple backend services.
The setup includes:
- A main service that receives requests and enriches them with user and address data
- A mock service that simulates user and address backend APIs
- Two callouts that fetch data sequentially (user data first, then address data)
- Docker
- Docker Compose
- Kong Enterprise license (for the Request Callout plugin)
- Start the Kong Gateway:
docker compose up -d- Verify the services are running:
curl localhost:8001/servicesSend a request with a user ID:
curl -X POST http://localhost:8000 \
-H "Host: callout.dev" \
-H "Content-Type: application/json" \
-d '{"id": "123"}'Expected response:
{
"username": "John Doe",
"country": "USA"
}-
When a request arrives with a user ID, the first callout (
c1) fetches user data:- Calls
/v1/users/{id} - Returns user information including username and address_id
- Calls
-
The second callout (
c2) uses the address_id from the first callout:- Calls
/v1/addresses/{id} - Returns address information including country
- Calls
-
The final response combines:
- Username from the first callout
- Country from the second callout
The example includes a mock API (using Kong's Mocking plugin) that simulates two backend services:
-
User Service (
/v1/users/{id}):- Returns user details including username and address_id
- Example users: 123 (John Doe), 124 (Jane Smith)
-
Address Service (
/v1/addresses/{id}):- Returns address details including country
- Example addresses: 456 (USA), 457 (Germany)
kong-config/kong.yaml: Main Kong configuration including services, routes, and pluginsmock-api.yaml: OpenAPI specification for the mock backend servicesdocker-compose.yaml: Docker Compose configuration for running Kong
- The Request Callout plugin requires Kong Enterprise
- The mock service runs on the same Kong instance using the Mocking plugin
- Callouts are executed sequentially, with
c2depending on the response fromc1