serverless-cloudflare-workers icon indicating copy to clipboard operation
serverless-cloudflare-workers copied to clipboard

KV Namespace Binding clarification

Open cwndrws opened this issue 6 years ago • 2 comments

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.

cwndrws avatar Jun 10 '19 13:06 cwndrws

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')

saibotsivad avatar Jan 31 '20 19:01 saibotsivad

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.

cwndrws avatar Jan 31 '20 19:01 cwndrws