Claude-skills vercel-kv
Vercel KV (Redis-compatible key-value storage via Upstash). Use for Next.js caching, sessions, rate limiting, TTL data storage, or encountering KV_REST_API_URL errors, rate limit issues, JSON serialization errors. Provides strong consistency vs eventual consistency.
install
source · Clone the upstream repo
git clone https://github.com/secondsky/claude-skills
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/secondsky/claude-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/plugins/vercel-kv/skills/vercel-kv" ~/.claude/skills/secondsky-claude-skills-vercel-kv && rm -rf "$T"
manifest:
plugins/vercel-kv/skills/vercel-kv/SKILL.mdsource content
Vercel KV (Redis-Compatible Storage)
Status: Production Ready Last Updated: 2025-12-14 Dependencies: None Latest Versions:
@vercel/kv@3.0.0
Quick Start (3 Minutes)
1. Create & Configure
# Create KV database in Vercel dashboard: Storage → Create Database → KV vercel env pull .env.local
Creates environment variables:
- Your KV database URLKV_REST_API_URL
- Auth tokenKV_REST_API_TOKEN
2. Install
bun add @vercel/kv
3. Use in Your App
Next.js Server Action:
'use server'; import { kv } from '@vercel/kv'; export async function incrementViews(slug: string) { const views = await kv.incr(`views:${slug}`); return views; }
Edge API Route:
import { kv } from '@vercel/kv'; export const runtime = 'edge'; export async function GET(request: Request) { const value = await kv.get('mykey'); return Response.json({ value }); }
Critical Rules
Always Do
| Rule | Why |
|---|---|
| Set TTL for temporary data | - Avoid memory leaks |
| Use namespaced keys | not - Prevents collisions |
| Handle null returns | Non-existent keys return |
| Use pipeline for batch ops | Single round-trip reduces latency |
| Serialize JSON-compatible only | No functions, circular references |
| Monitor command usage | Stay within free tier (30K/month) |
Never Do
| Rule | Why |
|---|---|
| Store sensitive data unencrypted | KV not encrypted at rest by default |
| Forget to set TTL | Keys without TTL stay forever |
| Use generic key names | , will collide |
| Store large values (>1MB) | Use Vercel Blob instead |
| Use as primary database | KV is for cache, not persistence |
Commit | Contains KV tokens |
Core Operations
Set/Get with TTL:
import { kv } from '@vercel/kv'; // Set with TTL (expires in 1 hour) await kv.setex('session:abc', 3600, { userId: 123 }); // Get value const session = await kv.get('session:abc'); // Delete await kv.del('session:abc');
Atomic Operations:
const views = await kv.incr('views:post:123'); const wasSet = await kv.setnx('lock:process', 'running'); // Set if not exists
Multiple Keys:
const values = await kv.mget('user:1', 'user:2', 'user:3'); await kv.mset({ 'user:1': data1, 'user:2': data2 });
Known Issues Prevention
This skill prevents 10 documented issues:
| # | Error | Quick Fix |
|---|---|---|
| 1 | | Run |
| 2 | | Convert to string, use plain objects |
| 3 | Unexpected data returned | Use namespaced keys: |
| 4 | Memory grows indefinitely | Use with TTL |
| 5 | Rate limit exceeded | Batch operations, upgrade plan |
| 6 | Value too large | Use Vercel Blob for >100KB |
| 7 | TypeScript type errors | Use with Zod validation |
| 8 | Pipeline silent failures | Check results array from |
| 9 | Scan timeout errors | Limit count, iterate with cursor |
| 10 | Session expires too early | Use for sliding window |
See:
references/known-issues.md for complete solutions with code examples.
Common Patterns Summary
| Pattern | Use Case | Key API |
|---|---|---|
| Cache-Aside | Read caching | → fetch → |
| Write-Through | Write consistency | on write, on delete |
| Rate Limiting | API protection | + |
| Session Management | User sessions | , , , |
| Distributed Lock | Concurrency control | + + |
| Leaderboard | Rankings | , , |
| Pipeline | Batch operations | |
See:
references/common-patterns.md for complete implementations.
Configuration
.env.local
# Created by: vercel env pull .env.local KV_REST_API_URL="https://your-database.kv.vercel-storage.com" KV_REST_API_TOKEN="your-token-here"
.gitignore
.env.local .env*.local
When to Load References
| Reference | Load When... |
|---|---|
| Debugging KV errors, type issues, or performance problems |
| Implementing caching, sessions, rate limiting, leaderboards, or locks |
Dependencies
{ "dependencies": { "@vercel/kv": "^3.0.0" } }
Free Tier Limits: 30,000 commands/month, 256MB storage
Official Documentation
- Vercel KV: https://vercel.com/docs/storage/vercel-kv
- SDK Reference: https://vercel.com/docs/storage/vercel-kv/kv-reference
- GitHub: https://github.com/vercel/storage
- Redis Commands: https://redis.io/commands
Troubleshooting
| Problem | Solution |
|---|---|
| Run |
| Rate limit exceeded | Use /pipeline, upgrade plan |
| Values not expiring | Use not |
| JSON serialization error | Use plain objects, convert BigInt to string |
Token Savings: ~60% (patterns extracted to references) Error Prevention: 100% (all 10 documented issues) Ready for production!