Nico
Nico14mo ago

Trouble accessing Deno KV via the Deno CLI

I'm trying to connect to a Deno KV via the CLI with the following simple code:
//main.ts
const kv = await Deno.openKv(
"https://api.deno.com/databases/d7822b57-b261-4c64-bbe4-41d5f981aacb/connect"
);

const result = kv.set(["foo"], "bar");
console.log(result);
//main.ts
const kv = await Deno.openKv(
"https://api.deno.com/databases/d7822b57-b261-4c64-bbe4-41d5f981aacb/connect"
);

const result = kv.set(["foo"], "bar");
console.log(result);
I have an access token set up in my account settings, as shown in the first screen shot. I also have an environment variable DENO_KV_ACCESS_TOKEN exported into my shell and when I run deno run --unstable main.ts, I allow access to that variable as shown int he second screenshot but I get the following error: Uncaught NotFound: Missing DENO_KV_ACCESS_TOKEN environment variable. Please set it to your access token from https://dash.deno.com/account Why is this happening?
No description
No description
1 Reply
Deno AI Helper
Deno AI Helper14mo ago
Based on the documentation, the DENO_KV_ACCESS_TOKEN environment variable is required to connect to your Deno KV instance from the command line. This token should be set to your personal access token from your Deno account. If you're seeing an error message that the DENO_KV_ACCESS_TOKEN environment variable is missing, it could be due to a few reasons: 1. The environment variable is not correctly set in your shell. You can verify this by running echo $DENO_KV_ACCESS_TOKEN in your shell. If it doesn't print your token, you need to set it correctly. 2. You're not allowing Deno to access the environment variable when running your script. When running your script, you need to include the --allow-env flag to give Deno access to your environment variables. So your command should look like this: deno run --unstable --allow-env main.ts. 3. The token you're using is not correct. Make sure you're using the correct personal access token from your Deno account. If you've checked all of these and you're still seeing the error, it might be a good idea to debug further by printing out the value of DENO_KV_ACCESS_TOKEN in your script before trying to open the KV instance. This can help you verify that the environment variable is being correctly accessed by your script.