How to Configure Dynamic Path in Krakend API Gateway

Published 2021-10-04

Why? (The Motivation Behind)

It's very common that our API endpoints have dynamic path such as users/1 and users/2. The value 1 and 2 often represent the unique ID of the user so backend services will query the requested user information and return to the caller.

But, the unique ID here is different for each user which means it is dynamic. So we need to configure in our Krakend endpoints to specify this is the dynamic value in our url pattern.

Let's see the example below.


The below API Gateway have a dynamic endpoint /v1/git-user/{username}. This dynamic endpoint allow us to query the Github user based on the username we insert in the URL path.

The dynamic variable is defined using curly braces, {username}. We will reuse the same defined variable in the url_pattern.

{ "version": 2, "endpoints": [ { "endpoint": "/v1/git-user/{username}", "backend": [ { "url_pattern": "/users/{username}", "host": [""] } ] } ] }

Let's see some response to see if it working.

curl http://localhost:8080/v1/git-user/tlcheah2 {"avatar_url":"","bio":"Javascript Developer. I explain via code and words. \r\nPersonal Mantra: Life is about trying stuff, validating stuff and retry some stuff if it is not working","blog":"","company":"Freelancer","created_at":"2014-05-01T05:44:09Z","email":null,"events_url":"{/privacy}","followers":9,"followers_url":"","following":0,"following_url":"{/other_user}","gists_url":"{/gist_id}","gravatar_id":"","hireable":null,"html_url":"","id":7456033,"location":"Kuala Lumpur, Malaysia","login":"tlcheah2","name":"Tek Loon","node_id":"MDQ6VXNlcjc0NTYwMzM=","organizations_url":"","public_gists":75,"public_repos":63,"received_events_url":"","repos_url":"","site_admin":false,"starred_url":"{/owner}{/repo}","subscriptions_url":"","twitter_username":"TekLoonCheah","type":"User","updated_at":"2021-06-29T02:14:03Z","url":""}

I query my own Github profile and it is working as expected!! 🎉🎉