<p>This sample shows how to use all of the different <a href="https://developer.fermyon.com/spin/kv-store-api-guide">Key/Value functions</a> in TypeScript, including how to decode results, trap errors in a <code>try/catch</code>, and work with JSON.</p> <p>This will help you get started with KV storage either locally using <code>spin up</code> or in Fermyon Cloud using <code>spin deploy</code>.</p> <p>Everything is demoed in one <code>index.ts</code> file. And while it is TypeScript, it is easily adapted to JavaScript with almost no changes (other than the occasional type annotation).</p> <p>Here’s a snippet of the code you’ll see in this project:</p> <pre><code class="language-javascript"> // We can get a list of all of the keys out of the DB. let keys = store.getKeys().join(&quot;, &quot;) console.log(&quot;The keys in the DB are: &quot; + keys) // Now we can get values back out. Note that the value is an array buffer, // so we have to convert it back to a string. let siteName = store.get(&quot;name&quot;) let dec = new TextDecoder(&quot;utf-8&quot;) // This will print &quot;Pet Database&quot; to the log console.log(&quot;Site name: &quot; + dec.decode(siteName)) // A non-existent key comes back with a null value. let noSuchValue = store.get(&quot;no-such-key&quot;) if (noSuchValue == null) { console.log(&quot;Got an expected 'null' for a nonexistent key&quot;) } </code></pre>