serverless-cloudflare-workers
serverless-cloudflare-workers copied to clipboard
KV Namespace Binding clarification
It's not clear that when binding a KV namespace, the name is the value that's required as opposed to the ID. This compounded with the fact that if you give it a namespace name that doesn't exist, it silently creates a new namespace with that name. This led to confusion as the KV API was returning different data than my worker and not until I went into the UI did I realize that there were multiple namespace created, one with the ID of the other as its name.
This was unclear to me as well, even reading your description 😅 so here's an example for anyone else confused.
If you have this in your YAML file:
functions:
demo:
name: my-demo
script: handler
resources:
kv:
- variable: CREDENTIALS
namespace: FOO_CREDENTIALS
You will want this in your JS:
export async function handleRequest() {
const credentials =await CREDENTIALS.get('example-secret')
The thing I was attempting to call out is that the namespace should be the name of the namespace and not the ID, which was not clear to me.