How to add routes idempotently via a script

HI all

I am converting our apis to routes and services that are deployed via a configuration script but I am having trouble adding routes idempotently. This script is used for first time creation but also for updating deployments with new config when required.

Services (and apis & consumers for that matter) use a name that defines them and so can be used to determine if it exists and create (POST) it if it does not exist or update (DELETE & POST or PATCH) it. Alternatively the PUT can be used to create or update it in one command.

However I am not sure how this works on routes as there is no name and the only way to get details on a route is via the internally generated id for it. If I keep doing a POST of the same details it just keeps adding a new route for the specified service. All the other commands for routes operate on the id only so how do I determine if the route i am creating already exists without having to save its generated id in some external location the first time it is created. The PUT command for example that add or updates requires the id in the admin URL but if its being created for the first time you dont know it.

Am i missing something here ? Will routes have names in the future to support this ?



Yes, Routes have name starting with Kong 1.0rc1 and forward.


intall jq in your command line (

getcmd=“http: //localhost:8001/services//routes”
json=$(curl $getcmd)
use jq to get your route id.
if found not run post, else run your post

1 Like

We faced this problem developing Kongverge.

My best answer is to use Kongverge! </shameless_plug>

But yes, we effectively pulled down the routes from Kong, and compared all the properties to determine if the route was equivalent before upserting, or removing and re-adding.