{"id":2135,"library":"nanoid","title":"Nano ID","description":"Nano ID is a tiny, secure, URL-friendly, unique string ID generator for Python. It provides compact IDs that are safer and shorter than UUIDs by using a larger alphabet and cryptographically strong random APIs. The current stable version is 2.0.0, and it is actively maintained.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/puyuan/py-nanoid","tags":["ID generator","unique ID","short ID","security","URL-friendly"],"install":[{"cmd":"pip install nanoid","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"generate","correct":"from nanoid import generate"},{"note":"Use this for non-security-critical applications where performance is prioritized, as it uses pseudo-random generation.","symbol":"non_secure_generate","correct":"from nanoid import non_secure_generate"}],"quickstart":{"code":"from nanoid import generate, non_secure_generate\n\n# Generate a default 21-character, URL-friendly ID\nid_default = generate()\nprint(f\"Default ID: {id_default}\")\n\n# Generate an ID with a specified length\nid_short = generate(size=10)\nprint(f\"Short ID (10 chars): {id_short}\")\n\n# Generate an ID with a custom alphabet and length\nid_custom = generate('1234567890abcdef', 16)\nprint(f\"Custom Alphabet ID: {id_custom}\")\n\n# Generate a non-secure ID (faster, but not for sensitive use)\nid_non_secure = non_secure_generate()\nprint(f\"Non-Secure ID: {id_non_secure}\")","lang":"python","description":"Quickly generate secure, URL-friendly unique IDs. You can specify the length and a custom alphabet. A faster, non-secure generator is also available for less sensitive use cases."},"warnings":[{"fix":"If migrating from a pre-2.0.0 version, explicitly define the old alphabet and length if consistent ID generation is critical for existing data. Otherwise, IDs generated by v2.0.0 will be different by default.","message":"Version 2.0.0 changed the default alphabet, replacing '~' with '-'. The default ID length was also reduced from 22 to 21 characters.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Always use `generate()` for sensitive applications, which employs cryptographically strong random APIs. Only use `non_secure_generate()` when performance is critical and security is not a concern.","message":"The `non_secure_generate` function uses `random.random` internally and is not cryptographically strong. It is explicitly not recommended for security-sensitive applications like generating tokens or secrets.","severity":"gotcha","affected_versions":"All versions where `non_secure_generate` is available"},{"fix":"Always use an ID collision probability calculator (often linked in Nano ID documentation for various languages) to determine a safe ID length based on your expected number of IDs and desired collision probability.","message":"Reducing the ID length (e.g., `generate(size=10)`) increases the probability of collisions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Prefer the default URL-friendly alphabet or ensure that custom alphabets avoid problematic characters for URL contexts. If problematic characters are necessary, ensure proper URL encoding.","message":"When using custom alphabets, be cautious with characters like `-,.()` at the end of a link in URLs, as they might be misinterpreted as punctuation by some systems.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using NanoIDs (or UUIDs) as clustered primary keys if insert performance is critical. Consider using them as regular unique identifiers and a separate sequential primary key for clustering, or evaluate the impact on your specific database system.","message":"NanoIDs are non-sequential. Using them as clustered primary keys in a database can lead to significant performance overhead during inserts, as the database may need to reorder records physically on disk to maintain the clustered index order.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}