Blog
  • AI Gateway
  • AI Security
  • AIOps
  • API Security
  • API Gateway
    • API Management
    • API Development
    • API Design
    • Automation
    • Service Mesh
    • Insomnia
    • View All Blogs
  1. Home
  2. Blog
  3. Engineering
  4. Developing a Kong Gateway Plugin With Go
Engineering
April 22, 2021
3 min read

Developing a Kong Gateway Plugin With Go

Mert Simsek
Topics
API GatewayAPI DevelopmentKong Gateway
Share on Social

More on this topic

eBooks

Maturity Model for API Management

eBooks

API Infrastructure: ESB versus API Gateway

See Kong in action

Accelerate deployments, reduce vulnerabilities, and gain real-time visibility. 

Get a Demo

This tutorial shows you how to create a custom Kong Gateway plugin with Go programming language. The sample plugin I created adds an extra layer for security between consumers and producers. The way it works is it identifies consumers through a consumer-key from a query string. Without this parameter, they'll get an error message.

Creating a Consumer Key with Kong Gateway Go Plugin

About Kong Gateway Go Plugins

At Kong Summit 2019, Kong announced that users could develop plugins for Kong Gateway with Go programming language.

To dive deeper into how Kong supports Go programming, visit the Kong Gateway documentation.

Blog Post: Creating Your First Custom Lua Plugin for Kong Gateway >>

How I Built a Kong Gateway Go Plugin

I used declarative configuration (decK) to create my custom Go plugin for Kong Gateway.

In my file, I have services with routes and paths. It will redirect the https://reqres.in/api/users?page=2 URL.

At the same time, I have a custom plugin that will check the API key before the redirect occurs. It will run like a proxy service.

I'll start writing the plugin from my empty Go file called key-checker.go. I'll import the Go PDK file, which allows my Go plugin to access functionality provided by the Kong PDK. To use it, I'll add github.com/kong/go-pdk to the list of packages imported by my plugin.

Next, I'll create a struct for configuration to represent the config parameters in the config.yaml config file.

I need to add a function called "New." Doing this returns an interface.

After that, I'll create a func. In Go programming language, figure out the request and response handlers for the processing lifecycle. I'll make a func called "Access," which gets the requests and responds.

With Func (conf Config) Access(kong *pdk.PDK) type, I'm getting the key parameter from the URL, and I need the parameter in the config file. For example, apiKey := conf.Apikey. If there’s an error, the system will log it.

Docker File: Create a Builder Image and Copy

In the Docker file, I created a builder image and copied my local files into it.

After that, I got the files to build an image into the Kong image.

The Go plugin server's role is to dynamically load the Go plugins and execute their code on demand. Kong handles the go-pluginserver lifecycle. All it needs to know is where to find the go-pluginserver's executable.

In this case, I'll do this by running Docker images using the default path. Therefore, I don’t need an extra parameter. When you build a Kong plugin, you should use the same compiler version, configuration, environment variables, etc., with the Go plugin server. To guarantee consistency, I've built my custom Go plugin server in the same Docker image.

After that, copy the built files into the Kong image.

Run Related Comments on the Terminal

My plugin is ready to run as a container.

After that, I’m going to run the image as a container.

I’m defining some paths, such as plugins directory or configuration files directory. Now we can call our service, and we’ll get a response. It’s running as a container.

I'll call my proxy service. You should see that the proxy service didn’t work because there was an incorrect API key.

I'll try it with the actual API key.

Now you should have reached the correct JSON form of data.

This plugin works like middleware and tries to find the correct key for each request.

What Custom Plugins Will You Build With Go?

Kong Go plugins help enhance your API gateway. In my example, I've just begun to explore how plugins can allow Kong Gateway to gain expanded capabilities. If you'd like a more in-depth look at some of the things you can build, check out the following resources.

  • Kong Gateway documentation
  • Kong Plugin Development Kit – Go edition

