You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/kv/get-started.mdx
+39-70Lines changed: 39 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,7 +116,7 @@ A [KV namespace](/kv/concepts/kv-namespaces/) is a key-value database replicated
116
116
117
117
<TabssyncKey='CLIvsDash'> <TabItemlabel='CLI'>
118
118
119
-
[Wrangler](/workers/wrangler/)allows you to put, list, get, and delete entries within your KV namespace.
119
+
You can use [Wrangler](/workers/wrangler/)to create a new KV namespace. You can also use it to perform operations such as put, list, get, and delete within your KV namespace.
120
120
121
121
:::note
122
122
@@ -171,6 +171,14 @@ To create a KV namespace via Wrangler:
171
171
172
172
You must create a binding to connect your Worker with your KV namespace. [Bindings](/workers/runtime-apis/bindings/) allow your Workers to access resources, like KV, on the Cloudflare developer platform.
173
173
174
+
:::note[Bindings]
175
+
176
+
A binding is how your Worker interacts with external resources such as [KV namespaces](/kv/concepts/kv-namespaces/). A binding is a runtime variable that the Workers runtime provides to your code. You can declare a variable name in your Wrangler file that binds to these resources at runtime, and interact with them through this variable. Every binding's variable name and behavior is determined by you when deploying the Worker.
177
+
178
+
Refer to [Environment](/kv/reference/environments/) for more information.
179
+
180
+
:::
181
+
174
182
To bind your KV namespace to your Worker:
175
183
176
184
<TabssyncKey='CLIvsDash'><TabItemlabel='CLI'>
@@ -194,14 +202,6 @@ To bind your KV namespace to your Worker:
194
202
- Your binding is available in your Worker at `env.<BINDING_NAME>` from within your Worker. For this tutorial, the binding is available at `env.USERS_NOTIFICATION_CONFIG`.
195
203
</Steps>
196
204
197
-
:::note[Bindings]
198
-
199
-
A binding is how your Worker interacts with external resources such as [KV namespaces](/kv/concepts/kv-namespaces/). A binding is a runtime variable that the Workers runtime provides to your code. You can declare a variable name in your Wrangler file that binds to these resources at runtime, and interact with them through this variable. Every binding's variable name and behavior is determined by you when deploying the Worker.
200
-
201
-
Refer to [Environment](/kv/reference/environments/) for more information.
202
-
203
-
:::
204
-
205
205
</TabItem><TabItemlabel='Dashboard'>
206
206
207
207
<Steps>
@@ -232,6 +232,8 @@ To write a value to your empty KV namespace using Wrangler:
232
232
npx wrangler kv key put --binding=<BINDING_NAME>"<KEY>""<VALUE>"
233
233
```
234
234
235
+
In this tutorial, you will add a key `user_1` with value `enabled` to the KV namespace you created in [step 2](/kv/get-started/#2-create-a-kv-namespace).
236
+
235
237
```sh
236
238
npx wrangler kv key put --binding=USERS_NOTIFICATION_CONFIG "user_1""enabled"
237
239
```
@@ -240,7 +242,7 @@ To write a value to your empty KV namespace using Wrangler:
240
242
```
241
243
</Steps>
242
244
243
-
:::note
245
+
:::note[Using `--namespace-id`]
244
246
Instead of using `--binding`, you can also use `--namespace-id` to specify which KV namespace should receive the operation:
245
247
246
248
```sh "--namespace-id=<BINDING_ID>"
@@ -252,12 +254,16 @@ Writing the value "<VALUE>" to key "<KEY>" on namespace <BINDING_ID>.
252
254
```
253
255
:::
254
256
255
-
To create a key and a value in local mode, add the `--local` flag at the end of the command:
257
+
:::note[Storing values in remote KV namespace]
256
258
257
-
```sh ins="--local"
258
-
npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>""<VALUE>" --local
259
+
By default, the values are written locally. To create a key and a value in your remote KV namespace, add the `--remote` flag at the end of the command:
260
+
261
+
```sh ins="--remote"
262
+
npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>""<VALUE>" --remote
259
263
```
260
264
265
+
:::
266
+
261
267
</TabItem><TabItemlabel='Dashboard'>
262
268
<Steps>
263
269
1. Go to [**Storage & Databases** > **KV**](https://dash.cloudflare.com/?to=/:account/workers/kv/namespaces).
To access the value from your KV namespace using Wrangler:
278
284
279
285
<Steps>
280
286
1. Run the `wrangler kv key get` subcommand in your terminal, and input your key value:
281
287
282
288
```sh
283
-
# Replace [OPTIONS] with --binding or --namespace-id
284
-
npx wrangler kv key get [OPTIONS] "<KEY>"
289
+
npx wrangler kv key get --binding=<BINDING_NAME>"<KEY>"
285
290
```
286
291
287
-
A KV namespace can be specified in two ways. Replace `[OPTIONS]` with `--binding` or `--namespace-id`:
288
-
289
-
<Detailsheader="With a `--binding`">
290
-
291
-
```sh
292
-
npx wrangler kv key get --binding=<BINDING_NAME>"<KEY>"
293
-
```
294
-
295
-
</Details>
296
-
297
-
<Detailsheader="With a `--namespace-id`">
292
+
In this tutorial, you will get the value of the key `user_1` from the KV namespace you created in [step 2](/kv/get-started/#2-create-a-kv-namespace).
298
293
299
294
```sh
300
-
npx wrangler kv key get --namespace-id=<YOUR_ID>"<KEY>"
295
+
npx wrangler kv key get --binding=USERS_NOTIFICATION_CONFIG "user_1" --text
301
296
```
302
-
</Details>
303
297
304
-
For example, using the `--binding` option:
298
+
:::note
299
+
To view the value directly within the terminal, you use the `--text` flag.
300
+
:::
305
301
306
-
```sh
307
-
npx wrangler kv key get --binding=USERS_NOTIFICATION_CONFIG "user_1"
308
-
```
302
+
Similar to the `put` command, the `get` command can also be used to access a KV namespace in two ways - with `--binding` or `--namespace-id`:
309
303
310
304
</Steps>
311
305
@@ -316,9 +310,7 @@ To access the value using Wrangler:
316
310
Exactly **one** of `--binding` or `--namespace-id` is required.
317
311
:::
318
312
319
-
:::note
320
-
To view the value directly within the terminal, add `--text`
321
-
:::
313
+
322
314
323
315
Refer to the [`kv bulk` documentation](/kv/reference/kv-commands/#kv-bulk) to write a file of multiple key-value pairs to a given KV namespace.
324
316
@@ -345,7 +337,7 @@ To have `wrangler dev` connect to your Workers KV namespace running on Cloudflar
345
337
:::
346
338
347
339
<Steps>
348
-
1. In your Worker script, add your KV binding in the `Env` interface:
340
+
1. In your Worker script, add your KV binding in the `Env` interface. If you have bootstrapped your project with JavaScript, this step is not required.
349
341
350
342
```ts
351
343
interfaceEnv {
@@ -354,16 +346,16 @@ To have `wrangler dev` connect to your Workers KV namespace running on Cloudflar
354
346
}
355
347
```
356
348
357
-
2. Use the `put()` method on `USERS_NOTIFICATION_CONFIG` to create a new key-value pair:
349
+
2. Use the `put()` method on `USERS_NOTIFICATION_CONFIG` to create a new key-value pair. You will add a new key `user_2` with value `disabled` to your KV namespace.
358
350
359
351
```ts
360
-
let value =awaitenv.USERS_NOTIFICATION_CONFIG.put(key, value);
352
+
let value =awaitenv.USERS_NOTIFICATION_CONFIG.put("user_2", "disabled");
361
353
```
362
354
363
-
3. Use the KV `get()` method to fetch the data you stored in your KV database:
355
+
3. Use the KV `get()` method to fetch the data you stored in your KV namespace. You will fetch the value of the key `user_2` from your KV namespace.
364
356
365
357
```ts
366
-
let value =awaitenv.USERS_NOTIFICATION_CONFIG.get("KEY");
358
+
let value =awaitenv.USERS_NOTIFICATION_CONFIG.get("user_2");
367
359
```
368
360
</Steps>
369
361
@@ -380,9 +372,11 @@ Your Worker code should look like this:
380
372
381
373
The code above:
382
374
383
-
1. Writes a key to `USERS_NOTIFICATION_CONFIG` binding using KV's `put()` method.
384
-
2. Reads the same key using KV's `get()` method, and returns an error if the key is null (or in case the key is not set, or does not exist).
385
-
3. Uses JavaScript's [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) exception handling to catch potential errors. When writing or reading from any service, such as Workers KV or external APIs using `fetch()`, you should expect to handle exceptions explicitly.
375
+
1. Writes a key to your KV namespace using KV's `put()` method.
376
+
2. Reads the same key using KV's `get()` method.
377
+
3. It checks if the key is null, and returns a `404` response if it is.
378
+
4. If the key is not null, it returns the value of the key.
379
+
5. Uses JavaScript's [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) exception handling to catch potential errors. When writing or reading from any service, such as Workers KV or external APIs using `fetch()`, you should expect to handle exceptions explicitly.
386
380
387
381
</TabItem><TabItemlabel='Dashboard'>
388
382
@@ -414,32 +408,7 @@ The code above:
414
408
415
409
</TabItem></Tabs>
416
410
417
-
## 6. Deploy your KV
418
-
419
-
You can deploy your project in one of two ways:
420
-
421
-
-**Locally:** Runs from your machine on a local server. The URL to your web application is only accessible while you are running the application from your terminal.
422
-
-**Remotely:** Runs on Cloudflare's global network. The URL to your web application will be accessible to anyone with the URL.
423
-
424
-
:::note
425
-
You can only deploy your project locally through the Wrangler CLI. You can only deploy remotely when deploying through the Cloudflare dashboard.
426
-
:::
427
-
428
-
In this tutorial, we expect the URL to simply return the value `disabled` (which corresponds to the key `user_2`).
429
-
430
-
### Deploy your project locally
431
-
432
-
<Steps>
433
-
1. Run the following command within your project directory:
434
-
435
-
```sh
436
-
npm run dev
437
-
```
438
-
439
-
2. Visit the URL for your newly created Workers KV application running on your local machine, as provided by Wrangler (usually `localhost:8787`).
0 commit comments