npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

random-web-token

v2.3.1

Published

Generate customizable random strings (tokens) using secure charsets with validation – zero dependencies.

Readme

random-web-token

This package uses the secure Node.js crypto module.

A lightweight utility for generating customizable random strings (a.k.a. "tokens") using predefined or custom character sets. Includes simple validation to check whether a given string matches a specific character set and length.

Ideal for user-facing identifiers such as activation links, invitation links, forgot password links, or other non-sensitive unique strings.


Installation

npm install random-web-token

(Optional) TypeScript support:

npm install --save-dev @types/random-web-token

Usage

Generate a random string (synchronously)

const token = require("random-web-token");

const generated = token.genSync("extra", 50);
console.log(generated); // e.g., "fT7ZkWA4NpDqF0BjgY..."

Generate a random string (asynchronously with await)

const token = require("random-web-token");

async function generate() {
  const generated = await token.genAsync("extra", 50);
  console.log(generated); // e.g., "h8YkMRaWg5tBz4QEX..."
}

generate();

TypeScript version:

import * as token from "random-web-token";

async function generate() {
  const generated = await token.genAsync("extra", 50);
  console.log(generated);
}

generate(); // -> sHF3p8zZCTdAmJ0cyS60NK...

Character sets

Use one of the predefined sets as the first argument when generating a string:

| Name | Characters Used | | --------------- | --------------- | | "normal" | a–z | | "normal+" | A–Z | | "medium" | a–z, 0–9 | | "medium+" | A–Z, 0–9 | | "extra" | a–z, A–Z, 0–9 | | "onlyNumbers" | 0–9 |


Validate a string

You can validate whether a given string matches a specific character set and length:

const token = require("random-web-token");

const t = token.genSync("extra", 50);

// Valid case
console.log(token.syncValidator("extra", 50, t)); // true

// Invalid: wrong length
console.log(token.syncValidator("extra", 40, t)); // false

// Invalid: wrong character set
console.log(token.syncValidator("normal", 50, t)); // false

Allowing custom extra characters

You can optionally allow specific extra characters:

const altered = token.genSync("extra", 50) + "+!/";
console.log(token.syncValidator("extra", 53, altered, "+!/")); // true

Use custom character sets

Need a fully custom character set? Use withMyOwnCharacters():

const token = require("random-web-token");

async function customToken() {
  const t = await token.withMyOwnCharacters("abc123", 10);
  console.log(t); // e.g., "a12cb31acb"
}

customToken();