12<html lang="en">3<head>4<meta charset="utf-8">5<meta name="viewport" content="width=device-width, initial-scale=1">6<title>NIP-C0 - Code Snippets — Nostr Instruction Manual</title>7<meta name="description" content="NIP-C0: code snippet events. Kind 1337 with metadata tags for language, filename, license, runtime, dependencies, and source repository. Complete manual page.">8<link rel="preload" href="../assets/fonts/michroma-400-latin.woff2" as="font" type="font/woff2" crossorigin>9<link rel="preload" href="../assets/fonts/exo2-var-latin.woff2" as="font" type="font/woff2" crossorigin>10<link rel="preload" href="../assets/fonts/sairacondensed-600-latin.woff2" as="font" type="font/woff2" crossorigin>11<link rel="preload" href="../assets/fonts/sairacondensed-700-latin.woff2" as="font" type="font/woff2" crossorigin>12<link rel="preload" href="../assets/fonts/sairacondensed-500-latin.woff2" as="font" type="font/woff2" crossorigin>13<link rel="preload" href="../assets/fonts/sharetechmono-400-latin.woff2" as="font" type="font/woff2" crossorigin>14<link rel="preload" href="../assets/fonts/exo2-400i-latin.woff2" as="font" type="font/woff2" crossorigin>15<link rel="icon" href="../assets/favicon.svg" type="image/svg+xml">16<link rel="stylesheet" href="../assets/manual.css">17</head>18<body>1920<svg width="0" height="0" style="position:absolute">21 <defs>22 <marker id="arr" markerWidth="9" markerHeight="9" refX="7" refY="4.5" orient="auto">23 <path d="M1,1 L8,4.5 L1,8 Z" fill="#f26a00"/>24 </marker>25 <marker id="arrb" markerWidth="9" markerHeight="9" refX="7" refY="4.5" orient="auto">26 <path d="M1,1 L8,4.5 L1,8 Z" fill="#2e5f9e"/>27 </marker>28 </defs>29</svg>3031<div class="page">32 <span class="staple s1"></span><span class="staple s2"></span>3334 <header class="band">35 <a class="logo" href="../index.html">NOSTR<b>▸</b>PROTOCOL MANUAL</a>36 <span class="booklet-note">Chapter C0 - Developer Tools</span>37 </header>38 <nav class="tabs">39 <a href="../index.html">Contents</a>40 <a href="#overview" class="on">Overview</a>41 <a href="#spec">Spec</a>42 <a href="#examples">Examples</a>43 <a href="#history">History</a>44 </nav>4546 <div class="chapter">47 <div class="nipno" data-n="C0">C0</div>48 <div class="titles">49 <div class="kicker">Nostr Implementation Possibility - Hex № C0</div>50 <h1>Code Snippets</h1>51 <p class="sub">A dedicated event kind for sharing code, with metadata tags for language, filename, license, runtime, and dependencies.</p>52 </div>53 </div>5455 <main class="spread">56 <div class="main">5758 <h2 class="bar" id="overview"><span class="num">1</span> Overview</h2>59 <p>Developers share code on Nostr today inside plain text notes (<code>kind:1</code>, see <a href="01.html">NIP-01</a> and <a href="10.html">NIP-10</a>). A text note carries no metadata about the code. Clients cannot tell the language. They cannot highlight syntax. They cannot offer a correct filename for download.</p>60 <p>This NIP defines a separate event kind for code snippets. The event stores the code itself in <code>content</code>. A set of optional tags describes the code: language, name, extension, description, runtime, license, dependencies, and source repository.</p>61 <p>The extra metadata improves discoverability. A client can filter snippets by language. It improves syntax highlighting. It improves the whole user experience around shared code.</p>6263 <div class="box hint">64 <span class="tag">Hint!</span>65 <p>Every tag in this NIP is optional. A bare kind-1337 event with only code in <code>content</code> is valid. Add tags when you know them; clients degrade gracefully when they are missing.</p>66 </div>6768 <figure class="diagram">69 <div class="frame">70 <svg viewBox="0 0 560 190" role="img" aria-label="A developer client publishes a kind 1337 snippet to a relay; a reader client requests snippets filtered by language and receives them">71 <rect class="d-actor" x="10" y="70" width="120" height="50" rx="8"/>72 <text class="d-actor-label" x="70" y="92" text-anchor="middle">Author</text>73 <text class="d-note" x="70" y="109" text-anchor="middle">client</text>74 <rect class="d-actor" x="225" y="70" width="110" height="50" rx="8"/>75 <text class="d-actor-label" x="280" y="99" text-anchor="middle">Relay</text>76 <rect class="d-actor" x="430" y="70" width="120" height="50" rx="8"/>77 <text class="d-actor-label" x="490" y="92" text-anchor="middle">Reader</text>78 <text class="d-note" x="490" y="109" text-anchor="middle">client</text>79 <path class="d-msg d-flow" d="M132,88 L222,88"/>80 <path class="d-msg blue" d="M428,102 L338,102"/>81 <path class="d-msg blue d-flow" d="M337,88 L427,88"/>82 <text class="d-label" x="177" y="78" text-anchor="middle">EVENT kind 1337</text>83 <text class="d-label" x="382" y="78" text-anchor="middle">EVENT (match)</text>84 <text class="d-label" x="382" y="122" text-anchor="middle">REQ {"#l":["rust"]}</text>85 <circle r="4" fill="#f26a00">86 <animateMotion dur="2.6s" repeatCount="indefinite" path="M132,88 L222,88"/>87 </circle>88 <circle r="4" fill="#2e5f9e">89 <animateMotion dur="2.6s" begin="1.3s" repeatCount="indefinite" path="M337,88 L427,88"/>90 </circle>91 </svg>92 </div>93 <figcaption>Fig. C0-1 — <b>Snippet flow.</b> The author publishes one signed kind-1337 event. Readers find it by kind or by indexed <code>l</code> tag.</figcaption>94 </figure>9596 <h2 class="bar" id="spec"><span class="num">2</span> The Snippet Event: Kind 1337</h2>97 <p>This NIP defines <code>kind:1337</code> as a code snippet event. The <code>content</code> field contains the actual code snippet text. Nothing else goes in <code>content</code>: no markdown fences, no commentary.</p>9899 <div class="crt">100 <div class="titlebar">Wire format - code snippet event</div>101<pre>{102 <span class="k">"id"</span>: <span class="c"><32-byte lowercase hex sha256 of the serialized event></span>103 <span class="k">"pubkey"</span>: <span class="c"><32-byte lowercase hex public key of the author></span>104 <span class="k">"created_at"</span>: <span class="c"><unix timestamp, seconds></span>105 <span class="k">"kind"</span>: 1337,106 <span class="k">"content"</span>: <span class="c"><the code snippet text></span>107 <span class="k">"tags"</span>: [ <span class="c"><optional metadata tags, see §3></span> ]108 <span class="k">"sig"</span>: <span class="c"><64-byte signature of the id></span>109}</pre>110 </div>111112 <p>Kind 1337 sits in the regular range (1000–9999) of <a href="01.html">NIP-01</a>. Relays store every snippet event. A new snippet does not replace an old one. To change a snippet, publish a new event.</p>113114 <h2 class="bar" id="tags"><span class="num">3</span> Optional Tags</h2>115 <p>All eight tags are optional. Each tag adds one piece of metadata about the code:</p>116117 <div class="tscroll"><table class="ref">118 <caption>Snippet metadata tags</caption>119 <tr><th>Tag</th><th>Value</th><th>Rule</th></tr>120 <tr><td class="btn">l</td><td>language name</td><td>Programming language, lowercase. Examples: <code>javascript</code>, <code>python</code>, <code>rust</code>.</td></tr>121 <tr><td class="btn">name</td><td>snippet name</td><td>Name of the snippet, commonly a filename. Examples: <code>hello-world.js</code>, <code>quick-sort.py</code>.</td></tr>122 <tr><td class="btn">extension</td><td>file extension</td><td>Extension without the dot. Examples: <code>js</code>, <code>py</code>, <code>rs</code>.</td></tr>123 <tr><td class="btn">description</td><td>free text</td><td>A brief description of what the code does.</td></tr>124 <tr><td class="btn">runtime</td><td>runtime spec</td><td>Runtime or environment. Examples: <code>node v18.15.0</code>, <code>python 3.11</code>.</td></tr>125 <tr><td class="btn">license</td><td>SPDX identifier</td><td>License for the code and related event data. Repeatable. See below.</td></tr>126 <tr><td class="btn">dep</td><td>dependency</td><td>One dependency the code needs to run. Repeat the tag for each dependency.</td></tr>127 <tr><td class="btn">repo</td><td>URL or address</td><td>The repository the code comes from. See below.</td></tr>128 </table></div>129130 <h3>The license tag</h3>131 <p>The <code>license</code> value covers the code and any related data in the event, such as the description. The value must be a standard SPDX short identifier when one exists. Examples: <code>MIT</code>, <code>GPL-3.0-or-later</code>, <code>Apache-2.0</code>. The tag may carry an additional parameter: a reference to the actual license text.</p>132 <p>The tag can be repeated to indicate multi-licensing. The recipient may then use the code under any one of the referenced licenses, at their choosing.</p>133134 <h3>The repo tag</h3>135 <p>The <code>repo</code> value points at the repository where the code originates. It must take one of two forms:</p>136 <ul>137 <li>A standard URL, such as <code>https://github.com/nostr-protocol/nostr</code>.</li>138 <li>The address of a <a href="34.html">NIP-34</a> git repository announcement event, in the form <code>30617:<32-byte hex pubkey>:<d tag value></code>. In this form, add an additional parameter: a recommended relay URL where the announcement event can be found.</li>139 </ul>140141 <div class="box tech">142 <span class="tag">Tech Note</span>143 <p>The single-letter <code>l</code> tag is also the label tag of <a href="32.html">NIP-32</a>, where it pairs with an <code>L</code> namespace tag. This NIP uses <code>l</code> alone, with a different meaning. Both share the same indexed <code>#l</code> filter, so a language query can match unrelated labeled events outside kind 1337. Always combine <code>"#l"</code> with <code>"kinds": [1337]</code>.</p>144 </div>145146 <h2 class="bar" id="behavior"><span class="num">4</span> Client Behavior</h2>147 <p>Clients that support this NIP <strong>should</strong> do six things:</p>148 <ol>149 <li>Display code snippets with proper syntax highlighting based on the language.</li>150 <li>Allow copying the full code snippet with a single action.</li>151 <li>Render the code with appropriate formatting. Preserve whitespace and indentation.</li>152 <li>Display the language and extension prominently.</li>153 <li>Provide a run function for supported languages when possible.</li>154 <li>Display the description, if available, as part of the snippet presentation.</li>155 </ol>156 <p>Clients <strong>may</strong> add more functionality:</p>157 <ol>158 <li>Code editing capabilities.</li>159 <li>Forking and modifying snippets.</li>160 <li>Creating executable environments based on the runtime and dependencies.</li>161 <li>Downloading the snippet as a file, using the provided extension.</li>162 <li>Sharing the snippet with attribution.</li>163 </ol>164165 <div class="box warn">166 <span class="tag">Warning!</span>167 <p>A run function executes code from strangers. Sandbox it. Never run a snippet with access to the user's keys, files, or network without explicit consent.</p>168 </div>169170 <figure class="diagram">171 <div class="frame">172 <svg viewBox="0 0 560 120" role="img" aria-label="Tags feed client features: the l tag drives highlighting, name and extension drive download, runtime and dep drive execution">173 <rect class="d-actor" x="10" y="35" width="120" height="50" rx="8"/>174 <text class="d-actor-label" x="70" y="57" text-anchor="middle">Event</text>175 <text class="d-note" x="70" y="74" text-anchor="middle">content + tags</text>176 <rect class="d-actor" x="180" y="35" width="110" height="50" rx="8"/>177 <text class="d-actor-label" x="235" y="57" text-anchor="middle">Highlight</text>178 <text class="d-note" x="235" y="74" text-anchor="middle">l, extension</text>179 <rect class="d-actor" x="340" y="35" width="90" height="50" rx="8"/>180 <text class="d-actor-label" x="385" y="57" text-anchor="middle">Save</text>181 <text class="d-note" x="385" y="74" text-anchor="middle">name + ext</text>182 <rect class="d-actor" x="480" y="35" width="70" height="50" rx="8"/>183 <text class="d-actor-label" x="515" y="57" text-anchor="middle">Run</text>184 <text class="d-note" x="515" y="74" text-anchor="middle">runtime, dep</text>185 <path class="d-msg d-flow" d="M132,60 L177,60"/>186 <path class="d-msg d-flow" d="M292,60 L337,60"/>187 <path class="d-msg d-flow" d="M432,60 L477,60"/>188 </svg>189 </div>190 <figcaption>Fig. C0-2 — <b>Metadata pipeline.</b> Each tag unlocks one client feature. Missing tags disable the feature, never the snippet.</figcaption>191 </figure>192193 <h2 class="bar" id="examples"><span class="num">5</span> Worked Example</h2>194 <p>The event below shares a small JavaScript function. Every optional tag is present:</p>195196 <div class="crt">197 <div class="titlebar">Example - full kind-1337 event</div>198<pre>{199 <span class="k">"id"</span>: <span class="c"><32-byte lowercase hex sha256 of the serialized event></span>,200 <span class="k">"pubkey"</span>: <span class="c"><32-byte lowercase hex public key of the event creator></span>,201 <span class="k">"created_at"</span>: 1743264000,202 <span class="k">"kind"</span>: 1337,203 <span class="k">"content"</span>: <span class="s">"function helloWorld() {\n console.log('Hello, Nostr!');\n}\n\nhelloWorld();"</span>,204 <span class="k">"tags"</span>: [205 [<span class="s">"l"</span>, <span class="s">"javascript"</span>],206 [<span class="s">"extension"</span>, <span class="s">"js"</span>],207 [<span class="s">"name"</span>, <span class="s">"hello-world.js"</span>],208 [<span class="s">"description"</span>, <span class="s">"A basic JavaScript function that prints 'Hello, Nostr!' to the console"</span>],209 [<span class="s">"runtime"</span>, <span class="s">"node v18.15.0"</span>],210 [<span class="s">"license"</span>, <span class="s">"MIT"</span>],211 [<span class="s">"repo"</span>, <span class="s">"https://github.com/nostr-protocol/nostr"</span>]212 ],213 <span class="k">"sig"</span>: <span class="c"><64-byte signature of the id></span>214}</pre>215 </div>216217 <p>The session below publishes the snippet, then fetches all JavaScript snippets from the same relay. Orange lines go to the relay. Blue lines come from the relay.</p>218 <div class="crt">219 <div class="titlebar">Session transcript - wss://relay.example.com</div>220<pre><span class="c">-- author publishes the snippet --</span>221<span class="send">→ ["EVENT", {"kind":1337, "content":"function helloWorld() {…", "tags":[["l","javascript"], …], …}]</span>222<span class="recv">← ["OK", "7ac21f30…", true, ""]</span>223224<span class="c">-- a reader asks for JavaScript snippets --</span>225<span class="send">→ ["REQ", "snips", {"kinds":[1337], "#l":["javascript"], "limit":20}]</span>226<span class="recv">← ["EVENT", "snips", {"id":"7ac21f30…", "kind":1337, "content":"function helloWorld() {…", …}]</span>227<span class="recv">← ["EOSE", "snips"]</span>228<span class="send">→ ["CLOSE", "snips"]</span></pre>229 </div>230231 <h2 class="bar" id="history"><span class="num">6</span> Version History</h2>232 <p>NIP-C0 is a young, stable draft. Notable changes, newest first:</p>233 <ul class="patchnotes">234 <li class="minor"><span class="ver">2025</span><span class="when">2025-12</span>Typo and link fixes across two cleanup passes.</li>235 <li><span class="ver">2025</span><span class="when">2025-11</span>General revision of the specification (#2138), tightening the tag definitions.</li>236 <li class="minor"><span class="ver">2025</span><span class="when">2025-04</span>Title and description cleanup in a repository-wide pass (#1888).</li>237 <li><span class="ver">2025</span><span class="when">2025-03</span>fiatjaf renamed the file from 95 to C0, giving the NIP its hex number.</li>238 <li><span class="ver">Origin</span><span class="when">2025-03-29</span>Christian Chiarulli published the code snippets proposal (#1852).</li>239 </ul>240241 <div class="credits">242 <h3 class="cardhead">Original Proposal - Credits</h3>243 <table>244 <tr><td>Author</td><td>Christian Chiarulli</td></tr>245 <tr><td>First commit</td><td>2025-03-29 — “NIP-C0: Code snippets” (<a href="https://github.com/nostr-protocol/nips/pull/1852">PR #1852</a>)</td></tr>246 <tr><td>Status</td><td><span class="chip draft">draft</span><span class="chip optional">optional</span></td></tr>247 <tr><td>Source</td><td><a href="https://github.com/nostr-protocol/nips/blob/master/C0.md">C0.md on GitHub</a> - <a href="https://github.com/nostr-protocol/nips/commits/master/C0.md">full change history</a></td></tr>248 </table>249 </div>250251 <h2 class="bar"><span class="num">7</span> See Also</h2>252 <ul>253 <li><a href="01.html">NIP-01</a> — the base event format, kind ranges, and the <code>#l</code> indexed filter used here.</li>254 <li><a href="34.html">NIP-34</a> — git repository announcements; the target of the addressable <code>repo</code> form.</li>255 <li><a href="32.html">NIP-32</a> — labeling; shares the single-letter <code>l</code> tag name with this NIP.</li>256 <li><a href="10.html">NIP-10</a> — plain kind-1 text notes, the metadata-free way to paste code.</li>257 <li><a href="19.html">NIP-19</a> — bech32 encodings for sharing snippet event pointers outside relays.</li>258 </ul>259 </div>260261 <aside>262 <div class="cartridge">263 <div class="inner">264 <h3 class="cardhead">Spec Card</h3>265 <div class="lights"><span class="led on"></span><span class="led warn"></span><span class="led warn"></span></div>266 <dl>267 <dt>Status</dt><dd><span class="chip draft">draft</span><span class="chip optional">optional</span></dd>268 <dt>Defines</dt><dd>kind 1337 - l / name / extension / description / runtime / license / dep / repo tags</dd>269 <dt>Kind</dt><dd>1337 (regular)</dd>270 <dt>Requires</dt><dd><a href="01.html">NIP-01</a>; <a href="34.html">NIP-34</a> for addressable repo refs</dd>271 <dt>Transport</dt><dd>WebSocket - JSON arrays</dd>272 <dt>First commit</dt><dd>2025-03-29</dd>273 <dt>Last updated</dt><dd>2025-12-30</dd>274 </dl>275 </div>276 </div>277 </aside>278 </main>279280 <nav class="navbtns">281 <a href="be.html">◀ NIP-BE: Nostr BLE Communications Protocol</a>282 <a href="c7.html">NIP-C7: Chats ▶</a>283 </nav>284285 <footer class="pagefoot">286 <a href="../index.html">Nostr Protocol Manual</a>287 <span class="pageno">NIP C0 / F4</span>288 </footer>289</div>290291<script src="../assets/webmcp.js" defer></script>292</body>293</html>