📗
Janus Manual
  • Introduction
  • Installation
    • Docker
  • Quick Start
    • Authenticating
    • Add an endpoint
    • Modify (Update/Delete) an endpoint
    • Add Plugins
    • Authentication
    • Adding your API - File System
  • Clustering/HA
  • Proxy Reference
    • Terminology
    • Overview
    • Routing capabilities
    • Load Balacing
    • Request Host header
      • Using wildcard hostnames
      • The preserve_host property
    • Request URI
      • The strip_path property
      • The append_path property
    • Request HTTP method
    • Routing priorities
    • Conclusion
  • Plugins
    • Basic
    • Organization
    • Body Limit
    • Circuit Breaker
    • Compression
    • CORS
    • OAuth
    • Rate Limit
    • Request Transformer
    • Response Transformer
    • Retry
  • Auth
    • OAuth 2.0
  • Misc
    • Health Checks
    • Monitoring
    • Tracing
  • Known Issues
    • Stale HTTP Keep-Alive
  • Upgrade Notes
    • 2.x to 3.x
    • 3.6.x to 3.7.x
Powered by GitBook
On this page
  • 1. Add the plugin
  • 2. Check if the plugin is working

Was this helpful?

  1. Quick Start

Add Plugins

PreviousModify (Update/Delete) an endpointNextAuthentication

Last updated 4 years ago

Was this helpful?

Any API Definition can make use of plugins. A plugin is a way to add functionalities to your definition, like rate limit, CORS, authentication, etc...

In this tutorial we will add a plugin to our definition.

1. Add the plugin

Let's create a file called rate_limit.json with our new additions to the API definition. We will create a rate limit rule that we can only send 5 req/m just for us to play around with it.

{
    "name" : "my-endpoint",
    "active" : true,
    "proxy" : {
        "listen_path" : "/example/*",
        "upstreams" : {
            "balancing": "roundrobin",
            "targets": [
                {"target": "http://www.mocky.io/v2/595625d22900008702cd71e8"}
            ]
        },
        "methods" : ["GET"]
    },
    "plugins": [
        {
            "name": "rate_limit",
            "enabled": true,
            "config": {
                "limit": "5-M",
                "policy": "local"
            }
        }
    ]
}

Now lets update our API definition:

http -v PUT localhost:8081/apis/my-endpoint "Authorization:Bearer yourToken" "Content-Type: application/json" < rate_limit.json

curl -X "PUT" localhost:8081/apis/my-endpoint -H "Authorization:Bearer yourToken" -H "Content-Type: application/json" -d @rate_limit.json

Done! Now Janus already reloaded the configuration and the rate limit is enabled on your endpoint.

2. Check if the plugin is working

Lets make a few requests to our endpoint and see if the rate limit is working.

You should see a few extra headers on the response of this request:

X-Ratelimit-Limit →5
X-Ratelimit-Remaining →4
X-Ratelimit-Reset →1498773715

This means the plugin is working properly. Now lets make the same request 4 more times... On the fifth time you should get:

Status Code: 429 Too Many Requests

Limit exceeded

After 1 minute you should be able to make 5 more requests :)

http -v GET

curl -X "GET"

In the we'll learn how to protect our endpoint.

rate limit
http://localhost:8080/example
http://localhost:8080/example
next part