📖 Scenario: You are building a small web service on Deno Deploy that stores and retrieves user notes using the KV (Key-Value) storage system.This service will let users save a note with a unique key and then fetch it later.
🎯 Goal: Create a Deno Deploy script that uses KV to store a note under a key and then retrieve it.You will set up the KV store, define a key and a note, save the note, and then read it back.
📋 What You'll Learn
Create a KV store instance
Define a key and a note string
Store the note in KV using the key
Retrieve the note from KV using the key
💡 Why This Matters
🌍 Real World
Many web apps need to save small pieces of data quickly and retrieve them later. KV storage on Deno Deploy is perfect for simple note-taking apps, user preferences, or session data.
💼 Career
Understanding KV storage and serverless functions is important for backend and full-stack developers working with modern cloud platforms like Deno Deploy.
Progress0 / 4 steps
1
Set up KV store and define data
Create a constant called kv that gets the KV store from Deno.openKv(). Then create a constant noteKey with the value ["note", "1"] and a constant noteValue with the string "Remember to drink water".
Deno
Hint
Use await Deno.openKv() to get the KV store. Define the key as an array with two strings. The note is a simple string.
2
Store the note in KV
Use await kv.set(noteKey, noteValue) to save the note string under the key in the KV store.
Deno
Hint
Use the set method on kv with the key and value to store data.
3
Retrieve the note from KV
Create a constant called result and assign it the value of await kv.get(noteKey) to fetch the stored note.
Deno
Hint
Use the get method on kv with the key to retrieve the stored value.
4
Access and export the stored note
Create a constant called storedNote and assign it the value property of result. Then export a default async function called handler that returns a new Response with storedNote as the body and content type text/plain.
Deno
Hint
Extract the value from result. Then create an async function named handler that returns a plain text response with the stored note.
Practice
(1/5)
1. What is the main purpose of KV on Deno Deploy?
easy
A. To create user interfaces
B. To run heavy computations on the server
C. To store small pieces of data in the cloud
D. To manage network connections
Solution
Step 1: Understand KV storage role
KV on Deno Deploy is designed to store small data pieces like counters or preferences.
Step 2: Compare options with KV purpose
Only To store small pieces of data in the cloud matches storing small data in the cloud, others describe unrelated tasks.
Final Answer:
To store small pieces of data in the cloud -> Option C
Quick Check:
KV stores small data = A [OK]
Hint: KV is for small cloud data storage, not heavy tasks [OK]
Common Mistakes:
Confusing KV with computation services
Thinking KV manages UI or network
Assuming KV handles large files
2. Which of the following is the correct syntax to save a value '42' with key 'count' using KV in Deno Deploy?
easy
A. await kv.set('count', 42);
B. kv.set('count', 42);
C. await kv.get('count', 42);
D. kv.get('count', 42);
Solution
Step 1: Identify correct method to save data
Saving data uses kv.set with await to handle the async operation.
Step 2: Check syntax correctness
await kv.set('count', 42); uses await kv.set('count', 42); which is correct syntax. Options C and D use get which reads data, not saves.
Final Answer:
await kv.set('count', 42); -> Option A
Quick Check:
Save data = await kv.set() [OK]
Hint: Use await with kv.set to save data asynchronously [OK]
Common Mistakes:
Omitting await causing unresolved promises
Using kv.get instead of kv.set to save
Passing wrong number of arguments
3. What will be the output of this code snippet on Deno Deploy KV?
const value = await kv.get('visits');
console.log(value);
Assuming 'visits' key was never set before.
medium
A. undefined
B. null
C. Throws an error
D. 0
Solution
Step 1: Understand kv.get behavior for missing keys
If a key does not exist, kv.get returns null, not undefined or error.
Step 2: Analyze console.log output
Since 'visits' was never set, value is null, so console.log prints null.
Final Answer:
null -> Option B
Quick Check:
Missing key returns null = C [OK]
Hint: Missing KV keys return null, not undefined or error [OK]
Common Mistakes:
Expecting undefined instead of null
Assuming kv.get throws error for missing keys
Thinking default numeric 0 is returned
4. Identify the error in this Deno Deploy KV code snippet:
const result = kv.set('score', 100);
console.log('Score saved:', result);
medium
A. Missing await before kv.set causing unresolved promise
B. kv.set does not accept string keys
C. kv.set should be kv.save
D. console.log syntax is incorrect
Solution
Step 1: Check async usage of kv.set
kv.set is asynchronous and returns a promise, so it needs await to resolve.
Step 2: Verify other parts of code
Keys can be strings, kv.set is correct method, and console.log syntax is valid.
Final Answer:
Missing await before kv.set causing unresolved promise -> Option A
Quick Check:
Async kv.set needs await = D [OK]
Hint: Always await kv.set to avoid unresolved promises [OK]
Common Mistakes:
Forgetting await on async kv.set
Using wrong method name like kv.save
Misunderstanding console.log syntax
5. You want to implement a visitor counter using KV on Deno Deploy. Which approach correctly increments the count each time a visitor arrives?
hard
A. await kv.set('visits', (await kv.get('visits')) + 1);
B. kv.set('visits', 1);
C. const current = kv.get('visits') || 0;
kv.set('visits', current + 1);
D. const current = await kv.get('visits') ?? 0;
await kv.set('visits', current + 1);
Solution
Step 1: Retrieve current count safely
Use await kv.get('visits') and default to 0 if null using ?? operator.
Step 2: Increment and save updated count
Add 1 to current count and await kv.set to save it asynchronously.
Step 3: Analyze options for correctness
const current = await kv.get('visits') ?? 0;
await kv.set('visits', current + 1); correctly handles async and null values. await kv.set('visits', (await kv.get('visits')) + 1); misses null check, C misses await, D resets count always.
Final Answer:
const current = await kv.get('visits') ?? 0;
await kv.set('visits', current + 1); -> Option D
Quick Check:
Safe get + increment + await set = B [OK]
Hint: Use null coalescing and await both get and set [OK]
Common Mistakes:
Not awaiting async calls
Not handling null from kv.get
Resetting count instead of incrementing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.