Beginner's Guide to Vercel KV

beginner Steps

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Getting Started
  4. Working with Data
  5. Examples of Supported Redis Commands
  6. Vercel KV Limits
  7. Pricing and Billing
  8. Resources

Introduction

Vercel KV is a durable, serverless Redis database that allows you to store and retrieve simple key-value data structures globally at the edge. It is available in Beta on Pro and Hobby plans. This beginner's guide will help you get started with Vercel KV and learn how to use it effectively.

Prerequisites

Before beginning, you'll need:

  • A Vercel account
  • A project on Vercel
  • Node.js and npm installed on your machine

Getting Started

Creating a KV Database

  1. Log in to your Vercel account and navigate to the project you want to add a KV database to.
  2. Go to the Storage tab and click on the "Connect Database" button.
  3. Under the "Create New" tab, select KV and click on the "Continue" button.
  4. Enter a database name (e.g., "my-store") and select a primary region.
  5. Click on "Create & Continue".
  6. Choose which environments of the project you want to make the database available to and click on "Connect".

Adding Environment Variables

After creating your KV store, Vercel will automatically add the following environment variables to your project:

  • KV_URL
  • KV_REST_API_URL
  • KV_REST_API_TOKEN
  • KV_REST_API_READ_ONLY_TOKEN

To use these environment variables in your local project, run the following command:

vercel env pull .env.development.local

Working with Data

Populating the Store

You can send Redis commands to your KV store using the CLI interface in the Vercel dashboard:

  1. Select your KV store and go to the CLI tab.
  2. Create a sample hash by running the following command:
hset user:me email email@me.com id 123
  1. Read the hash by running:
hgetall user:me

Querying the Store

To retrieve data from your KV Store, you can create a simple API route in your Next.js project. In the pages/api folder, create a new file called user.js and add the following code:

import kv from "@vercel/kv";

export default async function handler(request, response) {
  const user = await kv.hgetall("user:me");
  return response.status(200).json(user);
}

Run your application locally and visit /api/user to see your data.

Examples of Supported Redis Commands

Vercel KV supports most Redis commands, which can be executed using the Vercel KV SDK. Below are some examples of supported Redis commands:

Key Values

  • SET
  • GET

Hashes

  • HSET
  • HGET

Lists

  • LPUSH
  • LRANGE

Sets

  • SADD
  • SREM
  • SISMEMBER

Sorted Sets

  • ZADD
  • ZRANGE

Bitmaps

  • SETBIT
  • GETBIT

For a full list of supported commands, refer to the official Redis docs.

Vercel KV Limits

Vercel KV has some limitations, such as unsupported Upstash features, general limitations, and Redis compatibility. For more information, check the Vercel KV Limits documentation.

Pricing and Billing

Vercel KV offers a usage-based pricing model with a free tier for Hobby plans. For more information on pricing and billing, check the Vercel KV Pricing documentation.

Resources

For more details and advanced usage, check the following resources:

This guide was created using chatgpt and official Vercel documentation for the purpose of having a small guide on how to use Vercel KV. Its for me to learn how to combine complex documentation into a simple guide. I hope you enjoyed it!