# Custom Plugin execution order

**URL:** <https://discuss.konghq.com/t/custom-plugin-execution-order/6510>\
**Category:** General\
**Created:** [June 12, 2020, 7:58pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510 "2020-06-12T19:58:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 12, 2020, 7:58pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/1 "2020-06-12T19:58:12Z")

</div>

Is there any way to control plugin execution order? e.g. If I want to run custom plugin xyz, right after my JWT plugin executes or right before rate-limiting plugin. How can I achieve it?

Also, is there any way to inject any script() or code in existing kong plugin to perform some additional stuff. e.g. in JWT plugin apart from authentication I want to do some other checks too.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 15, 2020, 5:45pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/2 "2020-06-15T17:45:51Z")

</div>

The plugin priorities are currently hard-coded:  
[https://docs.konghq.com/2.0.x/plugin-development/custom-logic/#plugins-execution-order](https://docs.konghq.com/2.0.x/plugin-development/custom-logic/#plugins-execution-order)

We do want to change these to make them dynamic so that you can more easily define how plugins should be executed.

> [@on\_fla](#):
>
> Also, is there any way to inject any script() or code in existing kong plugin to perform some additional stuff. e.g. in JWT plugin apart from authentication I want to do some other checks too.

No, this is not possible. You can clone the plugin and create a custom plugin based on the what we ship in OSS.

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 16, 2020, 3:50pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/3 "2020-06-16T15:50:28Z")

</div>

Thank you for the clarification.  
I have follow up questions on related topic.

1. Where can I find hard coded plugin execution order?
2. Which plugin does route matching for a incoming request?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 16, 2020, 5:28pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/4 "2020-06-16T17:28:57Z")

</div>

> [@on\_fla](#):
>
> Where can I find hard coded plugin execution order?

In the above doc that I linked. 🙂

> [@on\_fla](#):
>
> Which plugin does route matching for a incoming request?

That happens in the core logic of Kong. Route matching happens before a pluign is executed in the `access` phase.

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 16, 2020, 6:41pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/5 "2020-06-16T18:41:44Z")

</div>

It is really helpful to get right information. Thanks for the prompt reply.

So, route matching is not implemented in any of plugins? route matching happens before any plugin is executed?

My use case is, add some custom logic, right after the route is matched.  
Is there any suggestion to utilize existing route matching functionality from kong and add some additional custom stuff once route is matched? [https://docs.konghq.com/1.0.x/pdk/kong.router/](https://docs.konghq.com/1.0.x/pdk/kong.router/) ?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 16, 2020, 9:06pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/6 "2020-06-16T21:06:41Z")

</div>

> [@on\_fla](#):
>
> So, route matching is not implemented in any of plugins? route matching happens before any plugin is executed?

Correct. There are exceptions to this but those are rare and not recommended.

> [@on\_fla](#):
>
> My use case is, add some custom logic, right after the route is matched.

Add a plugin with higher priority than zipkin. That should ensure that your plugin is executed after route matching is performed.

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 16, 2020, 10:40pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/7 "2020-06-16T22:40:08Z")

</div>

Thanks.

[https://docs.konghq.com/2.0.x/pdk/kong.router/](https://docs.konghq.com/2.0.x/pdk/kong.router/) This will give current matched route. Thus route matching can be mimicked in custom plugin by using this. Once matching route is found, any additional operations can be performed. No need to maintain the plugin execution order.

Is my understanding correct?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 17, 2020, 7:07pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/8 "2020-06-17T19:07:21Z")

</div>

> [@on\_fla](#):
>
> Is my understanding correct?

No.

Can you explain in detail what is it that you are trying to do?

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 17, 2020, 7:37pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/9 "2020-06-17T19:37:17Z")

</div>

Use-case: Get matched route and perform some operations on matched route.

I was thinking of two approaches.

1. Override native plugin of route matching and add additional functionality apart from native logic.  
Now, route matching functionality is performed in the core logic and not in plugins. So, this won’t work.

2. Introduce a custom plugin, somehow retrieve the matched route, perform additional functionality.

As per, [https://docs.konghq.com/2.0.x/pdk/kong.router/](https://docs.konghq.com/2.0.x/pdk/kong.router/), I can retrieved current matched route in custom plugin and add required additional operations there.

Plugin Execution does required in 2nd approach.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 18, 2020, 9:51pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/10 "2020-06-18T21:51:53Z")

</div>

> [@on\_fla](#):
>
> perform some operations

Can you elaborate on what you mean by “some operation”?

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 18, 2020, 10:07pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/11 "2020-06-18T22:07:06Z")

</div>

Add custom authorization. Add some metrics.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [June 18, 2020, 11:17pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/12 "2020-06-18T23:17:23Z")

</div>

You don’t need to hook into router for that purpose.  
The router is used for changing the route that was matched.  
In your case, you need to define a route and attach a plugin to that route. The plugin will be executed only when there is a request that matches the route you had defined.

---

<div class="post-metadata">

**Author:** ![111](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/111/32/2038_2.png) [@111](https://discuss.konghq.com/u/111)\
**Post date:** [June 19, 2020, 6:23am UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/14 "2020-06-19T06:23:44Z")

</div>

I use “kong.response.exit(res.status, res.body)”  
when res.status=200，The interface is open ，no problem  
but when res.status!=200,have questions  
questions is No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Did I configure it right？  
thank you~

---

<div class="post-metadata">

**Author:** ![on\_fla](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@on\_fla](https://discuss.konghq.com/u/on_fla)\
**Post date:** [June 19, 2020, 4:04pm UTC](https://discuss.konghq.com/t/custom-plugin-execution-order/6510/15 "2020-06-19T16:04:10Z")

</div>

Can you please initiate different thread for your question?
