Skip to content

Kind 1337: Code Snippet

Overview

Kind 1337 events represent code snippets in the Nostr ecosystem. Unlike regular text notes, code snippets include specialized metadata such as programming language, file extension, and other code-specific attributes that enhance discoverability and improve the user experience when sharing code. This kind allows for proper syntax highlighting, code formatting, and related functionality that developers expect when working with code.

Specification

PropertyValue
Kind Number1337
Event RangeRegular
Defined inNIP-C0

Content Format

The content field contains the actual code snippet text. This should preserve all whitespace, line breaks, and indentation required for the code to be properly displayed and executed.

Schema

json
"content": "function helloWorld() {\n  console.log('Hello, Nostr!');\n}\n\nhelloWorld();"

Tags

Tag NameDescriptionFormatRequired
lProgramming language["l", "javascript"]No
nameName of the snippet (often filename)["name", "hello-world.js"]No
extensionFile extension (without dot)["extension", "js"]No
descriptionBrief description of the code["description", "A basic JavaScript function"]No
runtimeRuntime or environment["runtime", "node v18.15.0"]No
licenseCode license["license", "MIT"]No
depDependency required (can repeat)["dep", "express@4.18.2"]No
repoSource repository["repo", "https://github.com/user/project"]No

Client Behavior

Clients should:

  1. Display code snippets with proper syntax highlighting based on the specified language
  2. Preserve whitespace, indentation, and line breaks in the code
  3. Provide a simple way to copy the entire code snippet with a single action
  4. Display relevant metadata such as language, filename, and description
  5. Optionally provide "run" functionality for supported languages when possible
  6. Allow downloading the snippet as a file using the provided name and extension

Relay Behavior

Relays should:

  1. Store code snippet events like any other regular event
  2. Support filtering by language and other tags to enable code discovery

Use Cases

  • Sharing code examples and solutions
  • Documenting programming techniques
  • Collaborating on code snippets with other developers
  • Creating interactive coding tutorials
  • Preserving useful code fragments for future reference
  • Demonstrating programming concepts

Example

json
{
  "id": "4376c65d2f232afbe9b882a35baa4f6fe8667c4e684749af565f981833ed6a65",
  "pubkey": "79dff8f82963424e0bb02708a22e44b4980893e3a4be0fa3cb60a43b946764e3",
  "created_at": 1671217411,
  "kind": 1337,
  "tags": [
    ["l", "javascript"],
    ["extension", "js"],
    ["name", "hello-world.js"],
    ["description", "A basic JavaScript function that prints 'Hello, Nostr!' to the console"],
    ["runtime", "node v18.15.0"],
    ["license", "MIT"]
  ],
  "content": "function helloWorld() {\n  console.log('Hello, Nostr!');\n}\n\nhelloWorld();",
  "sig": "908a15e46fb4d8675bab026fc230a0e3542bfade63da02d542fb78b2a8513fcd0092619a2c8c1221e581946e0191f2af505dfdf8657a414dbca329186f009262"
}

Python example:

json
{
  "id": "fe8b1c9a37218cf5ce604a8dce596c9c3ae0c650df2336f3c60c78e61a7b2031",
  "pubkey": "f7234bd4c1394dda46d09f35bd384dd30cc552ad5541990f98844fb06676e9ca",
  "created_at": 1671217500,
  "kind": 1337,
  "tags": [
    ["l", "python"],
    ["extension", "py"],
    ["name", "quick_sort.py"],
    ["description", "Implementation of the quick sort algorithm"],
    ["runtime", "python 3.11"],
    ["license", "Apache-2.0"]
  ],
  "content": "def quick_sort(arr):\n    if len(arr) <= 1:\n        return arr\n    pivot = arr[len(arr) // 2]\n    left = [x for x in arr if x < pivot]\n    middle = [x for x in arr if x == pivot]\n    right = [x for x in arr if x > pivot]\n    return quick_sort(left) + middle + quick_sort(right)\n\n# Example usage\nnumbers = [3, 6, 8, 10, 1, 2, 1]\nprint(quick_sort(numbers))",
  "sig": "a76f39224cebd44cf04eeebe01d5ab2e8f7a3e5e56c83cadd252e7bc5e2e34fd7ad7148cf7a2c7620d9490ccf2e1bac521995725f2e1f543159b8eb98bfdd405"
}

References

Notes

  • The kind number 1337 is a reference to "leet speak" - a nod to programming culture
  • Multiple dep tags can be included to list all dependencies needed for the code
  • Clients may implement additional features like code editing, forking snippets, or creating executable environments
  • When displaying code snippets, maintaining proper formatting is crucial for readability
  • Including appropriate license information is recommended for clarity on code reuse permissions