# express-http-context

> Get and set request-scoped context anywhere

Latest version **2.0.1** (published 2025-04-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-http-context
pnpm add express-http-context
yarn add express-http-context
bun add express-http-context
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-04-19 |
| First published | 2017-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.8.0 |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 313 |
| Author | Steve Konves |
| Maintainers | skonves |
| Keywords | express, http, request, context |

## Links

- npm: https://www.npmjs.com/package/express-http-context
- Repository: https://github.com/skonves/express-http-context
- Homepage: https://github.com/skonves/express-http-context#readme
- Issues: https://github.com/skonves/express-http-context/issues
- Funding: https://github.com/skonves/express-http-context?sponsor=1
- npm.io page: https://npm.io/package/express-http-context

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2025-04-19
- 2.0.0-rc.4 (rc) — 2025-04-02
- 2.0.0 — 2025-04-03
- 2.0.0-rc.3 — 2025-03-29
- 2.0.0-rc.2 — 2025-03-29
- 2.0.0-rc.1 — 2025-03-29
- 2.0.0-rc.0 — 2025-03-17
- 1.2.5 — 2025-03-17
- 1.2.5-rc.0 — 2025-03-16
- 1.2.4 — 2020-07-12
- 1.2.3 — 2019-07-13
- 1.2.2 — 2019-04-27
- 1.2.1 — 2019-03-26
- 1.2.0 — 2018-10-24
- 1.1.0 — 2018-09-17
- … 11 more at https://npm.io/package/express-http-context/versions

## README

[![build](https://img.shields.io/github/actions/workflow/status/skonves/express-http-context/build.yml?branch=master)](https://github.com/skonves/express-http-context/actions/workflows/build.yml)
[![coveralls](https://img.shields.io/coveralls/skonves/express-http-context.svg)](https://coveralls.io/github/skonves/express-http-context)
[![npm](https://img.shields.io/npm/v/express-http-context.svg)](https://www.npmjs.com/package/express-http-context)
[![npm](https://img.shields.io/npm/dm/express-http-context.svg)](https://www.npmjs.com/package/express-http-context)

# Express HTTP Context
Get and set request-scoped context anywhere. This package is an unopinionated, zero-dependency, Express-idiomatic implementation of [Node AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.

## How to use it

Install: `npm i express-http-context`  

Use the context middleware before the first middleware or handler that needs to have access to the context.

``` js
import express from 'express';
import * as httpContext from 'express-http-context';

const app = express();
app.use(httpContext.middleware);
// All code from here on has access to the same context for each request
```

Set values based on the incoming request:

``` js
// Example authentication middleware
app.use(async (req, res, next) => {
	try {
		// Get user from data on request
		const bearer = req.get('Authorization');
		const user = await userService.getUser(bearer);

		// Add user to the request-scoped context
		httpContext.set('user', user);

		return next();
	} catch (err) {
		return next(err);
	}
});
```

Get them from code that doesn't have access to the express `req` object:

``` js
import * as httpContext from 'express-http-context';

// Somewhere deep in the Todo Service
async function createTodoItem(title, content) {
	// Get the user from the request-scoped context
	const user = httpContext.get('user');

	await db.insert({ title, content, userId: user.id });
}
```

## Legacy versions

* For Node <7: `npm install --save express-http-context@0`
* For Node >=8 <12: `npm install --save express-http-context@1`

## Contributors
* Steve Konves (@skonves)
* Amiram Korach (@amiram)
* Yoni Rabinovitch (@yonirab)
* DontRelaX (@dontrelax)
* William Durand (@willdurand)
* Kristopher Morris (@beeduck)

Interesting in contributing? Take a look at the [Contributing Guidlines](/CONTRIBUTING.md)

---
_Source: https://npm.io/package/express-http-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
