Cannot update worker settings via API

Context:
I am using D1 bindings in my workers. I am deploying my workers using terraform. Terraform does not support D1 until GA, so after every CI deploy, the binding set via dashboard ‘ClickOps’ is removed.
I thought I can fix this, by scripting CF API call after terraform deploy as temporary measure.

Problem
Despite following API docs here https://developers.cloudflare.com/api/operations/worker-script-patch-settings I am getting error responses.

My code

// for brevity we'll try to update compatibility date only
  const url = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/workers/scripts/${SERVICE_NAME}/settings`;
  const body = new FormData();
  body.set('settings', JSON.stringify({ compatibility_date: '2022-04-05' }));
  const options: RequestInit = {
    method: 'PATCH',
    headers: {
      'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
      Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
    },
    body,
  };
  const result: any = await fetch(url, options).then(async res => res.json());

Expected
Settings are updated

Actual
Getting the following error

{
  result: null,
  success: false,
  errors: [
    { code: 10201, message: 'workers.api.error.missing_settings_part' }
  ],
  messages: []
}

Googling didn’t bring any meaningful results other then the one here https://www.reddit.com/r/CloudFlare/comments/16715sk/api_chaos/
Any help/advice would be appreciated.
Also, what is the timeframe for D1 General Availability?

So, a little further down in the reddit article you linked someone found the answer…

I’ll reiterate here to hopefully save someone elses sanity:

What the request expects is a content type of multipart/form-data and an attached json encoded file called settings.

1 Like