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. Building a geocoding plugin for the Kong Gateway using Python
Engineering
June 3, 2022
5 min read

Building a geocoding plugin for the Kong Gateway using Python

Shrikanth Rajgopalan
Topics
Kong GatewayPluginsOpen Source
Share on Social

More on this topic

Videos

Service Catalog with Traceable AI

Videos

Unlock Microservices Mastery: OpenTelemetry in Kong Gateway

See Kong in action

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

Get a Demo

Recently, I came across a request from one of our community users looking for a Kong plugin that could look up address details based on a few location search keywords. There are many libraries that support geocoding. I decided to pick up a Python Library as they are simple to use — and the Kong Gateway supports plugins written in Python since version 2.3.

To show the simplicity of using the Kong Python PDK, I’ve picked the GeoPy library, which will allow us to perform GeoCoding within our Kong Plugin.

When making an API request to Kong, the caller should be able to pass location search keywords such as "Trafalgar Square" in the request header. The plugin will use the GeoPy library to lookup the address for the search terms, determine the latitude and longitude for the address, and add them as additional headers to the request before passing it to the upstream.

Bootstrapping your development environment

The Python plugin support in Kong Gateway works by running a Python plugin server as a separate process on the same host as Kong Gateway and passing messages back and forth using Unix Sockets. The same mechanism is used for other external plugins in Kong, such as JS and Go.

In order to make things easier, I've put together a Docker-based environment for you to use. All you require is a machine with Docker and Docker Compose installed. Since it might take a minute or two to download the images and build our environment, I recommend running it now in the background as you keep reading:

The Docker Compose file sets up the following:

  • Creates a custom Kong image from the base image, installing the Kong Python PDK as well as the GeoPy Python library
  • Starts up a Kong Gateway instance in the DB-less mode — the plugin will run with the DB-backed mode as well, but I wanted to use DBless to keep our sample lean
  • Mounts the Kong declarative config and Python plugin file to the Kong instance using Docker volumes

Exploring the plugin

The Kong Gateway is configured to start a Python plugin server and load all the plugins in the kong-py-plugin directory. Currently, it contains a single plugin file: py-geocode.py. We start our plugin by importing the required Python libraries:

These Python libraries should be installed using pip in our Dockerfile used to build our Kong image.

Like all well-written plugins, the py-geocode plugin exposes all its parameters via configuration 😎

  • The request header where the location search term will be specified
  • The upstream header which will contain the completed address
  • The upstream headers with the location latitude and longitude

We also specify the version and priority of the plugin. We start by implementing the plugin object and the phases where we want to implement our custom logic.

There are five phases available for HTTP requests in the life-cycle of a Kong Gateway request:

  1. certificate - Executed once per request when the connection is SSL/TLS enabled for the proxy
  2. rewrite - Performed before the API gateway does any routing of the request to the upstream
  3. access - All routing is identified, and the plugin knows which service the request is bound to. This is the last phase before the API gateway sends the request to the upstream
  4. response - Allows you to manipulate the response from the upstream. Implementing this phase has a performance penalty as it enables request buffering
  5. log - Executed after the request has been completed

Since we want to add headers to the request before sending it to the upstream, we do it in the access phase:

We'll also be setting default values for these various headers if they're not set in plugin configuration. After all, we want our plugin to be resilient 💪

Here's a snippet:

Finally, we perform our custom logic that:

  • Extracts the location search keywords from the request header
  • Uses the GeoPy library to get the location details
  • Adds the upstream headers for the location address, latitude and longitude

We wrap all the logic in a try-except block for proper exception handling. This ensures that our plugin will not crash and burn when it hits an error 👊

Enabling the Plugin

The environment we're running uses Kong's declarative configuration capability. This means our Kong configuration is defined as a YAML file. Open up config/kong.yml, and you should see a service defined that proxies to httpbin.org/anything, and exposes a single route on the path "/any" that has our geocoding plugin applied:

The Kong Gateway only allows you to use plugins that are on an allowlist for security purposes. Open up docker-compose.yml and you can see that our plugin "py-geocode" has been enabled:

Finally, we enable the python plugin server using environment variables used to start the gateway container.

Trying it out

At this point the API gateway is ready to run our Python plugin, so let's go ahead and start it:

Once the containers are up, the Kong Gateway's proxy port will be available on port 8000 on our local machine.

Lets try a simple location search, specifying the location search keywords in the HTTP header "x-location-search" which is the default header that our plugin will look at. (We've not specified any plugin configuration in our declarative configuration.)

As you can see, our custom plugin has looked up the location address, latitude, and longitude and added them as upstream headers which are reflected by the httpbin response.

Specifying plugin configuration

Suppose, we want the plugin to lookup the search keywords in the header "x-search" — and we want the location address to be added to a header "x-location-addr". We can set these in the plugin configuration, because remember: we wrote our plugin to be intelligent. Head over to the declarative configuration in the dbless_config directory and edit kong.yaml to specify these in the plugin configuration:

If we restart our API gateway by pressing Ctrl+C then running docker-compose up again, we should now be able to make a request to localhost:8000, specifying the search location in the ‘x-search' header:

Conclusion

In a few lines of Python code, we have a working Kong Gateway plugin, complete with configuration options and exception handling!

What we've built together is a trivial plugin, but using the environment provided and what you've learned about Kong's configuration, you can go ahead and build plugins to your heart's content making full use of the wide range of libraries provided by the Python ecosystem.

If you're looking for more plugin examples, take a look at the examples directory in the kong-python-pdk.

Questions? Post them on Kong Nation. And stay in touch by joining the Kong Community.

Topics
Kong GatewayPluginsOpen Source
Share on Social
Shrikanth Rajgopalan

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