A modern, zero-dependency library for generating prefixed, k-sorted globally unique identifiers (KSUIDs) across multiple programming languages.
- ✅ Zero Dependencies - No external dependencies
- ✅ TypeScript First - Full TypeScript support with strict typing
- ✅ Cross-Platform - Works in Python, Node.js: Browser, Bun, and Deno
- ✅ K-Sortable - IDs are naturally sortable by creation time
- ✅ Environment Aware - Support for different environments (prod, dev, staging, etc.)
- ✅ Instance Detection - Automatic detection of Docker containers, MAC addresses, and PIDs
- ✅ Developer Friendly - A KSUID is selectable by double-clicking, bad day to be a hyphenated id.
The JavaScript implementation is production-ready and available on npm:
npm install @langwatch/ksuidQuick Start:
import { generate, parse } from '@langwatch/ksuid';
// Generate a KSUID
const ksuid = generate('user');
console.log(ksuid.toString());
// Output: user_00028U9MDT583X9eXPG1IU0ptdl1m
// Parse a KSUID
const parsed = parse('user_00028U9MDT583X9eXPG1IU0ptdl1m');
console.log(parsed.resource); // 'user'
console.log(parsed.environment); // 'prod'
console.log(parsed.date); // Date object📖 Full JavaScript Documentation
The Python implementation is production-ready and available on PyPI:
pip install langwatch-ksuid
# or
uv add langwatch-ksuidQuick Start:
from langwatch-ksuid import generate, parse
# Generate a KSUID
id = generate('user')
print(id.to_string())
# Output: user_00028U9MDT583X9eXPG1IU0ptdl1m
# Parse a KSUID
const parsed = parse('user_00028U9MDT583X9eXPG1IU0ptdl1m');
console.log(parsed.resource); # 'user'
console.log(parsed.environment); # 'prod'
console.log(parsed.date); # Date objectWe'll be adding support for other languages as we need them.
KSUID (K-Sortable Unique IDentifier) is a globally unique identifier that is:
- K-Sortable - IDs are naturally sortable by creation time
- Globally Unique - No coordination required between systems
- URL Safe - Uses base62 encoding (no special characters)
- Time-Based - Contains a timestamp for easy debugging and analytics
- Instance Aware - Includes machine/container identification
[environment_][resource_][timestamp][instance][sequence]
Example:
prod_user_00028U9MDT583X9eXPG1IU0ptdl1m
prod_- Environment prefix (optional)user_- Resource type prefix00028U9MDT583X9eXPG1IU0ptdl1m- Base62 encoded payload containing:- 32-bit timestamp (seconds since epoch)
- 64-bit instance identifier
- 32-bit sequence number
- Database Primary Keys - Natural sorting by creation time
- Distributed Systems - No coordination required
- Event IDs - Time-based ordering for event streams
- File Names - URL-safe identifiers
- API Request IDs - Unique request tracking
- User Sessions - Environment-aware session management
| Feature | UUID | ULID | KSUID | Snowflake |
|---|---|---|---|---|
| Size | 36 chars | 26 chars | ~27 chars | 19 chars |
| Sortable | ❌ | ✅ | ✅ | ✅ |
| URL Safe | ❌ | ✅ | ✅ | ✅ |
| Zero Dependencies | ✅ | ✅ | ✅ | ❌ |
| Instance Aware | ❌ | ❌ | ✅ | ✅ |
| Environment Aware | ❌ | ❌ | ✅ | ❌ |
| Double-click to highlight | ❌ | ❌ | ✅ | ✅ |
git clone https://github.com/langwatch/ksuid.git
cd ksuidWe welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
To add support for a new programming language:
- Create a new directory for the language (e.g.,
python/,go/,rust/) - Implement the core KSUID functionality following the specification
- Add comprehensive tests
- Update this README with the new implementation
- Submit a pull request
The KSUID specification ensures compatibility across all language implementations:
- Timestamp: 32-bit Unix timestamp (seconds since epoch)
- Instance: 64-bit instance identifier (MAC address, container ID, or random)
- Sequence: 32-bit sequence number (increments for same timestamp)
- Encoding: Base62 (URL-safe alphanumeric characters)
- Format:
[environment_][resource_][payload]
This project uses release-please to automate releases for both JavaScript and Python packages. See RELEASE_PLEASE.md for detailed documentation.
- Make Changes: Create PRs with conventional commit messages
- Merge to Master: Release Please automatically creates release PRs when it detects releasable changes
- Review Release PR: Check the generated changelog and version bumps
- Merge Release PR: This triggers automated publishing workflows for both packages
For manual releases or troubleshooting:
TypeScript Package:
cd js
pnpm install
pnpm test
pnpm run build
pnpm publish --provenance --access public --no-git-checksPython Package:
cd python
uv sync --dev
uv run pytest tests
uv run python -m build
uv publishMIT License - please see the LICENSE file in the respective language directory.
This library is inspired by the original KSUID specification and the @cuvva/ksuid implementation.
- 📖 Documentation: JavaScript Implementation
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
- 📧 Email: support@langwatch.com