Using Durable Redis with Vercel KV

In this guide, we will walk you through the process of integrating Vercel KV, a serverless key-value storage system, into your project. By leveraging Vercel KV, you can store and retrieve data efficiently, ensuring scalability and global distribution. Let's get started!

Step 1: Create a new Vercel KV store

  1. Login to your Vercel account at https://vercel.com/login
  2. Once logged in, navigate to the Vercel dashboard
  3. Click on the "Add New" button and choose "Storage" from the dropdown menu
  4. Click the “Create Database” button and select “KV”
  5. Give your KV store a name and click “Create”
Screenshot 2023-07-20 at 10.57.41 AM.png

Step 2: Add your KV store to a project

  1. In the Vercel dashboard, select the project where you want to add the Vercel KV store
  2. Click on the "Storage" tab for the project
  3. Find the KV store created in step 1 and click “Connect”
Screenshot 2023-07-20 at 10.56.47 AM.png

Step 3: Pull your new environment variables

You can retrieve your environment variables from the Vercel KV overview page in the Vercel dashboard. Alternatively, you can retrieve them from the Vercel CLI as follows:

  1. Open your terminal or command prompt
  2. Navigate to your project's root directory
  3. Run the following command to pull the environment variables from Vercel to your local development environment: Note: To run vercel env pull your project must be linked to a Vercel project. If it is not, you can do so by running vercel link and following the command line prompts

Step 4: Install Vercel KV into your project

  1. In your project's root directory, install the @vercel/kv package by running the following command:

Step 5: Use Vercel KV in your application

  1. If you are using Next.js, create a new route handler app/api/views/route.js with the following code:
  2. If you are using another framework, check out the Vercel KV documentation here.

Step 6: Query your KV database

  1. Within your project in the Vercel dashboard, click the "Storage" tab
  2. Select the KV database you wish to query
  3. Query your database using the built in CLI tool. For example, you can get or set values like this:

Now, you can access your API at /api/views to see the returned value of { views: "1" }​

Conclusion

Congratulations! You have successfully integrated Vercel KV into your project. Now you can leverage the durable serverless key-value storage to efficiently store and retrieve data, ensuring scalability and global distribution. Experiment with different data operations and continue building amazing applications with Vercel KV.

To learn more about Vercel KV, check out the docs. You can also get started instantly with one of our KV starter templates.

Couldn't find the guide you need?