Simulates a thread of execution to allow for true session context to take place per api call within the fastify lifecycle of calls.
Last updated a month ago by thorough-developer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fastify-http-context 
SYNC missed versions from official npm registry.

fastitfy-http-context Package

Fastify HTTP Context

This plugin was inspired by express-http-context, but works more seamlessly within the fastify ecosystem.

The purpose of this fastify plugin is to easily add a true thread local http context, where any variables set within the scope of a single http call won't be overwritten by simultaneous calls to the api nor will variables remain to be assumed by subsequent calls once a request is completed.

This is ideal when you need to, for instance, set a user in a hook, and then retrieve that user later on to add them as the createdBy or modifiedBy user later in subsequent calls. This plugin will ensure that the user who made the call is the user that is retrieved later on.

Getting started

First install the package:

npm i fastify-http-context

Next, set up the plugin:

const { fastifyHttpContextPlugin } = fastify-http-context
const fastify = require('fastify');

fastify.register(fastifyHttpContextPlugin, { defaults: user: { id: 'system' } };

This plugin takes in one of two option named defaults and hook.

defaults are what the values should be if not set. This is optional and not necessary. There are cases where defaults are not wanted nor necessary.

hook is one of the fastify lifecycle hooks. By default it is set to onRequest. Some times where the context is run from matters. For instance, if using a library like Typeorm the context gets lost in Subscribers if the context is run in onRequest but does not get lost if run in preHandler (not sure why but that is the motivation for allowing the hook to be configurable).

From there you can set a context in another hook, route, or method that is within scope. For instance:

const { fastifyHttpContextPlugin, httpContext } = require('fastify-http-context');

const fastify = require('fastify')();

fastify.register(fastifyHttpContextPlugin, {
 hook: 'onRequest', // this is optional. If not set will default to onRequest
 defaults: {
  user: {
   id: 'system'

fastify.addHook('onRequest', (req, reply, done) => {
  // overwrite the defaults
  httpContext.set('user', { id: 'helloUser' });

// this should now get the user id of helloUser instead of the default
fastify.get('/', (req, reply) => {
  const user = httpContext.get('user');
  reply.code(200).send( { user });

fastify.listen(3000, (err, address) => {
  if (err) throw err`server listening on ${address}`)

Current Tags

  • 2.0.0                                ...           latest (a month ago)

9 Versions

  • 2.0.0                                ...           a month ago
  • 1.1.0                                ...           a month ago
  • 1.0.6                                ...           a month ago
  • 1.0.5                                ...           a month ago
  • 1.0.4                                ...           a month ago
  • 1.0.3                                ...           a month ago
  • 1.0.2                                ...           a month ago
  • 1.0.1                                ...           a month ago
  • 1.0.0                                ...           a month ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 92
Dependencies (2)
Dev Dependencies (10)
Dependents (0)

Copyright 2014 - 2017 © |