Once you've successfully set up Kong Gateway plugins with Go, you may find these other tutorials helpful:

  • How to Use the Kong Gateway JWT Plugin for Service Authentication
  • 4 Steps to Authorizing Services With the Kong Gateway OAuth2 Plugin
  • Getting Started With Kuma Service Mesh

Have questions or want to stay in touch with the Kong community? Join us wherever you hang out:

⭐ Star us on GitHub

🐦 Follow us on Twitter

🌎 Join the Kong Community

🍻 Join our Meetups

❓ ️Ask and answer questions on Kong Nation

💯 Apply to become a Kong Champion

Topics
API GatewayAPI DevelopmentKong Gateway
Share on Social
Mert Simsek

Recommended posts

Unlocking API Analytics for Product Managers

Kong Logo
EngineeringSeptember 9, 2025

Meet Emily. She’s an API product manager at ACME, Inc., an ecommerce company that runs on dozens of APIs. One morning, her team lead asks a simple question: “Who’s our top API consumer, and which of your APIs are causing the most issues right now?”

Christian Heidenreich

Announcing terraform-provider-konnect v3

Kong Logo
Product ReleasesAugust 22, 2025

It’s been almost a year since we released our  Konnect Terraform provider . In that time we’ve seen over 300,000 installs, have 1.7 times as many resources available, and have expanded the provider to include data sources to enable federated managem

Michael Heap

How to Build a Multi-LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 31, 2025

In the last two parts of this series, we discussed How to Strengthen a ReAct AI Agent with Kong AI Gateway and How to Build a Single-LLM AI Agent with Kong AI Gateway and LangGraph . In this third and final part, we're going to evolve the AI Agen

Claudio Acquaviva

How to Build a Single LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 24, 2025

In my previous post, we discussed how we can implement a basic AI Agent with Kong AI Gateway. In part two of this series, we're going to review LangGraph fundamentals, rewrite the AI Agent and explore how Kong AI Gateway can be used to protect an LLM

Claudio Acquaviva

How to Strengthen a ReAct AI Agent with Kong AI Gateway

Kong Logo
EngineeringJuly 15, 2025

This is part one of a series exploring how Kong AI Gateway can be used in an AI Agent development with LangGraph. The series comprises three parts: Basic ReAct AI Agent with Kong AI Gateway Single LLM ReAct AI Agent with Kong AI Gateway and LangGr

Claudio Acquaviva

Build Your Own Internal RAG Agent with Kong AI Gateway

Kong Logo
EngineeringJuly 9, 2025

What Is RAG, and Why Should You Use It? RAG (Retrieval-Augmented Generation) is not a new concept in AI, and unsurprisingly, when talking to companies, everyone seems to have their own interpretation of how to implement it. So, let’s start with a r

Antoine Jacquemin

AI Gateway Benchmark: Kong AI Gateway, Portkey, and LiteLLM

Kong Logo
EngineeringJuly 7, 2025

In February 2024, Kong became the first API platform to launch a dedicated AI gateway, designed to bring production-grade performance, observability, and policy enforcement to GenAI workloads. At its core, Kong’s AI Gateway provides a universal API

Claudio Acquaviva

Ready to see Kong in action?

Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.

Get a Demo
Powering the API world

Increase developer productivity, security, and performance at scale with the unified platform for API management, AI gateways, service mesh, and ingress controller.

Sign up for Kong newsletter

Platform
Kong KonnectKong GatewayKong AI GatewayKong InsomniaDeveloper PortalGateway ManagerCloud GatewayGet a Demo
Explore More
Open Banking API SolutionsAPI Governance SolutionsIstio API Gateway IntegrationKubernetes API ManagementAPI Gateway: Build vs BuyKong vs PostmanKong vs MuleSoftKong vs Apigee
Documentation
Kong Konnect DocsKong Gateway DocsKong Mesh DocsKong AI GatewayKong Insomnia DocsKong Plugin Hub
Open Source
Kong GatewayKumaInsomniaKong Community
Company
About KongCustomersCareersPressEventsContactPricing
  • Terms•
  • Privacy•
  • Trust and Compliance•
  • © Kong Inc. 2025