# jwt-service

> A simple wrapper for a simpler JWT surface API

Latest version **12.0.2** (published 2026-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install jwt-service
pnpm add jwt-service
yarn add jwt-service
bun add jwt-service
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 12.0.2 |
| Published | 2026-04-17 |
| First published | 2018-09-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24.14.0 |
| Dependencies | 7 |
| Unpacked size | 71.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Nicolas Froidure |
| Maintainers | nfroidure |
| Keywords | jwt, knifecycle |

## Links

- npm: https://www.npmjs.com/package/jwt-service
- Repository: https://github.com/nfroidure/jwt-service
- Homepage: https://github.com/nfroidure/jwt-service#readme
- Issues: https://github.com/nfroidure/jwt-service/issues
- Funding: https://github.com/sponsors/nfroidure
- npm.io page: https://npm.io/package/jwt-service

## Dependencies (7)

- [ms](https://npm.io/package/ms.md) ^2.1.3
- [yerror](https://npm.io/package/yerror.md) ^11.0.0
- [@types/ms](https://npm.io/package/@types/ms.md) ^2.1.0
- [knifecycle](https://npm.io/package/knifecycle.md) ^21.1.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.3
- [common-services](https://npm.io/package/common-services.md) ^20.0.0
- [@types/jsonwebtoken](https://npm.io/package/@types/jsonwebtoken.md) ^9.0.8

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 12.0.2 (latest) — 2026-04-17
- 2.1.0-beta.0 (beta) — 2019-01-20
- 12.0.1 — 2026-04-07
- 12.0.0 — 2026-03-27
- 11.0.3 — 2025-11-06
- 11.0.2 — 2025-02-06
- 11.0.1 — 2025-02-04
- 11.0.0 — 2024-12-04
- 10.1.1 — 2024-07-15
- 10.1.0 — 2024-05-27
- 10.0.4 — 2023-08-20
- 10.0.3 — 2023-08-20
- 10.0.2 — 2023-08-17
- 10.0.1 — 2023-08-16
- 10.0.0 — 2023-08-12
- … 25 more at https://npm.io/package/jwt-service/versions

## README

[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it  except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# jwt-service
> A simple wrapper for a simpler JWT surface API

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/jwt-service/blob/main/LICENSE)


[//]: # (::contents:start)

This wrapper is directly usable with
[Knifecycle](https://github.com/nfroidure/knifecycle).

## Overriding

Let's say you wanna create a sub-service of this one with custom inputs, for
say, refresh tokens in a [Whook](https://github.com/nfroidure/whook) project,
here is how you would do:

```ts
import initJWT, { JWTServiceConfig } from 'jwt_service';
import { inject } from 'knifecycle';

export type RefreshJWTServiceConfig = {
  ENV: JWTServiceConfig['ENV'];
  REFRESH_JWT_SECRET_ENV_NAME: JWTServiceConfig['JWT_SECRET_ENV_NAME'];
  REFRESH_JWT: JWTServiceConfig['JWT'];
};

export default inject(
  [
    '?JWT_SECRET_ENV_NAME>REFRESH_JWT_SECRET_ENV_NAME',
    'JWT>REFRESH_JWT',
    '?ENV',
    '?log',
    '?time',
  ],
  initJWT,
);
```

[//]: # (::contents:end)

# API
## Functions

<dl>
<dt><a href="#initJWT">initJWT(services)</a> ⇒ <code><a href="#JWTService">Promise.&lt;JWTService&gt;</a></code></dt>
<dd><p>Instantiate the JWT service</p>
</dd>
</dl>

## Typedefs

<dl>
<dt><a href="#JWTService">JWTService</a></dt>
<dd></dd>
</dl>

<a name="initJWT"></a>

## initJWT(services) ⇒ [<code>Promise.&lt;JWTService&gt;</code>](#JWTService)
Instantiate the JWT service

**Kind**: global function  
**Returns**: [<code>Promise.&lt;JWTService&gt;</code>](#JWTService) - A promise of the jwt service  

| Param | Type | Description |
| --- | --- | --- |
| services | <code>Object</code> | The services to inject |
| [services.JWT_SECRET_ENV_NAME] | <code>function</code> | The environment variable name in which to pick-up the  JWT secret |
| [services.ENV] | <code>Object</code> | An environment object |
| services.JWT | <code>function</code> | The JWT service configuration object |
| [services.log] | <code>function</code> | A logging function |
| [services.time] | <code>function</code> | A function returning the current timestamp |

**Example**  
```js
import initJWTService from 'jwt-service';

const jwt = await initJWTService({
  JWT: {
    secret: 'secret',
    duration: '2d',
    tolerance: '2h',
    algorithms: ['HS256'],
  },
  log: console.log.bind(console),
  time: Date.now.bind(Date),
});

const token = await jwt.sign({ my: 'payload' });
```
<a name="JWTService"></a>

## JWTService
**Kind**: global typedef  

* [JWTService](#JWTService)
    * [.sign(payload, [algorithm])](#JWTService.sign) ⇒ <code>Promise.&lt;JWTSignResult&gt;</code>
    * [.verify([token])](#JWTService.verify) ⇒ <code>Promise.&lt;Object&gt;</code>

<a name="JWTService.sign"></a>

### JWTService.sign(payload, [algorithm]) ⇒ <code>Promise.&lt;JWTSignResult&gt;</code>
Sign the given payload

**Kind**: static method of [<code>JWTService</code>](#JWTService)  
**Returns**: <code>Promise.&lt;JWTSignResult&gt;</code> - A promise to be resolved with the signed token.  

| Param | Type | Description |
| --- | --- | --- |
| payload | <code>Object</code> | The payload to sign |
| [algorithm] | <code>String</code> | The signing algorithm |

**Example**  
```js
const token = await jwt.sign({ my: 'payload' });
```
<a name="JWTService.verify"></a>

### JWTService.verify([token]) ⇒ <code>Promise.&lt;Object&gt;</code>
Verify and decode the given token

**Kind**: static method of [<code>JWTService</code>](#JWTService)  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise to be resolved with the token payload.  

| Param | Type | Description |
| --- | --- | --- |
| [token] | <code>String</code> | The token to decode |

**Example**  
```js
const payload = await jwt.verify('my.jwt.token');
```

# Authors
- [Nicolas Froidure](https://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/jwt-service/blob/main/LICENSE)

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