Metadata.json Secrets KV WASM DO Examples

Hello,

Some examples for Secrets, Workers KV, WASM, DurableObjects bindings.

Upload

curl -X PUT https://api.cloudflare.com/client/v4/accounts/my-account-id/workers/scripts/my-worker-name" -H "Authorization: Bearer $CF_API_TOKEN" -F "[email protected];type=application/json" -F "[email protected];type=application/javascript" -F "[email protected];type=application/octet-stream -F "[email protected];type=application/wasm"

You can upload scripts as
application/javascript
application/javascript+module

That said, application/javascript+module is still in closed beta at 2021-02-10
Additional files like chat.html are probably only useful when imported as modules

The total size for the upload (all files) must be <= 1 MB

Deploy

curl -s -X POST https://api.cloudflare.com/client/v4/accounts/my-account-id/workers/scripts/my-worker-name/subdomain -H "Authorization: Bearer my-token" -H "Content-Type:application/json" --data "{ \"enabled\": true }"

You have to deploy after upload !

Metadata.json

Example of a WASM metadata.json

{
  "body_part": "script",
  "bindings": [
    {
      "type": "wasm_module",
      "name": "wasm",
      "part": "wasmprogram"
    }
  ]
}

name is the binding name, the one you have to use in your workers code
part is the name of the .wasm file, like : 1af86d-x25519.wasm

Example of a Durable Objects metadata.json

{
  "main_module": "chat.mjs",
  "bindings": [
    {
      "type": "durable_object_namespace",
      "name": "rooms",
      "namespace_id": "$ROOMS_ID"
    },
    {
      "type": "durable_object_namespace",
      "name": "limiters",
      "namespace_id": "$LIMITERS_ID"
    }
  ]
}

Example of a Secret / Workers KV metadata.json

{
  "body_part": "script",
  "bindings": [
    {
      "type": "secret_text",
      "name": "GREETING",
      "text": "Howdy"
    },
    {
      "type": "kv_namespace",
      "name": "helloNamespace",
      "namespace_id": "123"
    }
  ]
}

Example of a Text / Secret Inherits / KV / DO metadata.json

{
  "body_part": "script",
  "bindings": [
    { "name": "ENV_EXAMPLE", "text": "$value", "type": "plain_text" },
    { "name": "SECRET_EXAMPLE", "type": "inherit" },
    {
      "name": "KV_NAMESPACE_EXAMPLE",
      "type": "kv_namespace",
      "namespace_id": "$kv_namespace_id"
    },
    {
      "name": "DO_NAMESPACE_EXAMPLE",
      "type": "durable_object_namespace",
      "namespace_id": "$do_namespace_id"
    }
  ]
}

Text can be useful for example for translations
Secrets inherits just do the binding and allow you to NOT pass the secret itself itself in the file or through the web when uploading. You define the secret itself in the Cloudflare Console.

Sources

https://github.com/cloudflare/workers-chat-demo/blob/main/publish.sh#L91-L95
https://github.com/cloudflare/workers-chat-demo/blob/main/publish.sh#L131-L145
https://github.com/cloudflare/rustwasm-worker-template/blob/master/worker/metadata_wasm.json

https://developers.cloudflare.com/workers/platform/scripts#resource-bindings

3 Likes

Awesome stuff, will also make it easier for those that want to use custom bundlers like esbuild.

2 Likes