Can't get env.db binding for D1

I’m trying to create a worker module and attach the D1 database to it using public Cloudflare API. The database is created in advance.

This is the metadata I’m sending (the official documentation has incorrect doc on how to pass bindings, it is a correct way to do it):

{
  main_module: 'worker.js',
  bindings: [
    {
      type: 'd1',
      name: 'db',
      id: '<db-id>'
    }
  ]
}

It successfully creates a worker. And when I try to access env.db I get an empty object {}.

I will be thankful if someone can help me and I completely understand that D1 is in alpha right now.

Please don’t suggest me to do it using Wrangler, I need to do it using API request.

2 Likes