> For the complete documentation index, see [llms.txt](https://bootcamp.openguild.wtf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bootcamp.openguild.wtf/building-a-blockchain-with-polkadot-sdk/polkadot-sdk/substrate/adding-a-custom-logic-to-runtime/hooks.md).

# Hooks

{% embed url="<https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/trait.Hooks.html>" %}
Documentation about Pallet hooks
{% endembed %}

<figure><img src="https://2688244615-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqpKohKlriBQm0I9cc9k5%2Fuploads%2Fu2WXN9OHnkFqkAcDfsVr%2FScreenshot%202024-11-04%20at%2007.41.55.png?alt=media&amp;token=546def45-cf3c-4a73-8fd3-700eb9eda1aa" alt=""><figcaption></figcaption></figure>

* [**OnInitialize**](https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/trait.Hooks.html#method.on_initialize) hooks are only executed when a code change is detected.
* [`OnRuntimeUpgrade`](hooks::OnRuntimeUpgrade) hooks are mandatorily executed at the very beginning of the block body, before any extrinsics are processed.
* [`Inherents`](https://paritytech.github.io/polkadot-sdk/master/sp_inherents/index.html) are always executed before any other other signed or unsigned extrinsics.
* [`OnIdle`](hooks::OnIdle) hooks are executed after extrinsics if there is weight remaining in the block.
* [`OnFinalize`](hooks::OnFinalize) hooks are mandatorily executed after [`OnIdle`](hooks::OnIdle).
