By on March 17, 2016

Protecting and Rate Limiting an API with Kong

You have just finished building a nice RESTful API, implemented all the endpoints, and you are ready to make it available to potential developers. Usually the next steps before publishing the API are:

  1. Protecting the API, with a security mechanism, ie with an API-key or Basic authentication.
  2. Rate-Limit developers, to avoid them making too many requests to your service.
  3. Whitelist developers, and allow them to make more requests than the others.

With Kong, an open source API gateway, you can easily implement the above features and let it do the hard work for you. And most of all, you can do it quickly.

How does Kong work?

Kong sits in front of your APIs and is the main entry-point to your upstream APIs. While processing the request and the response, Kong will execute any plugin that you have decided to add to the API.




We assume you have already installed Kong on your system.

Adding an API on Kong

The first step is adding an API on Kong using Kong’s admin API. We will use Mockbin as our example API, and assume Kong is running on the local system at the default ports:

Now Kong is aware that every request sent to “/test” should be proxied to Mockbin. We can make the following request to Kong on its proxy port to test it:

And it works. The request has been received by Kong and proxied to Mockbin, not before stripping the “/test” path as instructed with the “strip_request_path” property when adding the API on Kong.

Adding Key Authentication

Now that the API has been added on Kong, we can start adding extra functionality to it by using Kong Plugins. For example, let’s add Key Authentication plugin that expects the developers to send a valid api-key in order to consume the API:

And that’s it. We added key authentication security in a few seconds. Trying to consume the API again will fail this time because no security credentials have been created:

Creating a consumer and a credential

Now that the API has been protected, we need to create a consumer and associate an api-key credential to it. This is very straightforward, let’s create a consumer first:

And then let’s create a new credential for our consumer:

Great, we now have an API, an authentication plugin, a consumer and its credential. We can now consume the API by appending the credential to the request, for example:

And it works! The consumer has been properly authenticated this time and the request went through to the final API.

Adding Rate-Limiting

We have protected our API by using Kong’s Key Authentication plugin, so let’s go ahead and protect it from an excessive number of requests by adding the rate-limiting functionality using the Rate-Limiting plugin, with a limit of 10 requests per minute from every consumer:

And done. If we now make more than 10 requests, Kong will respond with the following error message:

So far so good. We have literally added an API on Kong, protected it with key-authentication, create a consumer and a credential, plus we added rate-limiting in just five HTTP requests to Kong’s admin API.

Customizing the Rate-Limiting per consumer

Let’s say that now we have an enterprise customer who deserves a higher rate-limit. Well, many plugins on Kong can be configured not only per API, but also per API and Consumer, by specifying the “consumer_id” property when adding the plugin.

Our consumer’s ID is “03040614-5cfb-4138-8277-c3f856de1fe8”, so we can now add a customized the Rate-Limiting plugin just for him, with a higher limit of 200 requests per minute:

The above command will allow our consumer “mike” to make up to 200 requests per minute before being blocked by Kong, while still keeping the limit at 10req/minute for every other consumer.


What next?

Kong supports tens of plugins that allow to implement pretty much every common API requirement, from authentication to logging, going through transformations and monitoring. It is stateless, so you can scale your system just by adding more Kong instances, and supports every RESTful API and Microservice.

Want to secure, manage and extend your APIs and Microservices?

Share Post