Mock the API in Kong without Coding

Danuka Praneeth
3 min readNov 12, 2021

1) Introduction

This documentation describe the steps to setup a mock REST API in Kong API gateway without writing a single line of code. We use few of the many available plugins in Kong to setup this API flow solely using configurations.

This can be useful to simulate and test the behavior any new API service before actually implementing it though a web service. So we can let the consumers experience the actual behavior of requests and responses though the API gateway via the mock backend while your development team builds the real service. But this has few limitations as we cannot simulate very complex response payloads using the available plugins.

2) Features and API Flow

Our mock API simulation will contain the below features.

  • Mocked API response payload and headers
  • Request and response logging to a file

Below are the required Kong plugins to produce the above behaviors.

  • Request Termination Plugin: This plugin terminates incoming requests flow and generate a response with a specified status code and message. This can also be used to (temporarily) stop traffic on a Service or a Route, or even block a Consumer.
  • File Log Plugin: Append request and response data in JSON format to a log file. You can also specify streams (for example, /dev/stdout and /dev/stderr), which is especially useful when running Kong in Kubernetes.
  • Response transfer advanced: Transform the response sent by the upstream server on the fly via Kong, before returning the response to the client.

3) Configuring the Kong Plugins

Request Termination Plugin

  • Below is the sample configurations to terminate a HTTP request with a response code of 200. You can use desired HTTP response code in the parameter status_code of the below configuration.
kind: KongPlugin
name: terminate-http-with-200
namespace: common-ingress
status_code: 200
message: Success. Response from Kong is 200 !
plugin: request-termination

File Log Plugin

Below I have given two configurations. First config is for the Kong file-log plugin which actually logs the request to a file created on the folder path /filelog/client1-requests.log. If we apply this plugin to an Ingress, then it will logs all the requests to this Ingress.

path: /filelog/client1-requests.log
kind: KongPlugin
name: request-log-plugin
namespace: common-ingress
plugin: file-log

But if you are using Kong’s authentication features with OIDC plugin and ACL plugins, then you can apply the file-log plugin to those specific clients. Then it will only log the requests from a specific client. For a such behavior, you can apply the plugin to the kongconsumer as given below.

kind: KongConsumer
name: client1
namespace: common-ingress
annotations: request-log-plugin
username: client1
- acl-client1
  • There is a know limitation we have with the file log plugin. That is only one Kong gateway pod can write to a log file at a given time. So we need to scale down the API gateway to one pod to get an accurate log all the requests to the log file.

Response Transformer Advanced Plugin

Using this plugin, we can further modify the payload and the headers of your API response returned by the request-termination plugin. Below is just a sample configuration of this plugin and I am not using this plugin for the simulated API response given later in this article.

kind: KongPlugin
name: response-transformer
- "200"
- new-json-key:some_value
- another-json-key:some_value
- x-toremove
- json-key-toremove:json-key-toremove
- "200"
enabled: true
run_on: first
- http
- https
plugin: response-transformer-advanced

Now you can expose the mock API similar to how you expose a normal API in Kong. Once we apply the above plugins to our API, it will behave like a simulated API backend.

apiVersion: extensions/v1beta1
kind: Ingress
name: myapi
annotations: 'terminate-http-with-200, request-log-plugin' 'false' https 'true'
- http:
- path: /api/v1/mock/myapi
serviceName: mock-svc
servicePort: 8443

4) Sample API Requests

Below is a sample API request to test the above API and the expected output. This is the most simple response you can simulate with Kong. But you can generate more complex responses by changing the above plugins as per your requirement.

curl --location --request GET ''{"message":"Success. Response from Kong is 200 !"}

Thank You!



Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker