Kong: pre-function and post-function run Lua without building a plugin

The serverless-functions plugin executes inline Lua in any phase. pre-function has the highest priority of any plugin, which makes it the tool for anything that must happen first.

Code
curl -X POST http://localhost:8001/routes/api/plugins \
  --data name=pre-function \
  --data 'config.access[1]=
    local key = kong.request.get_query_arg("api_key")
    if key then
      kong.service.request.set_header("apikey", key)
    end'

# post-function has the LOWEST priority, so it runs last
  --data name=post-function \
  --data 'config.header_filter[1]=kong.response.clear_header("Server")'
Output
# pre-function  PRIORITY = 1000000   (before every auth plugin)
# post-function PRIORITY = -1000     (after everything)
#
# This is how you move a credential from a query string into the header that
# key-auth reads - request-transformer is priority 801 and would run far too
# late.
#
# Inline Lua has no tests, no version control and no review. It is right for
# a few lines; past that, write a real plugin.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25