How to do KV binding in wrangler.toml

I have created a namespace in dashboard but now I am not able to write binding in .toml to use it in my worker

You add it in the wrangler.toml like so:

kv_namespaces = [
  { binding = "KV", id = "<your-id>" }
]

What problem are you having?

My Binding in .toml is:

kv_namespaces = [
{ binding = “MY_KV”, id = “e9f1f787b96245e79f1d15067e0b5afb” }
]

This is how I am calling in my worker :
let value = await MY_KV.get(“my_tensor”);
if(value === null){
await MY_KV.put(“my_tensor”, ten_obj);

value = ten_obj;

}

Error I am getting :
Error: In order to preview a worker with KV namespaces, you must designate a preview_id in your configuration file for each KV namespace you’d like to preview.

Add preview_id in the KV namespace, it’s there so you can use a different namespace for prod and dev.

kv_namespaces = [
  { binding = “MY_KV”, id = “e9f1f787b96245e79f1d15067e0b5afb”, preview_id = “e9f1f787b96245e79f1d15067e0b5afb” }
]
1 Like

Thank you