Skip to main content
On this page

A Deno KV database hosted on Deno Deploy can be used from Node.js through the official client library on npm. The library speaks the open KV Connect protocol, so the same client also connects to a self-hosted denokv server.

Installation and usage Jump to heading

Use your preferred npm client to install the client library for Node.js using one of the commands below.

›_
npm install @deno/kv
›_
pnpm add @deno/kv
›_
yarn add @deno/kv

Once you've added the package to your Node project, you can import the openKv function (supports both ESM import and CJS require-based usage):

import { openKv } from "@deno/kv";

// Connect to a KV database on Deno Deploy
const kv = await openKv(
  "https://api.deno.com/v2/databases/<Database ID>/connect",
);

// Write some data
await kv.set(["users", "alice"], { name: "Alice" });

// Read it back
const result = await kv.get(["users", "alice"]);
console.log(result.value); // { name: "Alice" }

By default, the access token used for authentication comes from the DENO_KV_ACCESS_TOKEN environment variable. You can also pass it explicitly:

import { openKv } from "@deno/kv";

const kv = await openKv("<KV Connect URL>", { accessToken: myToken });

Once the client is initialized, the Deno KV API is available in Node, with one exception: queue operations (enqueue / listenQueue) are not part of the KV Connect protocol, so they are unavailable over a remote connection. The package README has the full support matrix and the supported Node.js versions.

KV Connect URLs Jump to heading

Connecting to a KV database outside of Deno requires a KV Connect URL. For a database hosted on Deno Deploy it has this format:

https://api.deno.com/v2/databases//connect

Where to find the database ID and how to create the access token that authenticates the connection (DENO_KV_ACCESS_TOKEN, or accessToken above) is described in Connect from outside Deno Deploy; the same URL and token work from the Deno CLI.

For a self-hosted denokv server, use the URL the server is listening on (for example http://localhost:4512) and the access token it was started with.

More information Jump to heading

More information about how to use the Deno KV module for Node can be found on the project's README page.

Last updated on

Did you find what you needed?

Edit this page
Privacy policy