The `nkeys` Python library, currently at version 0.2.1, provides a public-key signature system built upon Ed25519 cryptography, specifically designed for identity, authentication, and authorization within the NATS ecosystem. It offers utilities for generating, encoding, and managing NATS-compatible key pairs (Operators, Accounts, Users, Servers, Clusters). The library maintains a low-to-moderate release cadence, with recent updates focusing on dependency management and Python version compatibility.
pip install nkeysVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to generate a new NATS user key pair (seed, public, and private keys) using `nkeys` and `pynacl`, then sign and verify a message. It also highlights the importance of wiping sensitive key data.
Ensure `pynacl` and its underlying `libsodium` are correctly installed. For environments like Docker, this may involve adding build-time dependencies (e.g., `gcc`, `musl-dev`, `libffi-dev` on Alpine Linux, or development headers) or ensuring pre-built wheels are compatible with your platform.
If `pip install nkeys` or `pip install pynacl` fails, check the error output for missing compilers (like `gcc`) or development headers (like `libffi-dev`, `libsodium-dev`). Install these system-level packages via your OS package manager (e.g., `apt-get install build-essential libffi-dev` on Debian/Ubuntu, `apk add gcc musl-dev libffi-dev` on Alpine).
Always use the provided `nkeys.PREFIX_BYTE_X` constants (e.g., `nkeys.PREFIX_BYTE_USER`, `nkeys.PREFIX_BYTE_ACCOUNT`) when encoding raw keys into NATS NKey format, for instance, with `nkeys.encode_seed()`. Ensure that the resulting NKey strings start with the expected prefix for their type.
Upgrade `nkeys` to version 0.2.0 or higher: `pip install --upgrade nkeys`. This version replaces `ed25519` with `pynacl`, resolving the compatibility issue.
Install the necessary build tools and development libraries for your operating system. For Debian/Ubuntu: `sudo apt-get update && sudo apt-get install build-essential libffi-dev`. For Alpine Linux (in Docker): `apk add gcc musl-dev libffi-dev`. For other systems, consult `pynacl`'s documentation for prerequisites.