Skip to main content
Fiber provides a powerful hooks system that allows you to run custom code at specific points in the application lifecycle.

Hook Types

All hook handler type definitions:
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func(ListenData) error
type OnPreStartupMessageHandler = func(*PreStartupMessageData) error
type OnPostStartupMessageHandler = func(*PostStartupMessageData) error
type OnPreShutdownHandler = func() error
type OnPostShutdownHandler = func(error) error
type OnForkHandler = func(int) error
type OnMountHandler = func(*App) error

Routing Hooks

OnRoute

Executed after each route is registered.
func (h *Hooks) OnRoute(handler ...OnRouteHandler)
handler
...OnRouteHandler
One or more handlers that receive the registered route.
Example
app.Hooks().OnRoute(func(r fiber.Route) error {
    fmt.Printf("Route registered: %s %s\n", r.Method, r.Path)
    return nil
})

OnName

Executed when a route is named.
func (h *Hooks) OnName(handler ...OnNameHandler)
handler
...OnNameHandler
One or more handlers that receive the named route.
Example
app.Hooks().OnName(func(r fiber.Route) error {
    fmt.Printf("Route named: %s -> %s %s\n", r.Name, r.Method, r.Path)
    return nil
})

app.Get("/users/:id", handler).Name("user.show")
OnName only triggers for routes that are explicitly named using .Name().

OnGroup

Executed after each route group is created.
func (h *Hooks) OnGroup(handler ...OnGroupHandler)
handler
...OnGroupHandler
One or more handlers that receive the created group.
Example
app.Hooks().OnGroup(func(g fiber.Group) error {
    fmt.Printf("Group created: %s\n", g.Prefix)
    return nil
})

api := app.Group("/api")

OnGroupName

Executed when a group is named.
func (h *Hooks) OnGroupName(handler ...OnGroupNameHandler)
handler
...OnGroupNameHandler
One or more handlers that receive the named group.
OnGroupName only triggers for groups that are explicitly named.

Server Lifecycle Hooks

OnListen

Executed when the server starts listening.
func (h *Hooks) OnListen(handler ...OnListenHandler)
handler
...OnListenHandler
One or more handlers that receive ListenData.
Example
app.Hooks().OnListen(func(data fiber.ListenData) error {
    scheme := "http"
    if data.TLS {
        scheme = "https"
    }
    fmt.Printf("Server listening on %s://%s:%s\n", scheme, data.Host, data.Port)
    return nil
})

ListenData

Contains information about the listening server:
Host
string
The hostname or IP address the server is bound to.
Port
string
The port number the server is listening on.
TLS
bool
Whether TLS is enabled.
Version
string
The Fiber version.
AppName
string
The application name from Config.
HandlerCount
int
Total number of registered handlers.
ProcessCount
int
Number of processes (relevant in prefork mode).
PID
int
Current process ID.
Prefork
bool
Whether prefork mode is enabled.
ChildPIDs
[]int
Child process IDs when using prefork.
ColorScheme
Colors
Active color scheme for startup message.

OnPreStartupMessage

Executed before printing the startup banner.
func (h *Hooks) OnPreStartupMessage(handler ...OnPreStartupMessageHandler)
handler
...OnPreStartupMessageHandler
One or more handlers that receive PreStartupMessageData.
Example
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
    sm.BannerHeader = "MY APP " + sm.Version
    sm.AddInfo("environment", "Environment", os.Getenv("ENV"))
    sm.AddWarning("debug", "Debug Mode", "Enabled", 10)
    return nil
})

PreStartupMessageData Methods

AddInfo
Add an informational entry to the startup banner.
func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int)
key
string
Unique identifier for this entry.
title
string
Display label for the entry.
value
string
The value to display.
priority
int
Display priority (higher numbers appear first).
AddWarning
Add a warning entry to the startup banner.
func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int)
key
string
Unique identifier for this entry.
title
string
Display label for the entry.
value
string
The value to display.
priority
int
Display priority (higher numbers appear first).
AddError
Add an error entry to the startup banner.
func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int)
key
string
Unique identifier for this entry.
title
string
Display label for the entry.
value
string
The value to display.
priority
int
Display priority (higher numbers appear first).
EntryKeys
Get all entry keys currently in the startup message.
func (sm *PreStartupMessageData) EntryKeys() []string
[]string
[]string
Returns all entry keys.
DeleteEntry
Remove an entry from the startup message.
func (sm *PreStartupMessageData) DeleteEntry(key string)
key
string
The key of the entry to remove.
ResetEntries
Remove all entries from the startup message.
func (sm *PreStartupMessageData) ResetEntries()

