---
title: Write multiple key-value pairs
---

[Skip to content](#_top)

[API Reference](https://developers.cloudflare.com/api/python)

[KV](https://developers.cloudflare.com/api/python/resources/kv)

[Namespaces](https://developers.cloudflare.com/api/python/resources/kv/subresources/namespaces)

Copy Markdown

Open in **Claude**Open in **ChatGPT**Open in **Cursor**

---

**Copy Markdown****View as Markdown**

# Write multiple key-value pairs

kv.namespaces.bulk\_update(strnamespace\_id, NamespaceBulkUpdateParams\*\*kwargs) -> [NamespaceBulkUpdateResponse](<https://developers.cloudflare.com/api/python/resources/kv#(resource)%20kv.namespaces%20%3E%20(model)%20namespace_bulk_update_response%20%3E%20(schema)>)

PUT/accounts/{account\_id}/storage/kv/namespaces/{namespace\_id}/bulk

Writes up to 10,000 key-value pairs to the specified Workers KV namespace from a JSON array, with optional metadata and expiration settings for each pair. Existing values and expirations are overwritten. If neither `expiration` nor `expiration_ttl` is specified, the key-value pair will not expire. If both are set, `expiration_ttl` takes precedence. The entire request must be 100 megabytes or less. The result reports the number of successful writes and any keys that failed and should be retried.

##### Security

<details>

<summary>API Token</summary>



The preferred authorization scheme for interacting with the Cloudflare API. <a href="https://developers.cloudflare.com/fundamentals/api/get-started/create-token/">Create a token</a>.

**Example:**<code>Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY</code>

</details>

<details>

<summary>API Email + API Key</summary>



The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

**Example:**<code>X-Auth-Email: user@example.com</code>

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

**Example:**<code>X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194</code>

</details>

##### Accepted Permissions (at least one required)

`Workers KV Storage Write`

##### ParametersExpand Collapse

account\_id: str

ID of the Cloudflare account that owns the Workers KV namespaces.

maxLength32

[Link to this property](<#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20account_id%20%3E%20(schema)>)

namespace\_id: str

ID of the Workers KV namespace.

maxLength32

[Link to this property](<#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20namespace_id%20%3E%20(schema)>)

<details>

<summary>

body: Iterable\[Body]

</summary>

key: str

A key’s name. The name may be at most 512 bytes. All printable, non-whitespace characters are valid.

maxLength512

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20key">Link to this property</a>

value: str

A UTF-8 encoded string to be stored, up to 25 MiB in length.

maxLength26214400

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20value">Link to this property</a>

base64: Optional\[bool]

Indicates whether or not the server should base64 decode the value before storing it. Useful for writing values that wouldn’t otherwise be valid JSON strings, such as images.

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20base64">Link to this property</a>

expiration: Optional\[float]

Expires the key at a certain time, measured in number of seconds since the UNIX epoch.

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20expiration">Link to this property</a>

expiration\_ttl: Optional\[float]

Number of seconds until the key expires. Must be at least 60. Takes precedence over <code>expiration</code> when both are specified.

minimum60

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20expiration_ttl">Link to this property</a>

metadata: Optional\[object]

Arbitrary JSON that is associated with a key.

<a href="#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20metadata">Link to this property</a>

</details>

[Link to this property](<#(resource)%20kv.namespaces%20%3E%20(method)%20bulk_update%20%3E%20(params)%20default%20%3E%20(param)%20body%20%3E%20(schema)>)

##### ReturnsExpand Collapse

<details>

<summary>

class NamespaceBulkUpdateResponse: …

</summary>

successful\_key\_count: Optional\[float]

Number of keys successfully written or deleted by the bulk operation.

<a href="#(resource)%20kv.namespaces%20%3E%20(model)%20namespace_bulk_update_response%20%3E%20(schema)%20%3E%20(property)%20successful_key_count">Link to this property</a>

unsuccessful\_keys: Optional\[List\[str]]

Names of keys that failed to be written or deleted. Retry the operation for these keys.

<a href="#(resource)%20kv.namespaces%20%3E%20(model)%20namespace_bulk_update_response%20%3E%20(schema)%20%3E%20(property)%20unsuccessful_keys">Link to this property</a>

</details>

[Link to this property](<#(resource)%20kv.namespaces%20%3E%20(model)%20namespace_bulk_update_response%20%3E%20(schema)>)

### Write multiple key-value pairs

Python

HTTPTypeScriptPythonGoTerraform

```
import os
from cloudflare import Cloudflare

client = Cloudflare(
    api_token=os.environ.get("CLOUDFLARE_API_TOKEN"),  # This is the default and can be omitted
)
response = client.kv.namespaces.bulk_update(
    namespace_id="0f2ac74b498b48028cb68387c421e279",
    account_id="023e105f4ecef8ad9ca31a8372d0c353",
    body=[{
        "key": "My-Key",
        "value": "Some string",
    }],
)
print(response.successful_key_count)
```

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "success": true,
  "result": {
    "successful_key_count": 100,
    "unsuccessful_keys": [
      "string"
    ]
  }
}
```

##### Returns Examples

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "success": true,
  "result": {
    "successful_key_count": 100,
    "unsuccessful_keys": [
      "string"
    ]
  }
}
```