While developing an application, we often need to separate the environment variables from the source code. The environment variables could include things like database credentials, JWT secrets or other application secrets. In this post, we will create a Fastify Env example using the fastify-env package.

The fastify-env package is basically a Fastify plugin to handle environment variables using the .env file approach.

People coming from Express background are used to working with .env file. Basically, the fastify-env package helps that proceed with the same approach while moving over to Fastify.

1 – Installing the fastify-env package

The first step is to install the fastify-env package. This module is a wrapper around the env-schema package.

$ npm install --save fastify-env

Once the installation is complete, we can move towards the configuration part.

2 – Configuring the Fastify Env Example

The first step is to define a schema in our Fastify application code.

const fastifyEnv = require('@fastify/env')

const schema = {
  type: 'object',
  required: [ 'MSG' ],
  properties: {
    MSG: {
      type: 'string',
      default: 'Hello, World'
    }
  }
}

The schema is an object that describes the structure of the .env file.

The object contains a few important parameters. First is the type parameter with a value as object. Next, we have the list of required environment properties. Finally, we have the properties object.

Basically, we use this object to describe every environment variable along with its type and default values (if any).

As an example, we have one environment property known as MSG of type string. Below is the .env file for the same.

MSG=Welcome to Fastify

Next, we define another object known as options. Basically, this tells Fastify how to access the environment file. For simply using the .env file, we can declare the options object as below.

const options = {
  confKey: 'config',
  dotenv: true,
  schema: schema,
  data: process.env
}

The dotenv flag is set to true. The confKey specifies the name of the property that will contain the configuration.

For the data property, we link it to the NodeJS process.env object. Also, we set the schema property to the schema object.

Lastly, we simply need to pass the options object as part of register() method. In the ready() callback, we print the environment properties using fastify.config.

fastify
  .register(fastifyEnv, options)
  .ready((err) => {
    if (err) console.error(err)
    console.log(fastify.config) 
})

Conclusion

Fastify Env setup example using fastify-env package is a great way to handle environment variables in your applications. It makes the handling of environment properties part of the overall fastify context rather than a standalone feature.

Want to use Fastify in a bigger application? Check out this post on building a Fastify REST API with Postgres.

If you have any comments or queries about this post, feel free to mention them in the comments section below.

Categories: BlogFastify

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *