Skip to main content
Version: v3.x

EnvVar

EnvVar middleware for Fiber exposes environment variables with configurable options.

Signatures​

func New(config ...Config) fiber.Handler

Examples​

Import the middleware package:

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/envvar"
)

Once your Fiber app is initialized, configure the middleware as shown:

// Initialize default config (exports no variables)
app.Use("/expose/envvars", envvar.New())

// Or extend your config for customization
app.Use("/expose/envvars", envvar.New(
envvar.Config{
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
}),
)
note

Mount the middleware on a path; it cannot be used without one.

Only GET and HEAD requests are served. Any other method receives 405 Method Not Allowed with an Allow: GET, HEAD header.

Response​

Sample response:

{
"vars": {
"someEnvVariable": "someValue",
"anotherEnvVariable": "anotherValue"
}
}

Config​

PropertyTypeDescriptionDefault
ExportVarsmap[string]stringExportVars lists the environment variables to expose. Each key is a variable name and each value is the default reported when that variable is not set.map[string]string{}

Default Config​

var ConfigDefault = Config{
ExportVars: map[string]string{},
}
// Exports no environment variables