PreStartupMessageData Fields

BannerHeader
string
Custom ASCII art or header text. Leave empty for default Fiber banner.
PreventDefault
bool
Set to true to suppress the default startup message entirely.

OnPostStartupMessage

Executed after the startup banner is printed (or skipped).
func (h *Hooks) OnPostStartupMessage(handler ...OnPostStartupMessageHandler)
handler
...OnPostStartupMessageHandler
One or more handlers that receive PostStartupMessageData.
Example
app.Hooks().OnPostStartupMessage(func(sm *fiber.PostStartupMessageData) error {
    if !sm.Disabled && !sm.IsChild {
        fmt.Println("Server started successfully!")
    }
    return nil
})

PostStartupMessageData Fields

Disabled
bool
True if startup message was disabled via DisableStartupMessage config.
IsChild
bool
True if this is a child process (prefork mode).
Prevented
bool
True if PreventDefault was set in OnPreStartupMessage.

OnPreShutdown

Executed before the server begins shutdown.
func (h *Hooks) OnPreShutdown(handler ...OnPreShutdownHandler)
handler
...OnPreShutdownHandler
One or more handlers executed before shutdown.
Example
app.Hooks().OnPreShutdown(func() error {
    fmt.Println("Server shutting down...")
    // Close database connections, etc.
    return nil
})

OnPostShutdown

Executed after the server has shut down.
func (h *Hooks) OnPostShutdown(handler ...OnPostShutdownHandler)
handler
...OnPostShutdownHandler
One or more handlers that receive the shutdown error (if any).
Example
app.Hooks().OnPostShutdown(func(err error) error {
    if err != nil {
        fmt.Printf("Shutdown error: %v\n", err)
    } else {
        fmt.Println("Server shut down cleanly")
    }
    return nil
})

Process Hooks

OnFork

Executed in child processes when using prefork mode.
func (h *Hooks) OnFork(handler ...OnForkHandler)
handler
...OnForkHandler
One or more handlers that receive the worker ID.
Example
app.Hooks().OnFork(func(workerID int) error {
    fmt.Printf("Worker %d started\n", workerID)
    return nil
})

OnMount

Executed when a sub-application is mounted.
func (h *Hooks) OnMount(handler ...OnMountHandler)
handler
...OnMountHandler
One or more handlers that receive the parent App.
Example
subApp := fiber.New()

subApp.Hooks().OnMount(func(parent *fiber.App) error {
    fmt.Printf("Mounted at: %s\n", parent.MountPath())
    return nil
})

app.Use("/api", subApp)
OnName, OnRoute, OnGroup, and OnGroupName hooks are mount-aware. When a sub-app with these hooks is mounted, route and group paths include the mount prefix.

Example: Complete Hook Setup

Example
app := fiber.New()

// Route registration hooks
app.Hooks().OnRoute(func(r fiber.Route) error {
    fmt.Printf("[Route] %s %s\n", r.Method, r.Path)
    return nil
})

app.Hooks().OnName(func(r fiber.Route) error {
    fmt.Printf("[Named] %s -> %s\n", r.Name, r.Path)
    return nil
})

// Startup hooks
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
    sm.AddInfo("env", "Environment", os.Getenv("ENV"))
    return nil
})

app.Hooks().OnListen(func(data fiber.ListenData) error {
    fmt.Printf("Listening on %s:%s\n", data.Host, data.Port)
    return nil
})

// Shutdown hooks
app.Hooks().OnPreShutdown(func() error {
    fmt.Println("Cleaning up resources...")
    return nil
})

app.Hooks().OnPostShutdown(func(err error) error {
    fmt.Println("Shutdown complete")
    return nil
})

// Mount hooks
subApp := fiber.New()
subApp.Hooks().OnMount(func(parent *fiber.App) error {
    fmt.Printf("Sub-app mounted at %s\n", parent.MountPath())
    return nil
})

app.Use("/v1", subApp)

Build docs developers (and LLMs) love