{"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":"python","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,"search_vec":"'2.0.0':44 'activ':48 'alphabet':33 'api':38 'compact':20 'cryptograph':35 'current':40 'friend':11,59 'generat':15,51 'id':2,4,14,21,50,53,55 'larger':32 'maintain':49 'nano':1,3 'provid':19 'python':17 'random':37 'safer':24 'secur':8,56 'short':54 'shorter':26 'stabl':41 'string':13 'strong':36 'tini':7 'uniqu':12,52 'url':10,58 'url-friend':9,57 'use':30 'uuid':28 'version':42","created_at":"2026-04-09T18:45:01.299904+00:00","updated_at":"2026-04-16T17:19:00.924226+00:00","problems":[{"fix":"Install the package using pip: `pip install nanoid`","cause":"The 'nanoid' package is not installed in your Python environment.","error":"ModuleNotFoundError: No module named 'nanoid'"},{"fix":"Import the `generate` function explicitly and call it: `from nanoid import generate; id = generate()`","cause":"You are trying to call the 'nanoid' module directly as a function after `import nanoid`, but the ID generation functionality is exposed via the `generate` function within the module.","error":"TypeError: 'module' object is not callable"},{"fix":"The correct function to generate an ID is `generate`. Use `from nanoid import generate; id = generate()` or if you must use `import nanoid`, then `id = nanoid.generate()`.","cause":"You have imported the 'nanoid' module (e.g., `import nanoid`) and are attempting to call a non-existent attribute named 'nanoid' within it (e.g., `nanoid.nanoid()`), often a pattern from other language implementations.","error":"AttributeError: module 'nanoid' has no attribute 'nanoid'"},{"fix":"Import the correct function named 'generate': `from nanoid import generate`","cause":"You are attempting to import a function named 'nanoid' directly from the 'nanoid' package, but the primary ID generation function is named 'generate'.","error":"from nanoid import nanoid"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.0.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/puyuan/py-nanoid","docs":null,"changelog":null,"pypi":"https://pypi.org/project/nanoid/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}