Skip to content

Instantly share code, notes, and snippets.

@ikromnurrohim
Last active August 21, 2025 13:24
Show Gist options
  • Save ikromnurrohim/dd4ece36aa2d8d6ef30a840cf421366b to your computer and use it in GitHub Desktop.
Save ikromnurrohim/dd4ece36aa2d8d6ef30a840cf421366b to your computer and use it in GitHub Desktop.
KrakenD - Merging Chaining APIs Response

KrakenD Merging Chaining API Response

Summary: Using KrakenD chaining and a Lua formatter script, combine separate Product and Price API responses into a unified endpoint that maintains nested objects and respects client-side expectations.

Problem

  1. We have separate Product API and Price API.
  2. The client wants to receive both product information and pricing in a single response.

Goal: Update the endpoint without requiring any changes on the client-side logic.

Requirement

  1. Update an endpoint that merges responses from the Product API and the Price API.
  2. Keep the data structure using nested objects product and price.
  3. Ensure the response envelope (code, message, error) remains consistent.

Current condition

image

product detail response

{
    "code": "KND-200",
    "data": {
        "product": {
            "category": "Electronics",
            "id": 1,
            "name": "Wireless Earbuds",
            "stock": 150
        }
    },
    "error": "null",
    "message": "Success"
}

price detail response

{
    "code": "KND-200",
    "data": {
        "price": {
            "id": 1,
            "price": 199.99
        }
    },
    "error": "null",
    "message": "Success"
}

Expectation

image
{
    "code": "PLT-KND-200",
    "data": {
        "price": {
            "id": 1,
            "price": 199.99
        },
        "product": {
            "category": "Electronics",
            "id": 1,
            "name": "Wireless Earbuds",
            "stock": 150
        }
    },
    "error": "null",
    "message": "Success"
}

Approach Using Krakend

image

We implementation api chaining and response processing in KrakenD, the goals that get the both response then we proccess that response to be like our standard response.

  1. This is endpoint /product/{product_id} that client apps call
  2. This KrakenD request to to backend endpoint /product/{product_id} and /price/{product_id}
  3. After the both chaining request is done, KrakenD call lua script which has function response_formatter with three param one is the response (from both api call), then the object group from each response.
function response_formatter(resp, first_group_name, second_group_name)
    local response_data = resp:data()
    
    -- Extract data from nested response objects
    local first_group_obj = response_data:get(first_group_name)
    local second_group_obj = response_data:get(second_group_name)
    
    local first_group_data = nil
    local second_group_data = nil
    
    -- Get the 'data' field from first group if it exists
    if first_group_obj and first_group_obj.get then
        first_group_data = first_group_obj:get('data')
    end
    
    -- Get the 'data' field from second group if it exists
    if second_group_obj and second_group_obj.get then
        second_group_data = second_group_obj:get('data')
    end
    
    -- Clean up: remove the original nested objects
    response_data:del(first_group_name)
    response_data:del(second_group_name)
    
    -- Set standard response envelope
    response_data:set('code', 'KND-200')
    response_data:set('message', 'Success')
    response_data:set('error', 'null')
    
    -- Build the new data structure with dynamic keys
    local restructured_data = {}
    
    if first_group_data then
        restructured_data[first_group_name] = first_group_data
    end
    
    if second_group_data then
        restructured_data[second_group_name] = second_group_data
    end
    
    -- Set the restructured data
    response_data:set('data', restructured_data)
end
  1. Current response from endpoint /product/{product_id} after implemting response proccessing
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment